diff --git a/docs/documentation/build.md b/docs/documentation/build.md index 3bcd49be66af..4ae7f45898e9 100644 --- a/docs/documentation/build.md +++ b/docs/documentation/build.md @@ -293,6 +293,16 @@ Note: service worker support is experimental and subject to change.

+
+ common-chunk +

+ --common-chunk (aliases: -cc) default value: true +

+

+ Use a separate bundle containing code used across multiple bundles. +

+
+
verbose

diff --git a/docs/documentation/eject.md b/docs/documentation/eject.md index eba0b0ff8a61..27a671da7167 100644 --- a/docs/documentation/eject.md +++ b/docs/documentation/eject.md @@ -182,6 +182,16 @@ ng eject

+
+ common-chunk +

+ --common-chunk (aliases: -cc) default value: true +

+

+ Use a separate bundle containing code used across multiple bundles. +

+
+
verbose

diff --git a/docs/documentation/serve.md b/docs/documentation/serve.md index 3a237ed8ff60..b0ae3b70f187 100644 --- a/docs/documentation/serve.md +++ b/docs/documentation/serve.md @@ -258,6 +258,16 @@ All the build Options are available in serve, below are the additional options.

+
+ common-chunk +

+ --common-chunk (aliases: -cc) default value: true +

+

+ Use a separate bundle containing code used across multiple bundles. +

+
+
verbose

diff --git a/packages/@angular/cli/commands/build.ts b/packages/@angular/cli/commands/build.ts index 32ebd760dcde..fb609ae4a322 100644 --- a/packages/@angular/cli/commands/build.ts +++ b/packages/@angular/cli/commands/build.ts @@ -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 @@ -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, diff --git a/packages/@angular/cli/lib/config/schema.json b/packages/@angular/cli/lib/config/schema.json index 0b20cd903371..3cda338df6aa 100644 --- a/packages/@angular/cli/lib/config/schema.json +++ b/packages/@angular/cli/lib/config/schema.json @@ -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 } } }, diff --git a/packages/@angular/cli/models/build-options.ts b/packages/@angular/cli/models/build-options.ts index 12c61595d79c..55be02530677 100644 --- a/packages/@angular/cli/models/build-options.ts +++ b/packages/@angular/cli/models/build-options.ts @@ -5,6 +5,7 @@ export interface BuildOptions { aot?: boolean; sourcemaps?: boolean; vendorChunk?: boolean; + commonChunk?: boolean; baseHref?: string; deployUrl?: string; verbose?: boolean; diff --git a/packages/@angular/cli/models/webpack-configs/browser.ts b/packages/@angular/cli/models/webpack-configs/browser.ts index a24f2c055f1b..11691a941612 100644 --- a/packages/@angular/cli/models/webpack-configs/browser.ts +++ b/packages/@angular/cli/models/webpack-configs/browser.ts @@ -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({ @@ -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' diff --git a/tests/e2e/tests/misc/common-async.ts b/tests/e2e/tests/misc/common-async.ts index b2b2ba8dd017..da5bb5c5edaa 100644 --- a/tests/e2e/tests/misc/common-async.ts +++ b/tests/e2e/tests/misc/common-async.ts @@ -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() { @@ -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)