11import * 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.
58export 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
2730 && chunk . blocks . length > 0
28- && chunk . blocks [ 0 ] . dependencies
29- && chunk . blocks [ 0 ] . dependencies . length > 0
30- && chunk . blocks [ 0 ] . dependencies [ 0 ] . lazyRouteChunkName
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 ( / ( \. n g f a c t o r y ) ? \. ( j s | t s ) $ / , '' ) ;
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 ;
0 commit comments