@@ -11,21 +11,22 @@ class ReactSSRDynamicChunkPlugin {
1111 apply ( compiler ) {
1212 compiler . hooks . emit . tap ( 'ReactSSRDynamicChunkPlugin' , ( compilation , callback ) => {
1313 const buildPath = compilation . options . output . path ;
14-
15- if ( ! fs . existsSync ( buildPath ) ) {
16- mkdirp . sync ( buildPath ) ;
17- }
18-
1914 compilation . chunks . forEach ( chunk => {
2015 if ( ! this . opts . chunk ) {
2116 return ;
2217 }
2318
19+ const chunks = chunk . files || [ ] ;
2420 const asyncChunks = chunk . getAllAsyncChunks ( ) ;
25-
21+ const mainChunkFile = chunks . length > 0 ? chunks [ 0 ] : null ;
22+ const mainChunkDir = mainChunkFile ? path . dirname ( mainChunkFile ) : null ;
2623 asyncChunks && asyncChunks . forEach ( asyncChunk => {
2724 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+ }
2930 const source = compilation . assets [ filename ] . source ( ) ;
3031 fs . writeFileSync ( filepath , source , 'utf8' ) ;
3132 } ) ;
@@ -36,4 +37,4 @@ class ReactSSRDynamicChunkPlugin {
3637 }
3738}
3839
39- module . exports = ReactSSRDynamicChunkPlugin ;
40+ module . exports = ReactSSRDynamicChunkPlugin ;
0 commit comments