Skip to content

Commit 2e11974

Browse files
committed
feat(@angular/cli): allow chunk names for imports
Followup to angular#6881 Also name chunks created via `import()` or `System.import()`, and strip `.ngfactory` from the chunk name.
1 parent 37b9882 commit 2e11974

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

packages/@angular/cli/plugins/named-lazy-chunks-webpack-plugin.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import * as webpack from 'webpack';
2-
2+
import { basename } from 'path';
3+
const AsyncDependenciesBlock = require('webpack/lib/AsyncDependenciesBlock');
4+
const ContextElementDependency = require('webpack/lib/dependencies/ContextElementDependency');
5+
const ImportDependency = require('webpack/lib/dependencies/ImportDependency');
36

47
// This just extends webpack.NamedChunksPlugin to prevent name collisions.
58
export class NamedLazyChunksWebpackPlugin extends webpack.NamedChunksPlugin {
@@ -22,15 +25,22 @@ export class NamedLazyChunksWebpackPlugin extends webpack.NamedChunksPlugin {
2225
return chunk.name;
2326
}
2427

25-
// Try to figure out if it's a lazy loaded route.
28+
// Try to figure out if it's a lazy loaded route or import().
2629
if (chunk.blocks
27-
&& chunk.blocks.length > 0
28-
&& chunk.blocks[0].dependencies
29-
&& chunk.blocks[0].dependencies.length > 0
30-
&& chunk.blocks[0].dependencies[0].lazyRouteChunkName
30+
&& chunk.blocks.length === 1
31+
&& chunk.blocks[0] instanceof AsyncDependenciesBlock
32+
&& chunk.blocks[0].dependencies.length === 1
33+
&& (chunk.blocks[0].dependencies[0] instanceof ContextElementDependency
34+
|| chunk.blocks[0].dependencies[0] instanceof ImportDependency)
3135
) {
32-
// lazyRouteChunkName was added by @ngtools/webpack.
33-
return getUniqueName(chunk.blocks[0].dependencies[0].lazyRouteChunkName);
36+
// Create chunkname from file request, stripping ngfactory and extension.
37+
const req = chunk.blocks[0].dependencies[0].request;
38+
const chunkName = basename(req).replace(/(\.ngfactory)?\.(js|ts)$/, '');
39+
if (!chunkName || chunkName === '') {
40+
// Bail out if something went wrong with the name.
41+
return null;
42+
}
43+
return getUniqueName(chunkName);
3444
}
3545

3646
return null;

packages/@ngtools/webpack/src/plugin.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,7 @@ export class AotPlugin implements Tapable {
310310
.map((key) => {
311311
const value = this._lazyRoutes[key];
312312
if (value !== null) {
313-
const dep = new ContextElementDependency(value, key);
314-
// lazyRouteChunkName is used by webpack.NamedChunksPlugin to give the
315-
// lazy loaded chunk a name.
316-
dep.lazyRouteChunkName = path.basename(key, '.ts');
317-
return dep;
313+
return new ContextElementDependency(value, key);
318314
} else {
319315
return null;
320316
}

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ export default function() {
2828
oldNumberOfFiles = currentNumberOfDistFiles;
2929

3030
if (!distFiles.includes('lazy.module.chunk.js')){
31-
throw new Error('The bundle for the lazy module did not have a name.');
31+
throw new Error('The chunk for the lazy module did not have a name.');
3232
}
3333
if (!distFiles.includes('lazy.module.0.chunk.js')){
34-
throw new Error('The bundle for the lazy module did not use a unique name.');
34+
throw new Error('The chunk for the lazy module did not use a unique name.');
3535
}
3636
})
3737
// verify System.import still works
@@ -43,11 +43,15 @@ export default function() {
4343
System.import('./lazy-' + lazyFile);
4444
`))
4545
.then(() => ng('build'))
46-
.then(() => readdirSync('dist').length)
47-
.then(currentNumberOfDistFiles => {
46+
.then(() => readdirSync('dist'))
47+
.then((distFiles) => {
48+
const currentNumberOfDistFiles = distFiles.length;
4849
if (oldNumberOfFiles >= currentNumberOfDistFiles) {
4950
throw new Error('A bundle for the lazy file was not created.');
5051
}
52+
if (!distFiles.includes('lazy-file.chunk.js')) {
53+
throw new Error('The chunk for the lazy file did not have a name.');
54+
}
5155
oldNumberOfFiles = currentNumberOfDistFiles;
5256
})
5357
// verify 'import *' syntax doesn't break lazy modules

0 commit comments

Comments
 (0)