feat(@angular/cli): add tree shaking of ngDevMode if compiling in prod

Whether Ivy is on or not does not matter.
This commit is contained in:
Hans Larsen 2018-08-07 21:27:37 -07:00 committed by Hans
parent 3886aab55b
commit cf9f86e743
2 changed files with 31 additions and 16 deletions

View File

@ -205,6 +205,9 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
alias = rxPaths(nodeModules); alias = rxPaths(nodeModules);
} catch { } } catch { }
const isIvyEnabled = wco.tsConfig.raw.angularCompilerOptions
&& wco.tsConfig.raw.angularCompilerOptions.enableIvy;
const uglifyOptions = { const uglifyOptions = {
ecma: wco.supportES2015 ? 6 : 5, ecma: wco.supportES2015 ? 6 : 5,
warnings: !!buildOptions.verbose, warnings: !!buildOptions.verbose,
@ -215,9 +218,13 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
webkit: true, webkit: true,
}, },
// On server, we don't want to compress anything. // On server, we don't want to compress anything. We still set the ngDevMode = false for it
...(buildOptions.platform == 'server' ? {} : { // to remove dev code.
compress: { compress: (buildOptions.platform == 'server' ? {
global_defs: {
ngDevMode: false,
},
} : {
pure_getters: buildOptions.buildOptimizer, pure_getters: buildOptions.buildOptimizer,
// PURE comments work best with 3 passes. // PURE comments work best with 3 passes.
// See https://github.com/webpack/webpack/issues/2899#issuecomment-317425926. // See https://github.com/webpack/webpack/issues/2899#issuecomment-317425926.
@ -225,10 +232,13 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
// Workaround known uglify-es issue // Workaround known uglify-es issue
// See https://github.com/mishoo/UglifyJS2/issues/2949#issuecomment-368070307 // See https://github.com/mishoo/UglifyJS2/issues/2949#issuecomment-368070307
inline: wco.supportES2015 ? 1 : 3, inline: wco.supportES2015 ? 1 : 3,
}
global_defs: {
ngDevMode: false,
},
}), }),
// We also want to avoid mangling on server. // We also want to avoid mangling on server.
...(buildOptions.platform == 'server' ? { mangle: false } : {}) ...(buildOptions.platform == 'server' ? { mangle: false } : {}),
}; };
return { return {

View File

@ -13,7 +13,7 @@ export default async function() {
try { try {
await createProject('ivy-project', '--experimental-ivy'); await createProject('ivy-project', '--experimental-ivy');
await ngServe('--aot'); await ngServe('--prod');
// Verify the index.html // Verify the index.html
const body = await request('http://localhost:4200/'); const body = await request('http://localhost:4200/');
@ -22,12 +22,17 @@ export default async function() {
} }
// Verify it's Ivy. // Verify it's Ivy.
const main = await request('http://localhost:4200/main.js'); const mainUrlMatch = body.match(/src="(main\.[a-z0-9]{0,32}\.js)"/);
if (!main.match(/ngComponentDef/)) { const mainUrl = mainUrlMatch && mainUrlMatch[1];
const main = await request('http://localhost:4200/' + mainUrl);
if (!main.match(/ngComponentDef\s*=/)) {
throw new Error('Ivy could not be found.'); throw new Error('Ivy could not be found.');
} }
if (main.match(/ngDevMode/)) {
throw new Error('NgDevMode was not tree shaken away.');
}
} finally { } finally {
await killAllProcesses(); await killAllProcesses();
} }
} }