File tree 8 files changed +71
-6
lines changed
8 files changed +71
-6
lines changed Original file line number Diff line number Diff line change 1
1
import * as webpack from 'webpack' ;
2
2
import * as path from 'path' ;
3
3
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin' ;
4
+ import { NamedLazyChunksWebpackPlugin } from '../../plugins/named-lazy-chunks-webpack-plugin' ;
4
5
import { extraEntryParser , getOutputHashFormat } from './utils' ;
5
6
import { WebpackConfigOptions } from '../webpack-config' ;
6
7
@@ -109,7 +110,8 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
109
110
] . concat ( extraRules )
110
111
} ,
111
112
plugins : [
112
- new webpack . NoEmitOnErrorsPlugin ( )
113
+ new webpack . NoEmitOnErrorsPlugin ( ) ,
114
+ new NamedLazyChunksWebpackPlugin ( ) ,
113
115
] . concat ( extraPlugins ) ,
114
116
node : {
115
117
fs : 'empty' ,
Original file line number Diff line number Diff line change @@ -97,7 +97,7 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
97
97
new webpack . EnvironmentPlugin ( {
98
98
'NODE_ENV' : 'production'
99
99
} ) ,
100
- new ( < any > webpack ) . HashedModuleIdsPlugin ( ) ,
100
+ new webpack . HashedModuleIdsPlugin ( ) ,
101
101
new webpack . optimize . UglifyJsPlugin ( < any > {
102
102
mangle : { screw_ie8 : true } ,
103
103
compress : { screw_ie8 : true , warnings : buildOptions . verbose } ,
Original file line number Diff line number Diff line change
1
+ import * as webpack from 'webpack' ;
2
+
3
+
4
+ // This just extends webpack.NamedChunksPlugin to prevent name collisions.
5
+ export class NamedLazyChunksWebpackPlugin extends webpack . NamedChunksPlugin {
6
+ constructor ( ) {
7
+ const nameMap = new Map ( ) ;
8
+ // Append a dot and number if the name already exists.
9
+ const getNextName = ( baseName : string ) => {
10
+ let num : number | null = null ;
11
+ const name = ( ) => num !== null ? `${ baseName } .${ num } ` : baseName ;
12
+ while ( nameMap . has ( name ( ) ) ) {
13
+ num = num ? num ++ : 0 ;
14
+ }
15
+ nameMap . set ( name ( ) , true ) ;
16
+ return name ( ) ;
17
+ } ;
18
+
19
+ const nameResolver = ( chunk : any ) => {
20
+ // Entry chunks have a name already, use it.
21
+ if ( chunk . name ) {
22
+ return chunk . name ;
23
+ }
24
+
25
+ // Try to figure out if it's a lazy loaded route.
26
+ 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
31
+ ) {
32
+ // lazyRouteChunkName was added by @ngtools /webpack.
33
+ return getNextName ( chunk . blocks [ 0 ] . dependencies [ 0 ] . lazyRouteChunkName ) ;
34
+ }
35
+
36
+ return null ;
37
+ } ;
38
+
39
+ super ( nameResolver ) ;
40
+ }
41
+ }
Original file line number Diff line number Diff line change @@ -6,5 +6,7 @@ module.exports = {
6
6
GlobCopyWebpackPlugin : require ( '../plugins/glob-copy-webpack-plugin' ) . GlobCopyWebpackPlugin ,
7
7
SuppressExtractedTextChunksWebpackPlugin :
8
8
require ( '../plugins/suppress-entry-chunks-webpack-plugin' )
9
- . SuppressExtractedTextChunksWebpackPlugin
9
+ . SuppressExtractedTextChunksWebpackPlugin ,
10
+ NamedLazyChunksWebpackPlugin :
11
+ require ( '../plugins/named-lazy-chunks-webpack-plugin' ) . NamedLazyChunksWebpackPlugin
10
12
} ;
Original file line number Diff line number Diff line change @@ -175,6 +175,7 @@ class JsonWebpackSerializer {
175
175
this . _addImport ( 'webpack.optimize' , 'UglifyJsPlugin' ) ;
176
176
break ;
177
177
case angularCliPlugins . BaseHrefWebpackPlugin :
178
+ case angularCliPlugins . NamedLazyChunksWebpackPlugin :
178
179
case angularCliPlugins . SuppressExtractedTextChunksWebpackPlugin :
179
180
this . _addImport ( '@angular/cli/plugins/webpack' , plugin . constructor . name ) ;
180
181
break ;
Original file line number Diff line number Diff line change
1
+ import * as webpack from 'webpack' ;
2
+
3
+ declare module 'webpack' {
4
+ export class NamedChunksPlugin {
5
+ constructor ( nameResolver : ( chunk : any ) => string | null ) ;
6
+ }
7
+ export class HashedModuleIdsPlugin {
8
+ constructor ( ) ;
9
+ }
10
+ }
Original file line number Diff line number Diff line change @@ -310,7 +310,11 @@ export class AotPlugin implements Tapable {
310
310
. map ( ( key ) => {
311
311
const value = this . _lazyRoutes [ key ] ;
312
312
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 ;
314
318
} else {
315
319
return null ;
316
320
}
Original file line number Diff line number Diff line change @@ -17,12 +17,17 @@ export default function() {
17
17
RouterModule.forRoot([{ path: "lazy1", loadChildren: "./lazy/lazy.module#LazyModule" }])
18
18
` , '@angular/router' ) )
19
19
. then ( ( ) => ng ( 'build' ) )
20
- . then ( ( ) => readdirSync ( 'dist' ) . length )
21
- . then ( currentNumberOfDistFiles => {
20
+ . then ( ( ) => readdirSync ( 'dist' ) )
21
+ . then ( ( distFiles ) => {
22
+ const currentNumberOfDistFiles = distFiles . length ;
22
23
if ( oldNumberOfFiles >= currentNumberOfDistFiles ) {
23
24
throw new Error ( 'A bundle for the lazy module was not created.' ) ;
24
25
}
25
26
oldNumberOfFiles = currentNumberOfDistFiles ;
27
+
28
+ if ( ! distFiles . includes ( 'lazy.module.chunk.js' ) ) {
29
+ throw new Error ( 'The bundle for the lazy module did not have a name.' ) ;
30
+ }
26
31
} )
27
32
// verify System.import still works
28
33
. then ( ( ) => writeFile ( 'src/app/lazy-file.ts' , '' ) )
You can’t perform that action at this time.
0 commit comments