UNPKG

4.87 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");
7const transformers_utils_1 = require("../utils/transformers-utils");
8function nsReplaceBootstrap(getNgCompiler) {
9 const shouldTransform = (fileName) => !fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts');
10 const getTypeChecker = () => getNgCompiler().typeChecker;
11 const standardTransform = function (sourceFile) {
12 const ops = [];
13 const entryModule = transformers_utils_1.getResolvedEntryModule(getNgCompiler());
14 if (!shouldTransform(sourceFile.fileName) || !entryModule) {
15 return ops;
16 }
17 // Find all identifiers.
18 const entryModuleIdentifiers = transformers_1.collectDeepNodes(sourceFile, ts.SyntaxKind.Identifier)
19 .filter(identifier => identifier.text === entryModule.className);
20 if (entryModuleIdentifiers.length === 0) {
21 return [];
22 }
23 const relativeEntryModulePath = path_1.relative(path_1.dirname(sourceFile.fileName), entryModule.path);
24 const normalizedEntryModulePath = `./${relativeEntryModulePath}`.replace(/\\/g, '/');
25 // Find the bootstrap calls.
26 entryModuleIdentifiers.forEach(entryModuleIdentifier => {
27 // Figure out if it's a `platformNativeScriptDynamic().bootstrapModule(AppModule)` call.
28 if (!(entryModuleIdentifier.parent
29 && entryModuleIdentifier.parent.kind === ts.SyntaxKind.CallExpression)) {
30 return;
31 }
32 const bootstrapCallExpr = entryModuleIdentifier.parent;
33 if (bootstrapCallExpr.expression.kind !== ts.SyntaxKind.PropertyAccessExpression) {
34 return;
35 }
36 const bootstrapPropAccessExpr = bootstrapCallExpr.expression;
37 if (bootstrapPropAccessExpr.name.text !== 'bootstrapModule'
38 || bootstrapPropAccessExpr.expression.kind !== ts.SyntaxKind.CallExpression) {
39 return;
40 }
41 const nsPlatformCallExpr = bootstrapPropAccessExpr.expression;
42 if (!(ast_utils_1.getExpressionName(nsPlatformCallExpr.expression) === 'platformNativeScriptDynamic')) {
43 return;
44 }
45 const idPlatformNativeScript = ts.createUniqueName('__NgCli_bootstrap_1');
46 const idNgFactory = ts.createUniqueName('__NgCli_bootstrap_2');
47 const firstNode = transformers_1.getFirstNode(sourceFile);
48 // Add the transform operations.
49 const factoryClassName = entryModule.className + 'NgFactory';
50 const factoryModulePath = normalizedEntryModulePath + '.ngfactory';
51 const newBootstrapPropAccessExpr = ts.getMutableClone(bootstrapPropAccessExpr);
52 const newNsPlatformCallExpr = ts.getMutableClone(bootstrapPropAccessExpr.expression);
53 newNsPlatformCallExpr.expression = ts.createPropertyAccess(idPlatformNativeScript, 'platformNativeScript');
54 newBootstrapPropAccessExpr.expression = newNsPlatformCallExpr;
55 newBootstrapPropAccessExpr.name = ts.createIdentifier("bootstrapModuleFactory");
56 const newBootstrapCallExpr = ts.getMutableClone(bootstrapCallExpr);
57 newBootstrapCallExpr.expression = newBootstrapPropAccessExpr;
58 newBootstrapCallExpr.arguments = ts.createNodeArray([
59 ts.createPropertyAccess(idNgFactory, ts.createIdentifier(factoryClassName))
60 ]);
61 ops.push(
62 // Insert an import of the {N} Angular static bootstrap module in the beginning of the file:
63 // import * as __NgCli_bootstrap_2 from "nativescript-angular/platform-static";
64 ...transformers_1.insertStarImport(sourceFile, idPlatformNativeScript, 'nativescript-angular/platform-static', firstNode, true),
65 // Insert an import of the module factory in the beginning of the file:
66 // import * as __NgCli_bootstrap_1 from "./app.module.ngfactory";
67 ...transformers_1.insertStarImport(sourceFile, idNgFactory, factoryModulePath, firstNode, true),
68 // Replace the bootstrap call expression. For example:
69 // from: platformNativeScriptDynamic().bootstrapModule(AppModule);
70 // to: platformNativeScript().bootstrapModuleFactory(__NgCli_bootstrap_2.AppModuleNgFactory);
71 new transformers_1.ReplaceNodeOperation(sourceFile, bootstrapCallExpr, newBootstrapCallExpr));
72 });
73 return ops;
74 };
75 return transformers_1.makeTransform(standardTransform, getTypeChecker);
76}
77exports.nsReplaceBootstrap = nsReplaceBootstrap;
78//# sourceMappingURL=ns-replace-bootstrap.js.map
\No newline at end of file