Skip to content

Commit 826c634

Browse files
filipesilvaBrocco
authored andcommitted
feat(@angular/cli): add flag to control chunk naming
Followup to #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 9ec5b4e commit 826c634

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
@@ -342,3 +342,13 @@ Note: service worker support is experimental and subject to change.
342342
(Experimental) Enables @angular-devkit/build-optimizer optimizations when using `--aot`.
343343
</p>
344344
</details>
345+
346+
<details>
347+
<summary>named-chunks</summary>
348+
<p>
349+
<code>--named-chunks</code> (aliases: <code>-nm</code>)
350+
</p>
351+
<p>
352+
Use file name for lazy loaded chunks.
353+
</p>
354+
</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', 'commonChunk'
12+
'showCircularDependencies', 'commonChunk', 'namedChunks'
1313
]);
1414

1515
// defaults for BuildOptions
@@ -166,6 +166,13 @@ export const baseBuildCommandOptions: any = [
166166
aliases: ['bo'],
167167
description: '(Experimental) Enables @angular-devkit/build-optimizer '
168168
+ 'optimizations when using `--aot`.'
169+
},
170+
{
171+
name: 'named-chunks',
172+
type: Boolean,
173+
aliases: ['nc'],
174+
description: 'Use file name for lazy loaded chunks.',
175+
default: buildConfigDefaults['namedChunks']
169176
}
170177
];
171178

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

+4
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@
486486
"description": "Use a separate bundle containing code used across multiple bundles.",
487487
"type": "boolean",
488488
"default": true
489+
},
490+
"namedChunks": {
491+
"description": "Use file name for lazy loaded chunks.",
492+
"type": "boolean"
489493
}
490494
}
491495
},

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

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ export interface BuildOptions {
2323
extractLicenses?: boolean;
2424
showCircularDependencies?: boolean;
2525
buildOptimizer?: boolean;
26+
namedChunks?: boolean;
2627
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,16 @@ export class NgCliWebpackConfig {
8484
environment: 'dev',
8585
outputHashing: 'media',
8686
sourcemaps: true,
87-
extractCss: false
87+
extractCss: false,
88+
namedChunks: true,
89+
aot: false
8890
},
8991
production: {
9092
environment: 'prod',
9193
outputHashing: 'all',
9294
sourcemaps: false,
9395
extractCss: true,
96+
namedChunks: false,
9497
aot: true
9598
}
9699
};

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
8282
});
8383
}
8484

85+
if (buildOptions.namedChunks) {
86+
extraPlugins.push(new NamedLazyChunksWebpackPlugin());
87+
}
88+
8589
return {
8690
resolve: {
8791
extensions: ['.ts', '.js'],
@@ -112,8 +116,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
112116
].concat(extraRules)
113117
},
114118
plugins: [
115-
new webpack.NoEmitOnErrorsPlugin(),
116-
new NamedLazyChunksWebpackPlugin(),
119+
new webpack.NoEmitOnErrorsPlugin()
117120
].concat(extraPlugins),
118121
node: {
119122
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)