Skip to content

Commit

Permalink
feat(@angular/cli): make the common chunk optional
Browse files Browse the repository at this point in the history
- Add a "--no-common-chunk" build option for disabling the common async chunk.
- The existing behavior is maintained by default.

closes #7021
  • Loading branch information
kevinbuhmann authored and Brocco committed Jul 19, 2017
1 parent aebf4c8 commit 26e9433
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 9 deletions.
10 changes: 10 additions & 0 deletions docs/documentation/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ Note: service worker support is experimental and subject to change.
</p>
</details>

<details>
<summary>common-chunk</summary>
<p>
<code>--common-chunk</code> (aliases: <code>-cc</code>) <em>default value: true</em>
</p>
<p>
Use a separate bundle containing code used across multiple bundles.
</p>
</details>

<details>
<summary>verbose</summary>
<p>
Expand Down
10 changes: 10 additions & 0 deletions docs/documentation/eject.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ ng eject
</p>
</details>

<details>
<summary>common-chunk</summary>
<p>
<code>--common-chunk</code> (aliases: <code>-cc</code>) <em>default value: true</em>
</p>
<p>
Use a separate bundle containing code used across multiple bundles.
</p>
</details>

<details>
<summary>verbose</summary>
<p>
Expand Down
10 changes: 10 additions & 0 deletions docs/documentation/serve.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ All the build Options are available in serve, below are the additional options.
</p>
</details>

<details>
<summary>common-chunk</summary>
<p>
<code>--common-chunk</code> (aliases: <code>-cc</code>) <em>default value: true</em>
</p>
<p>
Use a separate bundle containing code used across multiple bundles.
</p>
</details>

<details>
<summary>verbose</summary>
<p>
Expand Down
10 changes: 9 additions & 1 deletion packages/@angular/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Command = require('../ember-cli/lib/models/command');
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
const buildConfigDefaults = config.getPaths('defaults.build', [
'sourcemaps', 'baseHref', 'progress', 'poll', 'deleteOutputPath', 'preserveSymlinks',
'showCircularDependencies'
'showCircularDependencies', 'commonChunk'
]);

// defaults for BuildOptions
Expand Down Expand Up @@ -52,6 +52,14 @@ export const baseBuildCommandOptions: any = [
aliases: ['vc'],
description: 'Use a separate bundle containing only vendor libraries.'
},
{
name: 'common-chunk',
type: Boolean,
default: buildConfigDefaults['common-chunk'] === undefined ?
true : buildConfigDefaults['common-chunk'],
aliases: ['cc'],
description: 'Use a separate bundle containing code used across multiple bundles.'
},
{
name: 'base-href',
type: String,
Expand Down
5 changes: 5 additions & 0 deletions packages/@angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@
"description": "Show circular dependency warnings on builds.",
"type": "boolean",
"default": true
},
"commonChunk": {
"description": "Use a separate bundle containing code used across multiple bundles.",
"type": "boolean",
"default": true
}
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/models/build-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface BuildOptions {
aot?: boolean;
sourcemaps?: boolean;
vendorChunk?: boolean;
commonChunk?: boolean;
baseHref?: string;
deployUrl?: string;
verbose?: boolean;
Expand Down
15 changes: 9 additions & 6 deletions packages/@angular/cli/models/webpack-configs/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
}));
}

if (buildOptions.commonChunk) {
extraPlugins.push(new webpack.optimize.CommonsChunkPlugin({
name: 'main',
async: 'common',
children: true,
minChunks: 2
}));
}

return {
plugins: [
new HtmlWebpackPlugin({
Expand All @@ -68,12 +77,6 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
new BaseHrefWebpackPlugin({
baseHref: buildOptions.baseHref
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'main',
async: 'common',
children: true,
minChunks: 2
}),
new webpack.optimize.CommonsChunkPlugin({
minChunks: Infinity,
name: 'inline'
Expand Down
16 changes: 14 additions & 2 deletions tests/e2e/tests/misc/common-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {oneLine} from 'common-tags';

import {ng, npm} from '../../utils/process';
import {addImportToModule} from '../../utils/ast';
import {appendToFile} from '../../utils/fs';
import {appendToFile, expectFileToExist} from '../../utils/fs';
import {expectToFail} from '../../utils/utils';


export default function() {
Expand Down Expand Up @@ -42,13 +43,24 @@ export default function() {
console.log(moment);
`))
.then(() => ng('build'))
.then(() => expectFileToExist('dist/common.chunk.js'))
.then(() => readdirSync('dist').length)
.then(currentNumberOfDistFiles => {
if (oldNumberOfFiles >= currentNumberOfDistFiles) {
throw new Error('A bundle for the common async module was not created.');
throw new Error(oneLine`The build contains the wrong number of files.
The test for 'dist/common.chunk.js' to exist should have failed.`);
}
oldNumberOfFiles = currentNumberOfDistFiles;
})
.then(() => ng('build', '--no-common-chunk'))
.then(() => expectToFail(() => expectFileToExist('dist/common.chunk.js')))
.then(() => readdirSync('dist').length)
.then(currentNumberOfDistFiles => {
if (oldNumberOfFiles <= currentNumberOfDistFiles) {
throw new Error(oneLine`The build contains the wrong number of files.
The test for 'dist/common.chunk.js' not to exist should have failed.`);
}
})
// Check for AoT and lazy routes.
.then(() => ng('build', '--aot'))
.then(() => readdirSync('dist').length)
Expand Down

0 comments on commit 26e9433

Please sign in to comment.