@@ -11,21 +11,22 @@ class ReactSSRDynamicChunkPlugin {
11
11
apply ( compiler ) {
12
12
compiler . hooks . emit . tap ( 'ReactSSRDynamicChunkPlugin' , ( compilation , callback ) => {
13
13
const buildPath = compilation . options . output . path ;
14
-
15
- if ( ! fs . existsSync ( buildPath ) ) {
16
- mkdirp . sync ( buildPath ) ;
17
- }
18
-
19
14
compilation . chunks . forEach ( chunk => {
20
15
if ( ! this . opts . chunk ) {
21
16
return ;
22
17
}
23
18
19
+ const chunks = chunk . files || [ ] ;
24
20
const asyncChunks = chunk . getAllAsyncChunks ( ) ;
25
-
21
+ const mainChunkFile = chunks . length > 0 ? chunks [ 0 ] : null ;
22
+ const mainChunkDir = mainChunkFile ? path . dirname ( mainChunkFile ) : null ;
26
23
asyncChunks && asyncChunks . forEach ( asyncChunk => {
27
24
asyncChunk . files . forEach ( filename => {
28
- const filepath = path . join ( buildPath , filename ) ;
25
+ const filepath = mainChunkDir ? path . join ( buildPath , mainChunkDir , filename ) : path . join ( buildPath , filename ) ;
26
+ const filedir = path . dirname ( filepath ) ;
27
+ if ( ! fs . existsSync ( filedir ) ) {
28
+ mkdirp . sync ( filedir ) ;
29
+ }
29
30
const source = compilation . assets [ filename ] . source ( ) ;
30
31
fs . writeFileSync ( filepath , source , 'utf8' ) ;
31
32
} ) ;
@@ -36,4 +37,4 @@ class ReactSSRDynamicChunkPlugin {
36
37
}
37
38
}
38
39
39
- module . exports = ReactSSRDynamicChunkPlugin ;
40
+ module . exports = ReactSSRDynamicChunkPlugin ;
0 commit comments