Skip to content

Commit 26e9433

Browse files
kevinbuhmannBrocco
authored andcommitted
feat(@angular/cli): make the common chunk optional
- Add a "--no-common-chunk" build option for disabling the common async chunk. - The existing behavior is maintained by default. closes #7021
1 parent aebf4c8 commit 26e9433

File tree

8 files changed

+68
-9
lines changed

8 files changed

+68
-9
lines changed

docs/documentation/build.md

+10
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ Note: service worker support is experimental and subject to change.
293293
</p>
294294
</details>
295295

296+
<details>
297+
<summary>common-chunk</summary>
298+
<p>
299+
<code>--common-chunk</code> (aliases: <code>-cc</code>) <em>default value: true</em>
300+
</p>
301+
<p>
302+
Use a separate bundle containing code used across multiple bundles.
303+
</p>
304+
</details>
305+
296306
<details>
297307
<summary>verbose</summary>
298308
<p>

docs/documentation/eject.md

+10
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ ng eject
182182
</p>
183183
</details>
184184

185+
<details>
186+
<summary>common-chunk</summary>
187+
<p>
188+
<code>--common-chunk</code> (aliases: <code>-cc</code>) <em>default value: true</em>
189+
</p>
190+
<p>
191+
Use a separate bundle containing code used across multiple bundles.
192+
</p>
193+
</details>
194+
185195
<details>
186196
<summary>verbose</summary>
187197
<p>

docs/documentation/serve.md

+10
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,16 @@ All the build Options are available in serve, below are the additional options.
258258
</p>
259259
</details>
260260

261+
<details>
262+
<summary>common-chunk</summary>
263+
<p>
264+
<code>--common-chunk</code> (aliases: <code>-cc</code>) <em>default value: true</em>
265+
</p>
266+
<p>
267+
Use a separate bundle containing code used across multiple bundles.
268+
</p>
269+
</details>
270+
261271
<details>
262272
<summary>verbose</summary>
263273
<p>

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Command = require('../ember-cli/lib/models/command');
99
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
1010
const buildConfigDefaults = config.getPaths('defaults.build', [
1111
'sourcemaps', 'baseHref', 'progress', 'poll', 'deleteOutputPath', 'preserveSymlinks',
12-
'showCircularDependencies'
12+
'showCircularDependencies', 'commonChunk'
1313
]);
1414

1515
// defaults for BuildOptions
@@ -52,6 +52,14 @@ export const baseBuildCommandOptions: any = [
5252
aliases: ['vc'],
5353
description: 'Use a separate bundle containing only vendor libraries.'
5454
},
55+
{
56+
name: 'common-chunk',
57+
type: Boolean,
58+
default: buildConfigDefaults['common-chunk'] === undefined ?
59+
true : buildConfigDefaults['common-chunk'],
60+
aliases: ['cc'],
61+
description: 'Use a separate bundle containing code used across multiple bundles.'
62+
},
5563
{
5664
name: 'base-href',
5765
type: String,

packages/@angular/cli/lib/config/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@
481481
"description": "Show circular dependency warnings on builds.",
482482
"type": "boolean",
483483
"default": true
484+
},
485+
"commonChunk": {
486+
"description": "Use a separate bundle containing code used across multiple bundles.",
487+
"type": "boolean",
488+
"default": true
484489
}
485490
}
486491
},

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

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface BuildOptions {
55
aot?: boolean;
66
sourcemaps?: boolean;
77
vendorChunk?: boolean;
8+
commonChunk?: boolean;
89
baseHref?: string;
910
deployUrl?: string;
1011
verbose?: boolean;

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
5151
}));
5252
}
5353

54+
if (buildOptions.commonChunk) {
55+
extraPlugins.push(new webpack.optimize.CommonsChunkPlugin({
56+
name: 'main',
57+
async: 'common',
58+
children: true,
59+
minChunks: 2
60+
}));
61+
}
62+
5463
return {
5564
plugins: [
5665
new HtmlWebpackPlugin({
@@ -68,12 +77,6 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
6877
new BaseHrefWebpackPlugin({
6978
baseHref: buildOptions.baseHref
7079
}),
71-
new webpack.optimize.CommonsChunkPlugin({
72-
name: 'main',
73-
async: 'common',
74-
children: true,
75-
minChunks: 2
76-
}),
7780
new webpack.optimize.CommonsChunkPlugin({
7881
minChunks: Infinity,
7982
name: 'inline'

tests/e2e/tests/misc/common-async.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {oneLine} from 'common-tags';
33

44
import {ng, npm} from '../../utils/process';
55
import {addImportToModule} from '../../utils/ast';
6-
import {appendToFile} from '../../utils/fs';
6+
import {appendToFile, expectFileToExist} from '../../utils/fs';
7+
import {expectToFail} from '../../utils/utils';
78

89

910
export default function() {
@@ -42,13 +43,24 @@ export default function() {
4243
console.log(moment);
4344
`))
4445
.then(() => ng('build'))
46+
.then(() => expectFileToExist('dist/common.chunk.js'))
4547
.then(() => readdirSync('dist').length)
4648
.then(currentNumberOfDistFiles => {
4749
if (oldNumberOfFiles >= currentNumberOfDistFiles) {
48-
throw new Error('A bundle for the common async module was not created.');
50+
throw new Error(oneLine`The build contains the wrong number of files.
51+
The test for 'dist/common.chunk.js' to exist should have failed.`);
4952
}
5053
oldNumberOfFiles = currentNumberOfDistFiles;
5154
})
55+
.then(() => ng('build', '--no-common-chunk'))
56+
.then(() => expectToFail(() => expectFileToExist('dist/common.chunk.js')))
57+
.then(() => readdirSync('dist').length)
58+
.then(currentNumberOfDistFiles => {
59+
if (oldNumberOfFiles <= currentNumberOfDistFiles) {
60+
throw new Error(oneLine`The build contains the wrong number of files.
61+
The test for 'dist/common.chunk.js' not to exist should have failed.`);
62+
}
63+
})
5264
// Check for AoT and lazy routes.
5365
.then(() => ng('build', '--aot'))
5466
.then(() => readdirSync('dist').length)

0 commit comments

Comments
 (0)