Skip to content

Commit 07e35f7

Browse files
committed
feat(@ngtools/webpack): replace server bootstrap code
1 parent 0de9cb6 commit 07e35f7

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

packages/@ngtools/webpack/src/loader.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import {LoaderContext, ModuleReason} from './webpack';
77
const loaderUtils = require('loader-utils');
88
const NormalModule = require('webpack/lib/NormalModule');
99

10+
// This is a map of changes which need to be made
11+
const changeMap: {[key: string]: string} = {
12+
platformBrowserDynamic: 'platformBrowser',
13+
platformDynamicServer: 'platformServer'
14+
};
15+
16+
const importLocations: { [key: string]: string } = {
17+
platformBrowser: '@angular/platform-browser',
18+
platformServer: '@angular/platform-server'
19+
};
1020

1121
function _getContentOfKeyLiteral(_source: ts.SourceFile, node: ts.Node): string {
1222
if (!node) {
@@ -205,9 +215,10 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
205215
= refactor.findAstNodes(access, ts.SyntaxKind.CallExpression, true) as ts.CallExpression[];
206216
return previous.concat(expressions);
207217
}, [])
218+
.filter((call: ts.CallExpression) => call.expression.kind == ts.SyntaxKind.Identifier)
208219
.filter((call: ts.CallExpression) => {
209-
return call.expression.kind == ts.SyntaxKind.Identifier
210-
&& (call.expression as ts.Identifier).text == 'platformBrowserDynamic';
220+
// Find if the expression matches one of the replacement targets
221+
return !!changeMap[(call.expression as ts.Identifier).text];
211222
});
212223

213224
if (calls.length == 0) {
@@ -222,16 +233,25 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
222233
refactor.replaceNode(call.arguments[0], entryModule.className + 'NgFactory');
223234
});
224235

225-
calls.forEach(call => refactor.replaceNode(call.expression, 'platformBrowser'));
236+
calls.forEach(call => {
237+
// Replace with mapped replacement
238+
refactor.replaceNode(call.expression, changeMap[(call.expression as ts.Identifier).text]);
239+
});
226240

227241
bootstraps
228242
.forEach((bs: ts.PropertyAccessExpression) => {
229243
// This changes the call.
230244
refactor.replaceNode(bs.name, 'bootstrapModuleFactory');
231245
});
232246

233-
refactor.insertImport('platformBrowser', '@angular/platform-browser');
234247
refactor.insertImport(entryModule.className + 'NgFactory', ngFactoryPath);
248+
249+
// We need to import the additional imports which are used
250+
Object.keys(importLocations)
251+
.filter(imp => refactor.sourceMatch(new RegExp(imp)))
252+
.forEach(imp => {
253+
refactor.insertImport(imp, importLocations[imp]);
254+
});
235255
}
236256

237257
export function removeModuleIdOnlyForTesting(refactor: TypeScriptFileRefactor) {

0 commit comments

Comments
 (0)