Skip to content

feat(@angular/cli): make the common chunk optional #7023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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