fix(@angular/cli): improve rebundling speed

Forcing TypeScript to output commonjs modules instead of es2015 modules drastically improves rebuild speeds, especially for AOT.

This PR forces this option on the following modes:
- `ng build --watch --target=development`
- `ng serve --target=development`
- `ng test --code-coverage=false`

Please note that `--target=development` and `--code-coverage=false` are the defaults.

See https://github.com/webpack/webpack/issues/5863 for the webpack issue.
This commit is contained in:
Filipe Silva 2017-10-20 15:41:12 +01:00 committed by Hans
parent 5c93e107f8
commit 28e66f352d
6 changed files with 27 additions and 0 deletions

View File

@ -220,6 +220,11 @@ const BuildCommand = Command.extend({
commandOptions.vendorChunk = !commandOptions.buildOptimizer;
}
// Force commonjs module format for TS on dev watch builds.
if (commandOptions.target === 'development' && commandOptions.watch === true) {
commandOptions.forceTsCommonjs = true;
}
const BuildTask = require('../tasks/build').default;
const buildTask = new BuildTask({

View File

@ -135,6 +135,11 @@ const ServeCommand = Command.extend({
commandOptions.vendorChunk = !commandOptions.buildOptimizer;
}
// Force commonjs module format for TS on dev builds.
if (commandOptions.target === 'development') {
commandOptions.forceTsCommonjs = true;
}
// Default evalSourcemaps to true for serve. This makes rebuilds faster.
commandOptions.evalSourcemaps = true;

View File

@ -24,6 +24,7 @@ export interface TestOptions {
environment?: string;
app?: string;
preserveSymlinks?: boolean;
forceTsCommonjs?: boolean;
}
@ -134,6 +135,13 @@ const TestCommand = Command.extend({
// if not watching ensure karma is doing a single run
commandOptions.singleRun = true;
}
// Don't force commonjs for code coverage builds, some setups need es2015 for it.
// https://github.com/angular/angular-cli/issues/5526
if (!commandOptions.codeCoverage) {
commandOptions.forceTsCommonjs = true;
}
return testTask.run(commandOptions);
}
});

View File

@ -30,4 +30,5 @@ export interface BuildOptions {
buildOptimizer?: boolean;
namedChunks?: boolean;
subresourceIntegrity?: boolean;
forceTsCommonjs?: boolean;
}

View File

@ -21,10 +21,17 @@ const webpackLoader: string = g['angularCliIsLocal']
function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
const { appConfig, projectRoot, buildOptions } = wco;
options.compilerOptions = options.compilerOptions || {};
if (wco.buildOptions.preserveSymlinks) {
options.compilerOptions.preserveSymlinks = true;
}
// Forcing commonjs seems to drastically improve rebuild speeds on webpack.
// Dev builds on watch mode will set this option to true.
if (wco.buildOptions.forceTsCommonjs) {
options.compilerOptions.module = 'commonjs';
}
// Read the environment, and set it in the compiler host.
let hostReplacementPaths: any = {};
// process environment file replacement

View File

@ -44,6 +44,7 @@ export default Task.extend({
poll: options.poll,
environment: options.environment,
preserveSymlinks: options.preserveSymlinks,
forceTsCommonjs: options.forceTsCommonjs,
app: options.app
};