Skip to content

Commit 1fe5595

Browse files
committed
feat(@angular/cli): name lazy chunks
Before: ``` $ ng build --no-progress Hash: ff03df269349b817eef4 Time: 11202ms chunk {0} 0.chunk.js, 0.chunk.js.map 1.61 kB {1} {3} [rendered] chunk {1} 1.chunk.js, 1.chunk.js.map 1.46 kB {0} {3} [rendered] chunk {2} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 160 kB {6} [initial] [rendered] chunk {3} main.bundle.js, main.bundle.js.map (main) 6.38 kB {5} [initial] [rendered] chunk {4} styles.bundle.js, styles.bundle.js.map (styles) 10.5 kB {6} [initial] [rendered] chunk {5} vendor.bundle.js, vendor.bundle.js.map (vendor) 2.16 MB [initial] [rendered] chunk {6} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered] ``` After: ``` $ ng build --no-progress Hash: 2bc12a89f40f3b4818b5 Time: 9613ms chunk {feature.module} feature.module.chunk.js, feature.module.chunk.js.map 1.46 kB {lazy.module} {main} [rendered] chunk {inline} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered] chunk {lazy.module} lazy.module.chunk.js, lazy.module.chunk.js.map 1.61 kB {feature.module} {main} [rendered] chunk {main} main.bundle.js, main.bundle.js.map (main) 6.38 kB {vendor} [initial] [rendered] chunk {polyfills} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 160 kB {inline} [initial] [rendered] chunk {styles} styles.bundle.js, styles.bundle.js.map (styles) 10.5 kB {inline} [initial] [rendered] chunk {vendor} vendor.bundle.js, vendor.bundle.js.map (vendor) 2.16 MB [initial] [rendered] ``` Fix angular#6700
1 parent 64e6b94 commit 1fe5595

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as webpack from 'webpack';
22
import * as path from 'path';
33
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
4+
import { NamedLazyChunksWebpackPlugin } from '../../plugins/named-lazy-chunks-webpack-plugin';
45
import { extraEntryParser, getOutputHashFormat } from './utils';
56
import { WebpackConfigOptions } from '../webpack-config';
67

@@ -109,7 +110,8 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
109110
].concat(extraRules)
110111
},
111112
plugins: [
112-
new webpack.NoEmitOnErrorsPlugin()
113+
new webpack.NoEmitOnErrorsPlugin(),
114+
// new NamedLazyChunksWebpackPlugin(),
113115
].concat(extraPlugins),
114116
node: {
115117
fs: 'empty',
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as webpack from 'webpack';
2+
3+
// This is just a wrapper around webpack.NamedChunksPlugin to prevent name collisions.
4+
export class NamedLazyChunksWebpackPlugin {
5+
constructor() {
6+
const nameMap = new Map();
7+
// Append a dot and number if the name already exists.
8+
const getNextName = (baseName: string) => {
9+
let num: number | null = null;
10+
const name = () => num !== null ? `${baseName}.${num}` : baseName;
11+
while (nameMap.has(name())) {
12+
num = num ? num++ : 0;
13+
}
14+
nameMap.set(name(), true);
15+
return name();
16+
};
17+
18+
return new (webpack as any).NamedChunksPlugin(function (chunk: any) {
19+
// Entry chunks have a name already, use it.
20+
if (chunk.name) {
21+
return chunk.name;
22+
}
23+
24+
// Try to figure out if it's a lazy loaded route.
25+
if (chunk.blocks
26+
&& chunk.blocks.length > 0
27+
&& chunk.blocks[0].dependencies
28+
&& chunk.blocks[0].dependencies.length > 0
29+
&& chunk.blocks[0].dependencies[0].lazyRouteChunkName
30+
) {
31+
// lazyRouteChunkName was added by @ngtools/webpack.
32+
return getNextName(chunk.blocks[0].dependencies[0].lazyRouteChunkName);
33+
}
34+
35+
return null;
36+
});
37+
}
38+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,11 @@ export class AotPlugin implements Tapable {
310310
.map((key) => {
311311
const value = this._lazyRoutes[key];
312312
if (value !== null) {
313-
return new ContextElementDependency(value, key);
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;
314318
} else {
315319
return null;
316320
}

0 commit comments

Comments
 (0)