Skip to content

Commit cf9f86e

Browse files
committed
feat(@angular/cli): add tree shaking of ngDevMode if compiling in prod
Whether Ivy is on or not does not matter.
1 parent 3886aab commit cf9f86e

File tree

2 files changed

+31
-16
lines changed
  • packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs
  • tests/legacy-cli/e2e/tests/experimental

2 files changed

+31
-16
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts

+22-12
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
205205
alias = rxPaths(nodeModules);
206206
} catch { }
207207

208+
const isIvyEnabled = wco.tsConfig.raw.angularCompilerOptions
209+
&& wco.tsConfig.raw.angularCompilerOptions.enableIvy;
210+
208211
const uglifyOptions = {
209212
ecma: wco.supportES2015 ? 6 : 5,
210213
warnings: !!buildOptions.verbose,
@@ -215,20 +218,27 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
215218
webkit: true,
216219
},
217220

218-
// On server, we don't want to compress anything.
219-
...(buildOptions.platform == 'server' ? {} : {
220-
compress: {
221-
pure_getters: buildOptions.buildOptimizer,
222-
// PURE comments work best with 3 passes.
223-
// See https://github.com/webpack/webpack/issues/2899#issuecomment-317425926.
224-
passes: buildOptions.buildOptimizer ? 3 : 1,
225-
// Workaround known uglify-es issue
226-
// See https://github.com/mishoo/UglifyJS2/issues/2949#issuecomment-368070307
227-
inline: wco.supportES2015 ? 1 : 3,
228-
}
221+
// On server, we don't want to compress anything. We still set the ngDevMode = false for it
222+
// to remove dev code.
223+
compress: (buildOptions.platform == 'server' ? {
224+
global_defs: {
225+
ngDevMode: false,
226+
},
227+
} : {
228+
pure_getters: buildOptions.buildOptimizer,
229+
// PURE comments work best with 3 passes.
230+
// See https://github.com/webpack/webpack/issues/2899#issuecomment-317425926.
231+
passes: buildOptions.buildOptimizer ? 3 : 1,
232+
// Workaround known uglify-es issue
233+
// See https://github.com/mishoo/UglifyJS2/issues/2949#issuecomment-368070307
234+
inline: wco.supportES2015 ? 1 : 3,
235+
236+
global_defs: {
237+
ngDevMode: false,
238+
},
229239
}),
230240
// We also want to avoid mangling on server.
231-
...(buildOptions.platform == 'server' ? { mangle: false } : {})
241+
...(buildOptions.platform == 'server' ? { mangle: false } : {}),
232242
};
233243

234244
return {

tests/legacy-cli/e2e/tests/experimental/ivy.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default async function() {
1313
try {
1414
await createProject('ivy-project', '--experimental-ivy');
1515

16-
await ngServe('--aot');
16+
await ngServe('--prod');
1717

1818
// Verify the index.html
1919
const body = await request('http://localhost:4200/');
@@ -22,12 +22,17 @@ export default async function() {
2222
}
2323

2424
// Verify it's Ivy.
25-
const main = await request('http://localhost:4200/main.js');
26-
if (!main.match(/ngComponentDef/)) {
25+
const mainUrlMatch = body.match(/src="(main\.[a-z0-9]{0,32}\.js)"/);
26+
const mainUrl = mainUrlMatch && mainUrlMatch[1];
27+
const main = await request('http://localhost:4200/' + mainUrl);
28+
29+
if (!main.match(/ngComponentDef\s*=/)) {
2730
throw new Error('Ivy could not be found.');
2831
}
32+
if (main.match(/ngDevMode/)) {
33+
throw new Error('NgDevMode was not tree shaken away.');
34+
}
2935
} finally {
3036
await killAllProcesses();
3137
}
3238
}
33-

0 commit comments

Comments
 (0)