UNPKG

5.06 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path_1 = require("path");
4const ts = require("typescript");
5const transformers_1 = require("@ngtools/webpack/src/transformers");
6const ast_utils_1 = require("../utils/ast-utils");
7function nsSupportHmrNg(getNgCompiler, entryPath) {
8 const standardTransform = function (sourceFile) {
9 let ops = [];
10 if (!entryPath || path_1.normalize(sourceFile.fileName) !== path_1.normalize(entryPath)) {
11 return ops;
12 }
13 try {
14 ops = handleHmrSupport(sourceFile);
15 }
16 catch (e) {
17 ops = [];
18 }
19 return ops;
20 };
21 return transformers_1.makeTransform(standardTransform, () => getNgCompiler().typeChecker);
22}
23exports.nsSupportHmrNg = nsSupportHmrNg;
24function handleHmrSupport(mainFile) {
25 const importNodesInFile = transformers_1.collectDeepNodes(mainFile, ts.SyntaxKind.ImportDeclaration);
26 if (!importNodesInFile || !importNodesInFile.length) {
27 return [];
28 }
29 const bootstrapModuleCallNode = ast_utils_1.findBootstrapModuleCallInSource(mainFile);
30 if (!bootstrapModuleCallNode || !bootstrapModuleCallNode.arguments || !bootstrapModuleCallNode.arguments.length) {
31 return [];
32 }
33 const appModuleName = ast_utils_1.getExpressionName(bootstrapModuleCallNode.arguments[0]);
34 const nativeScriptPlatformCallNode = ast_utils_1.findNativeScriptPlatformCallInSource(mainFile);
35 if (!nativeScriptPlatformCallNode || !nativeScriptPlatformCallNode.arguments) {
36 return [];
37 }
38 return handleHmrSupportCore(mainFile, importNodesInFile, appModuleName, nativeScriptPlatformCallNode);
39}
40exports.handleHmrSupport = handleHmrSupport;
41function handleHmrSupportCore(mainFile, importNodesInFile, appModuleName, nativeScriptPlatformCallNode) {
42 const firstImportNode = importNodesInFile[0];
43 const lastImportNode = importNodesInFile[importNodesInFile.length - 1];
44 const appModulePath = ast_utils_1.findBootstrappedModulePathInSource(mainFile);
45 if (!appModuleName || !appModulePath) {
46 return [];
47 }
48 let currentAppOptionsInitializationNode = ts.createObjectLiteral();
49 if (nativeScriptPlatformCallNode.arguments.length > 0) {
50 currentAppOptionsInitializationNode = nativeScriptPlatformCallNode.arguments[0];
51 }
52 const optionsDeclaration = ts.createVariableDeclaration(exports.GeneratedDynamicAppOptions, undefined, ts.createObjectLiteral());
53 const optionsDeclarationList = ts.createVariableDeclarationList([optionsDeclaration]);
54 const optionsStatement = ts.createVariableStatement(undefined, optionsDeclarationList);
55 const setHmrOptionsNode = ts.createIdentifier(getHmrOptionsCode(appModuleName, appModulePath));
56 const acceptHmrNode = ts.createIdentifier(getAcceptMainModuleCode(appModulePath));
57 const objectAssignNode = ts.createPropertyAccess(ts.createIdentifier("Object"), ts.createIdentifier("assign"));
58 const extendAppOptionsNode = ts.createCall(objectAssignNode, undefined, [currentAppOptionsInitializationNode, ts.createIdentifier(exports.GeneratedDynamicAppOptions)]);
59 const newNsDynamicCallArgs = ts.createNodeArray([extendAppOptionsNode, ...nativeScriptPlatformCallNode.arguments.slice(1)]);
60 const nsPlatformPath = ast_utils_1.findNativeScriptPlatformPathInSource(mainFile);
61 const nsPlatformText = ast_utils_1.getExpressionName(nativeScriptPlatformCallNode.expression);
62 const newNsDynamicCallNode = ts.createCall(ts.createPropertyAccess(ts.createIdentifier(NsNgPlatformStarImport), ts.createIdentifier(nsPlatformText)), [], newNsDynamicCallArgs);
63 return [
64 ...transformers_1.insertStarImport(mainFile, ts.createIdentifier(NsNgPlatformStarImport), nsPlatformPath, firstImportNode, true),
65 new transformers_1.AddNodeOperation(mainFile, lastImportNode, undefined, optionsStatement),
66 new transformers_1.AddNodeOperation(mainFile, lastImportNode, undefined, setHmrOptionsNode),
67 new transformers_1.AddNodeOperation(mainFile, lastImportNode, undefined, acceptHmrNode),
68 new transformers_1.ReplaceNodeOperation(mainFile, nativeScriptPlatformCallNode, newNsDynamicCallNode)
69 ];
70}
71exports.GeneratedDynamicAppOptions = "options_Generated";
72const NsNgPlatformStarImport = "nativescript_angular_platform_Generated";
73function getHmrOptionsCode(appModuleName, appModulePath) {
74 return `
75if (module["hot"]) {
76 ${exports.GeneratedDynamicAppOptions} = {
77 hmrOptions: {
78 moduleTypeFactory: function () { return require("${appModulePath}").${appModuleName}; },
79 livesyncCallback: function (platformReboot) { setTimeout(platformReboot, 0); }
80 }
81 };
82}
83`;
84}
85exports.getHmrOptionsCode = getHmrOptionsCode;
86function getAcceptMainModuleCode(mainModulePath) {
87 return `
88if (module["hot"]) {
89 module["hot"].accept(["${mainModulePath}"], function () {
90 global["hmrRefresh"]({});
91 });
92}
93`;
94}
95exports.getAcceptMainModuleCode = getAcceptMainModuleCode;
96//# sourceMappingURL=ns-support-hmr-ng.js.map
\No newline at end of file