Skip to content

feat(@angular/cli): add flag to control chunk naming #7007

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
9 changes: 9 additions & 0 deletions docs/documentation/angular-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@
- *service*: Options for generating a service.
- *flat* (`boolean`): Flag to indicate if a dir is created. Default is `true`.
- *spec* (`boolean`): Specifies if a spec file is generated. Default is `true`.
- *build*: Properties to be passed to the build command.
- *sourcemaps* (`boolean`): Output sourcemaps.
- *baseHref* (`string`): Base url for the application being built.
- *progress* (`boolean`): Log progress to the console while building. Default is `true`.
- *poll* (`number`): Enable and define the file watching poll time period (milliseconds).
- *deleteOutputPath* (`boolean`): Delete output path before build. Default is `true`.
- *preserveSymlinks* (`boolean`): Do not use the real path when resolving modules. Default is `false`.
- *showCircularDependencies* (`boolean`): Show circular dependency warnings on builds. Default is `true`.
- *namedChunks* (`boolean`): Use file name for lazy loaded chunks.
- *serve*: Properties to be passed to the serve command
- *port* (`number`): The port the application will be served on. Default is `4200`.
- *host* (`string`): The host the application will be served on. Default is `localhost`.
Expand Down
10 changes: 10 additions & 0 deletions docs/documentation/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,13 @@ Note: service worker support is experimental and subject to change.
(Experimental) Enables @angular-devkit/build-optimizer optimizations when using `--aot`.
</p>
</details>

<details>
<summary>named-chunks</summary>
<p>
<code>--named-chunks</code> (aliases: <code>-nm</code>)
</p>
<p>
Use file name for lazy loaded chunks.
</p>
</details>
9 changes: 8 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', 'commonChunk'
'showCircularDependencies', 'commonChunk', 'namedChunks'
]);

// defaults for BuildOptions
Expand Down Expand Up @@ -166,6 +166,13 @@ export const baseBuildCommandOptions: any = [
aliases: ['bo'],
description: '(Experimental) Enables @angular-devkit/build-optimizer '
+ 'optimizations when using `--aot`.'
},
{
name: 'named-chunks',
type: Boolean,
aliases: ['nc'],
description: 'Use file name for lazy loaded chunks.',
default: buildConfigDefaults['namedChunks']
}
];

Expand Down
4 changes: 4 additions & 0 deletions packages/@angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@
"description": "Use a separate bundle containing code used across multiple bundles.",
"type": "boolean",
"default": true
},
"namedChunks": {
"description": "Use file name for lazy loaded chunks.",
"type": "boolean"
}
}
},
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 @@ -23,4 +23,5 @@ export interface BuildOptions {
extractLicenses?: boolean;
showCircularDependencies?: boolean;
buildOptimizer?: boolean;
namedChunks?: boolean;
}
5 changes: 4 additions & 1 deletion packages/@angular/cli/models/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ export class NgCliWebpackConfig {
environment: 'dev',
outputHashing: 'media',
sourcemaps: true,
extractCss: false
extractCss: false,
namedChunks: true,
aot: false
},
production: {
environment: 'prod',
outputHashing: 'all',
sourcemaps: false,
extractCss: true,
namedChunks: false,
aot: true
}
};
Expand Down
7 changes: 5 additions & 2 deletions packages/@angular/cli/models/webpack-configs/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
});
}

if (buildOptions.namedChunks) {
extraPlugins.push(new NamedLazyChunksWebpackPlugin());
}

return {
resolve: {
extensions: ['.ts', '.js'],
Expand Down Expand Up @@ -112,8 +116,7 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
].concat(extraRules)
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new NamedLazyChunksWebpackPlugin(),
new webpack.NoEmitOnErrorsPlugin()
].concat(extraPlugins),
node: {
fs: 'empty',
Expand Down
20 changes: 15 additions & 5 deletions tests/e2e/tests/misc/lazy-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function() {
RouterModule.forRoot([{ path: "lazy1", loadChildren: "./lazy/lazy.module#LazyModule" }]),
RouterModule.forRoot([{ path: "lazy2", loadChildren: "./too/lazy/lazy.module#LazyModule" }])
`, '@angular/router'))
.then(() => ng('build'))
.then(() => ng('build', '--named-chunks'))
.then(() => readdirSync('dist'))
.then((distFiles) => {
const currentNumberOfDistFiles = distFiles.length;
Expand All @@ -28,10 +28,10 @@ export default function() {
oldNumberOfFiles = currentNumberOfDistFiles;

if (!distFiles.includes('lazy.module.chunk.js')){
throw new Error('The chunk for the lazy module did not have a name.');
throw new Error('The lazy module chunk did not have a name.');
}
if (!distFiles.includes('lazy.module.0.chunk.js')){
throw new Error('The chunk for the lazy module did not use a unique name.');
throw new Error('The lazy module chunk did not use a unique name.');
}
})
// verify System.import still works
Expand All @@ -42,15 +42,15 @@ export default function() {
const lazyFile = 'file';
System.import('./lazy-' + lazyFile);
`))
.then(() => ng('build'))
.then(() => ng('build', '--named-chunks'))
.then(() => readdirSync('dist'))
.then((distFiles) => {
const currentNumberOfDistFiles = distFiles.length;
if (oldNumberOfFiles >= currentNumberOfDistFiles) {
throw new Error('A bundle for the lazy file was not created.');
}
if (!distFiles.includes('lazy-file.chunk.js')) {
throw new Error('The chunk for the lazy file did not have a name.');
throw new Error('The lazy file chunk did not have a name.');
}
oldNumberOfFiles = currentNumberOfDistFiles;
})
Expand All @@ -67,6 +67,16 @@ export default function() {
throw new Error('Bundles were not created after adding \'import *\'.');
}
})
.then(() => ng('build', '--no-named-chunks'))
.then(() => readdirSync('dist'))
.then((distFiles) => {
if (distFiles.includes('lazy.module.chunk.js')
|| distFiles.includes('lazy.module.0.chunk.js')
|| distFiles.includes('lazy-file.chunk.js')
) {
throw new Error('Lazy chunks shouldn\'t have a name but did.');
}
})
// Check for AoT and lazy routes.
.then(() => ng('build', '--aot'))
.then(() => readdirSync('dist').length)
Expand Down