Skip to content

Commit 0326db5

Browse files
committed
feat(@angular/cli): make the common chunk optional/configurable
- Add options to the build command to disable or configure the common chunk. - The existing behavior is maintained by default. closes #7021
1 parent 93d5717 commit 0326db5

File tree

7 files changed

+92
-6
lines changed

7 files changed

+92
-6
lines changed

docs/documentation/build.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,26 @@ 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+
306+
<details>
307+
<summary>common-chunk-min-chunks</summary>
308+
<p>
309+
<code>--common-chunk-min-chunks</code> (aliases: <code>-ccm</code>) <em>default value: 2</em>
310+
</p>
311+
<p>
312+
The minimum number of bundles that must use a module before it is moved to the common chunk.
313+
</p>
314+
</details>
315+
296316
<details>
297317
<summary>verbose</summary>
298318
<p>

docs/documentation/eject.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,26 @@ 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+
195+
<details>
196+
<summary>common-chunk-min-chunks</summary>
197+
<p>
198+
<code>--common-chunk-min-chunks</code> (aliases: <code>-ccm</code>) <em>default value: 2</em>
199+
</p>
200+
<p>
201+
The minimum number of bundles that must use a module before it is moved to the common chunk.
202+
</p>
203+
</details>
204+
185205
<details>
186206
<summary>verbose</summary>
187207
<p>

docs/documentation/serve.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,26 @@ 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+
271+
<details>
272+
<summary>common-chunk-min-chunks</summary>
273+
<p>
274+
<code>--common-chunk-min-chunks</code> (aliases: <code>-ccm</code>) <em>default value: 2</em>
275+
</p>
276+
<p>
277+
The minimum number of bundles that must use a module before it is moved to the common chunk.
278+
</p>
279+
</details>
280+
261281
<details>
262282
<summary>verbose</summary>
263283
<p>

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ 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: true,
59+
aliases: ['cc'],
60+
description: 'Use a separate bundle containing code used across multiple bundles.'
61+
},
62+
{
63+
name: 'common-chunk-min-chunks',
64+
type: Number,
65+
default: 2,
66+
aliases: ['ccm'],
67+
description: oneLine`The minimum number of bundles that must use a module before it is moved to
68+
the common chunk.`
69+
},
5570
{
5671
name: 'base-href',
5772
type: String,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface BuildOptions {
55
aot?: boolean;
66
sourcemaps?: boolean;
77
vendorChunk?: boolean;
8+
commonChunk?: boolean;
9+
commonChunkMinChunks?: number;
810
baseHref?: string;
911
deployUrl?: string;
1012
verbose?: boolean;

packages/@angular/cli/models/webpack-config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ export class NgCliWebpackConfig {
7070
if (buildOptions.target !== 'development' && buildOptions.target !== 'production') {
7171
throw new Error("Invalid build target. Only 'development' and 'production' are available.");
7272
}
73+
74+
buildOptions.commonChunkMinChunks = buildOptions.commonChunk ?
75+
buildOptions.commonChunkMinChunks : undefined;
76+
if (buildOptions.commonChunkMinChunks < 2) {
77+
throw new Error('The common chunk min chunks value must be greater than or equal to 2');
78+
}
7379
}
7480

7581
// Fill in defaults for build targets

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

Lines changed: 9 additions & 6 deletions
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: buildOptions.commonChunkMinChunks
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'

0 commit comments

Comments
 (0)