Skip to content

Commit ac5eceb

Browse files
committed
feat(@angular/cli): add flag to control chunk naming
Followup to angular#6881 Allow controlling chunk naming via the `--named-chunks` flag, which can be set on `.angular-cli.json` as well. Defaults to true in development, false in production.
1 parent 93d5717 commit ac5eceb

File tree

8 files changed

+56
-9
lines changed

8 files changed

+56
-9
lines changed

docs/documentation/angular-cli.md

+9
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@
7777
- *service*: Options for generating a service.
7878
- *flat* (`boolean`): Flag to indicate if a dir is created. Default is `true`.
7979
- *spec* (`boolean`): Specifies if a spec file is generated. Default is `true`.
80+
- *build*: Properties to be passed to the build command.
81+
- *sourcemaps* (`boolean`): Output sourcemaps.
82+
- *baseHref* (`string`): Base url for the application being built.
83+
- *progress* (`boolean`): Log progress to the console while building. Default is `true`.
84+
- *poll* (`number`): Enable and define the file watching poll time period (milliseconds).
85+
- *deleteOutputPath* (`boolean`): Delete output path before build. Default is `true`.
86+
- *preserveSymlinks* (`boolean`): Do not use the real path when resolving modules. Default is `false`.
87+
- *showCircularDependencies* (`boolean`): Show circular dependency warnings on builds. Default is `true`.
88+
- *namedChunks* (`boolean`): Use file name for lazy loaded chunks.
8089
- *serve*: Properties to be passed to the serve command
8190
- *port* (`number`): The port the application will be served on. Default is `4200`.
8291
- *host* (`string`): The host the application will be served on. Default is `localhost`.

docs/documentation/build.md

+10
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,13 @@ Note: service worker support is experimental and subject to change.
322322
Show circular dependency warnings on builds.
323323
</p>
324324
</details>
325+
326+
<details>
327+
<summary>named-chunks</summary>
328+
<p>
329+
<code>--named-chunks</code> (aliases: <code>-scd</code>)
330+
</p>
331+
<p>
332+
Use file name for lazy loaded chunks.
333+
</p>
334+
</details>

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

+8-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', 'namedChunks'
1313
]);
1414

1515
// defaults for BuildOptions
@@ -151,6 +151,13 @@ export const baseBuildCommandOptions: any = [
151151
aliases: ['scd'],
152152
description: 'Show circular dependency warnings on builds.',
153153
default: buildConfigDefaults['showCircularDependencies']
154+
},
155+
{
156+
name: 'named-chunks',
157+
type: Boolean,
158+
aliases: ['nc'],
159+
description: 'Use file name for lazy loaded chunks.',
160+
default: buildConfigDefaults['namedChunks']
154161
}
155162
];
156163

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

+4
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@
481481
"description": "Show circular dependency warnings on builds.",
482482
"type": "boolean",
483483
"default": true
484+
},
485+
"namedChunks": {
486+
"description": "Use file name for lazy loaded chunks.",
487+
"type": "boolean"
484488
}
485489
}
486490
},

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

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ export interface BuildOptions {
2121
preserveSymlinks?: boolean;
2222
extractLicenses?: boolean;
2323
showCircularDependencies?: boolean;
24+
namedChunks?: boolean;
2425
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,16 @@ export class NgCliWebpackConfig {
7979
environment: 'dev',
8080
outputHashing: 'media',
8181
sourcemaps: true,
82-
extractCss: false
82+
extractCss: false,
83+
namedChunks: true,
84+
aot: false
8385
},
8486
production: {
8587
environment: 'prod',
8688
outputHashing: 'all',
8789
sourcemaps: false,
8890
extractCss: true,
91+
namedChunks: false,
8992
aot: true
9093
}
9194
};

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
7171
}));
7272
}
7373

74+
if (buildOptions.namedChunks) {
75+
extraPlugins.push(new NamedLazyChunksWebpackPlugin());
76+
}
77+
7478
return {
7579
resolve: {
7680
extensions: ['.ts', '.js'],
@@ -101,8 +105,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
101105
].concat(extraRules)
102106
},
103107
plugins: [
104-
new webpack.NoEmitOnErrorsPlugin(),
105-
new NamedLazyChunksWebpackPlugin(),
108+
new webpack.NoEmitOnErrorsPlugin()
106109
].concat(extraPlugins),
107110
node: {
108111
fs: 'empty',

tests/e2e/tests/misc/lazy-module.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default function() {
1818
RouterModule.forRoot([{ path: "lazy1", loadChildren: "./lazy/lazy.module#LazyModule" }]),
1919
RouterModule.forRoot([{ path: "lazy2", loadChildren: "./too/lazy/lazy.module#LazyModule" }])
2020
`, '@angular/router'))
21-
.then(() => ng('build'))
21+
.then(() => ng('build', '--named-chunks'))
2222
.then(() => readdirSync('dist'))
2323
.then((distFiles) => {
2424
const currentNumberOfDistFiles = distFiles.length;
@@ -28,10 +28,10 @@ export default function() {
2828
oldNumberOfFiles = currentNumberOfDistFiles;
2929

3030
if (!distFiles.includes('lazy.module.chunk.js')){
31-
throw new Error('The chunk for the lazy module did not have a name.');
31+
throw new Error('The lazy module chunk did not have a name.');
3232
}
3333
if (!distFiles.includes('lazy.module.0.chunk.js')){
34-
throw new Error('The chunk for the lazy module did not use a unique name.');
34+
throw new Error('The lazy module chunk did not use a unique name.');
3535
}
3636
})
3737
// verify System.import still works
@@ -42,15 +42,15 @@ export default function() {
4242
const lazyFile = 'file';
4343
System.import('./lazy-' + lazyFile);
4444
`))
45-
.then(() => ng('build'))
45+
.then(() => ng('build', '--named-chunks'))
4646
.then(() => readdirSync('dist'))
4747
.then((distFiles) => {
4848
const currentNumberOfDistFiles = distFiles.length;
4949
if (oldNumberOfFiles >= currentNumberOfDistFiles) {
5050
throw new Error('A bundle for the lazy file was not created.');
5151
}
5252
if (!distFiles.includes('lazy-file.chunk.js')) {
53-
throw new Error('The chunk for the lazy file did not have a name.');
53+
throw new Error('The lazy file chunk did not have a name.');
5454
}
5555
oldNumberOfFiles = currentNumberOfDistFiles;
5656
})
@@ -67,6 +67,16 @@ export default function() {
6767
throw new Error('Bundles were not created after adding \'import *\'.');
6868
}
6969
})
70+
.then(() => ng('build', '--no-named-chunks'))
71+
.then(() => readdirSync('dist'))
72+
.then((distFiles) => {
73+
if (distFiles.includes('lazy.module.chunk.js')
74+
|| distFiles.includes('lazy.module.0.chunk.js')
75+
|| distFiles.includes('lazy-file.chunk.js')
76+
) {
77+
throw new Error('Lazy chunks shouldn\'t have a name but did.');
78+
}
79+
})
7080
// Check for AoT and lazy routes.
7181
.then(() => ng('build', '--aot'))
7282
.then(() => readdirSync('dist').length)

0 commit comments

Comments
 (0)