1010'use strict' ;
1111
1212const fs = require ( 'fs' ) ;
13- const path = require ( 'path' ) ;
1413const sharedEntryTmpName = require ( '../utils/sharedEntryTmpName' ) ;
14+ const RawSource = require ( 'webpack-sources/lib/RawSource' ) ;
1515
16- function SharedEntryConcatPlugin ( sharedEntryName , buildDir ) {
16+ function SharedEntryConcatPlugin ( sharedEntryName ) {
1717 this . sharedEntryName = sharedEntryName ;
18- this . buildDir = buildDir ;
1918}
2019
21- function getChunkFilename ( stats , chunkName ) {
22- const chunk = stats . compilation . namedChunks . get ( chunkName ) ;
20+ function getChunkFilename ( compilation , chunkName ) {
21+ const chunk = compilation . namedChunks . get ( chunkName ) ;
2322
2423 if ( ! chunk ) {
2524 throw new Error ( `Cannot find chunk ${ chunkName } ` ) ;
@@ -36,8 +35,21 @@ function getChunkFilename(stats, chunkName) {
3635 return jsFiles [ 0 ] ;
3736}
3837
38+ /**
39+ * @param {Source } asset
40+ * @return {string }
41+ */
42+ function getAssetSource ( asset ) {
43+ let content = asset . source ( ) ;
44+ if ( Buffer . isBuffer ( content ) ) {
45+ content = Buffer . toString ( 'utf-8' ) ;
46+ }
47+
48+ return content ;
49+ }
50+
3951SharedEntryConcatPlugin . prototype . apply = function ( compiler ) {
40- const emit = ( stats ) => {
52+ const emit = ( compilation ) => {
4153 /*
4254 * This is a hack. See ConfigGenerator.buildEntryConfig()
4355 * for other details.
@@ -55,23 +67,26 @@ SharedEntryConcatPlugin.prototype.apply = function(compiler) {
5567 * executed. This fixes that.
5668 */
5769
58- const sharedEntryOutputFile = path . join ( this . buildDir , getChunkFilename ( stats , this . sharedEntryName ) ) ;
59- const tmpEntryBootstrapFile = path . join ( this . buildDir , getChunkFilename ( stats , sharedEntryTmpName ) ) ;
70+ const sharedEntryOutputFile = getChunkFilename ( compilation , this . sharedEntryName ) ;
71+ const tmpEntryFile = getChunkFilename ( compilation , sharedEntryTmpName ) ;
72+ const assets = compilation . assets ;
73+
74+ const sharedEntryAsset = assets [ sharedEntryOutputFile ] ;
75+ const tmpEntryAsset = assets [ tmpEntryFile ] ;
6076
61- if ( ! fs . existsSync ( sharedEntryOutputFile ) ) {
77+ if ( typeof sharedEntryAsset === 'undefined' ) {
6278 throw new Error ( `Could not find shared entry output file: ${ sharedEntryOutputFile } ` ) ;
6379 }
6480
65- if ( ! fs . existsSync ( tmpEntryBootstrapFile ) ) {
66- throw new Error ( `Could not find temporary shared entry bootstrap file: ${ tmpEntryBootstrapFile } ` ) ;
81+ if ( typeof assets [ tmpEntryFile ] === 'undefined' ) {
82+ throw new Error ( `Could not find temporary shared entry bootstrap file: ${ tmpEntryFile } ` ) ;
6783 }
6884
69- fs . writeFileSync (
70- sharedEntryOutputFile ,
71- [ fs . readFileSync ( sharedEntryOutputFile ) , fs . readFileSync ( tmpEntryBootstrapFile ) ] . join ( '\n' )
85+ assets [ sharedEntryOutputFile ] = new RawSource (
86+ [ getAssetSource ( sharedEntryAsset ) , getAssetSource ( tmpEntryAsset ) ] . join ( '\n' )
7287 ) ;
7388
74- fs . unlinkSync ( tmpEntryBootstrapFile ) ;
89+ delete ( assets [ tmpEntryFile ] ) ;
7590 } ;
7691
7792 compiler . hooks . emit . tap (
0 commit comments