Skip to content

Commit 8949ecc

Browse files
filipesilvahansl
authored andcommitted
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 webpack/webpack#5863 for the webpack issue.
1 parent 8bbbfcd commit 8949ecc

File tree

6 files changed

+27
-0
lines changed

6 files changed

+27
-0
lines changed

packages/@angular/cli/commands/build.ts

+5
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ const BuildCommand = Command.extend({
220220
commandOptions.vendorChunk = !commandOptions.buildOptimizer;
221221
}
222222

223+
// Force commonjs module format for TS on dev watch builds.
224+
if (commandOptions.target === 'development' && commandOptions.watch === true) {
225+
commandOptions.forceTsCommonjs = true;
226+
}
227+
223228
const BuildTask = require('../tasks/build').default;
224229

225230
const buildTask = new BuildTask({

packages/@angular/cli/commands/serve.ts

+5
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ const ServeCommand = Command.extend({
135135
commandOptions.vendorChunk = !commandOptions.buildOptimizer;
136136
}
137137

138+
// Force commonjs module format for TS on dev builds.
139+
if (commandOptions.target === 'development') {
140+
commandOptions.forceTsCommonjs = true;
141+
}
142+
138143
// Default evalSourcemaps to true for serve. This makes rebuilds faster.
139144
commandOptions.evalSourcemaps = true;
140145

packages/@angular/cli/commands/test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface TestOptions {
2424
environment?: string;
2525
app?: string;
2626
preserveSymlinks?: boolean;
27+
forceTsCommonjs?: boolean;
2728
}
2829

2930

@@ -134,6 +135,13 @@ const TestCommand = Command.extend({
134135
// if not watching ensure karma is doing a single run
135136
commandOptions.singleRun = true;
136137
}
138+
139+
// Don't force commonjs for code coverage builds, some setups need es2015 for it.
140+
// https://github.com/angular/angular-cli/issues/5526
141+
if (!commandOptions.codeCoverage) {
142+
commandOptions.forceTsCommonjs = true;
143+
}
144+
137145
return testTask.run(commandOptions);
138146
}
139147
});

packages/@angular/cli/models/build-options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ export interface BuildOptions {
3030
buildOptimizer?: boolean;
3131
namedChunks?: boolean;
3232
subresourceIntegrity?: boolean;
33+
forceTsCommonjs?: boolean;
3334
}

packages/@angular/cli/models/webpack-configs/typescript.ts

+7
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ const webpackLoader: string = g['angularCliIsLocal']
2121
function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
2222
const { appConfig, projectRoot, buildOptions } = wco;
2323
options.compilerOptions = options.compilerOptions || {};
24+
2425
if (wco.buildOptions.preserveSymlinks) {
2526
options.compilerOptions.preserveSymlinks = true;
2627
}
2728

29+
// Forcing commonjs seems to drastically improve rebuild speeds on webpack.
30+
// Dev builds on watch mode will set this option to true.
31+
if (wco.buildOptions.forceTsCommonjs) {
32+
options.compilerOptions.module = 'commonjs';
33+
}
34+
2835
// Read the environment, and set it in the compiler host.
2936
let hostReplacementPaths: any = {};
3037
// process environment file replacement

packages/@angular/cli/tasks/test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export default Task.extend({
4444
poll: options.poll,
4545
environment: options.environment,
4646
preserveSymlinks: options.preserveSymlinks,
47+
forceTsCommonjs: options.forceTsCommonjs,
4748
app: options.app
4849
};
4950

0 commit comments

Comments
 (0)