1
1
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' ) ;
3
6
4
7
// This just extends webpack.NamedChunksPlugin to prevent name collisions.
5
8
export class NamedLazyChunksWebpackPlugin extends webpack . NamedChunksPlugin {
@@ -22,15 +25,22 @@ export class NamedLazyChunksWebpackPlugin extends webpack.NamedChunksPlugin {
22
25
return chunk . name ;
23
26
}
24
27
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() .
26
29
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 )
31
35
) {
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 ) ;
34
44
}
35
45
36
46
return null ;
0 commit comments