1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var path_1 = require("path");
|
4 | var typescript_1 = require("typescript");
|
5 | var typescript_utils_1 = require("../util/typescript-utils");
|
6 | function getFallbackMainContent() {
|
7 | return "\nimport { platformBrowser } from '@angular/platform-browser';\nimport { enableProdMode } from '@angular/core';\n\nimport { AppModuleNgFactory } from './app.module.ngfactory';\n\nenableProdMode();\nplatformBrowser().bootstrapModuleFactory(AppModuleNgFactory);";
|
8 | }
|
9 | exports.getFallbackMainContent = getFallbackMainContent;
|
10 | function getBootstrapNodes(allCalls) {
|
11 | return allCalls
|
12 | .filter(function (call) { return call.expression.kind === typescript_1.SyntaxKind.PropertyAccessExpression; })
|
13 | .map(function (call) { return call.expression; })
|
14 | .filter(function (access) {
|
15 | return access.name.kind === typescript_1.SyntaxKind.Identifier
|
16 | && access.name.text === 'bootstrapModule';
|
17 | });
|
18 | }
|
19 | function replaceNgModuleClassName(filePath, fileContent, className) {
|
20 | var sourceFile = typescript_utils_1.getTypescriptSourceFile(filePath, fileContent, typescript_1.ScriptTarget.Latest, false);
|
21 | var allCalls = typescript_utils_1.findNodes(sourceFile, sourceFile, typescript_1.SyntaxKind.CallExpression, true);
|
22 | var bootstraps = getBootstrapNodes(allCalls);
|
23 | var modifiedContent = fileContent;
|
24 | allCalls.filter(function (call) { return bootstraps.some(function (bs) { return bs === call.expression; }); }).forEach(function (call) {
|
25 | modifiedContent = typescript_utils_1.replaceNode(filePath, modifiedContent, call.arguments[0], className + 'NgFactory');
|
26 | });
|
27 | return modifiedContent;
|
28 | }
|
29 | function replacePlatformBrowser(filePath, fileContent) {
|
30 | var sourceFile = typescript_utils_1.getTypescriptSourceFile(filePath, fileContent, typescript_1.ScriptTarget.Latest, false);
|
31 | var allCalls = typescript_utils_1.findNodes(sourceFile, sourceFile, typescript_1.SyntaxKind.CallExpression, true);
|
32 | var bootstraps = getBootstrapNodes(allCalls);
|
33 | var calls = bootstraps.reduce(function (previous, access) {
|
34 | var expressions = typescript_utils_1.findNodes(sourceFile, access, typescript_1.SyntaxKind.CallExpression, true);
|
35 | return previous.concat(expressions);
|
36 | }, [])
|
37 | .filter(function (call) {
|
38 | return call.expression.kind === typescript_1.SyntaxKind.Identifier
|
39 | && call.expression.text === 'platformBrowserDynamic';
|
40 | });
|
41 | var modifiedContent = fileContent;
|
42 | calls.forEach(function (call) {
|
43 | modifiedContent = typescript_utils_1.replaceNode(filePath, modifiedContent, call.expression, 'platformBrowser');
|
44 | });
|
45 | return modifiedContent;
|
46 | }
|
47 | function checkForPlatformDynamicBrowser(filePath, fileContent) {
|
48 | var sourceFile = typescript_utils_1.getTypescriptSourceFile(filePath, fileContent, typescript_1.ScriptTarget.Latest, false);
|
49 | var allCalls = typescript_utils_1.findNodes(sourceFile, sourceFile, typescript_1.SyntaxKind.CallExpression, true);
|
50 | var bootstraps = getBootstrapNodes(allCalls);
|
51 | var calls = bootstraps.reduce(function (previous, access) {
|
52 | var expressions = typescript_utils_1.findNodes(sourceFile, access, typescript_1.SyntaxKind.CallExpression, true);
|
53 | return previous.concat(expressions);
|
54 | }, [])
|
55 | .filter(function (call) {
|
56 | return call.expression.kind === typescript_1.SyntaxKind.Identifier
|
57 | && call.expression.text === 'platformBrowserDynamic';
|
58 | });
|
59 | return calls && calls.length;
|
60 | }
|
61 | function replaceBootstrapModuleFactory(filePath, fileContent) {
|
62 | var sourceFile = typescript_utils_1.getTypescriptSourceFile(filePath, fileContent, typescript_1.ScriptTarget.Latest, false);
|
63 | var allCalls = typescript_utils_1.findNodes(sourceFile, sourceFile, typescript_1.SyntaxKind.CallExpression, true);
|
64 | var bootstraps = getBootstrapNodes(allCalls);
|
65 | var modifiedContent = fileContent;
|
66 | bootstraps.forEach(function (bs) {
|
67 | modifiedContent = typescript_utils_1.replaceNode(filePath, modifiedContent, bs.name, 'bootstrapModuleFactory');
|
68 | });
|
69 | return modifiedContent;
|
70 | }
|
71 | function getPlatformBrowserFunctionNode(filePath, fileContent) {
|
72 | var modifiedFileContent = fileContent;
|
73 | var sourceFile = typescript_utils_1.getTypescriptSourceFile(filePath, modifiedFileContent, typescript_1.ScriptTarget.Latest, false);
|
74 | var allCalls = typescript_utils_1.findNodes(sourceFile, sourceFile, typescript_1.SyntaxKind.CallExpression, true);
|
75 | var callsToPlatformBrowser = allCalls.filter(function (call) { return call.expression && call.expression.kind === typescript_1.SyntaxKind.Identifier && call.expression.text === 'platformBrowser'; });
|
76 | var toAppend = "enableProdMode();\n";
|
77 | if (callsToPlatformBrowser.length) {
|
78 | modifiedFileContent = typescript_utils_1.appendBefore(filePath, modifiedFileContent, callsToPlatformBrowser[0].expression, toAppend);
|
79 | }
|
80 | else {
|
81 |
|
82 | modifiedFileContent += toAppend;
|
83 | }
|
84 | return modifiedFileContent;
|
85 | }
|
86 | function importAndEnableProdMode(filePath, fileContent) {
|
87 | var modifiedFileContent = fileContent;
|
88 | modifiedFileContent = typescript_utils_1.insertNamedImportIfNeeded(filePath, modifiedFileContent, 'enableProdMode', '@angular/core');
|
89 | var isCalled = typescript_utils_1.checkIfFunctionIsCalled(filePath, modifiedFileContent, 'enableProdMode');
|
90 | if (!isCalled) {
|
91 |
|
92 | modifiedFileContent = getPlatformBrowserFunctionNode(filePath, modifiedFileContent);
|
93 | }
|
94 | return modifiedFileContent;
|
95 | }
|
96 | function replaceBootstrapImpl(filePath, fileContent, appNgModulePath, appNgModuleClassName) {
|
97 | if (!fileContent.match(/\bbootstrapModule\b/)) {
|
98 | throw new Error("Could not find bootstrapModule in " + filePath);
|
99 | }
|
100 | var withoutExtension = path_1.join(path_1.dirname(appNgModulePath), path_1.basename(appNgModulePath, '.ts'));
|
101 | var appModuleAbsoluteFileName = path_1.normalize(path_1.resolve(withoutExtension));
|
102 | var withNgFactory = appModuleAbsoluteFileName + '.ngfactory';
|
103 | var originalImport = './' + path_1.relative(path_1.dirname(filePath), appModuleAbsoluteFileName);
|
104 | var ngFactryImport = './' + path_1.relative(path_1.dirname(filePath), withNgFactory);
|
105 | if (!checkForPlatformDynamicBrowser(filePath, fileContent)) {
|
106 | throw new Error("Could not find any references to \"platformBrowserDynamic\" in " + filePath);
|
107 | }
|
108 | var modifiedFileContent = fileContent;
|
109 | modifiedFileContent = replaceNgModuleClassName(filePath, modifiedFileContent, appNgModuleClassName);
|
110 | modifiedFileContent = replacePlatformBrowser(filePath, modifiedFileContent);
|
111 | modifiedFileContent = replaceBootstrapModuleFactory(filePath, modifiedFileContent);
|
112 | modifiedFileContent = typescript_utils_1.replaceNamedImport(filePath, modifiedFileContent, 'platformBrowserDynamic', 'platformBrowser');
|
113 | modifiedFileContent = typescript_utils_1.replaceNamedImport(filePath, modifiedFileContent, appNgModuleClassName, appNgModuleClassName + 'NgFactory');
|
114 | modifiedFileContent = typescript_utils_1.replaceImportModuleSpecifier(filePath, modifiedFileContent, '@angular/platform-browser-dynamic', '@angular/platform-browser');
|
115 | modifiedFileContent = typescript_utils_1.replaceImportModuleSpecifier(filePath, modifiedFileContent, originalImport, ngFactryImport);
|
116 |
|
117 | modifiedFileContent = importAndEnableProdMode(filePath, modifiedFileContent);
|
118 | return modifiedFileContent;
|
119 | }
|
120 | exports.replaceBootstrapImpl = replaceBootstrapImpl;
|