10
10
'use strict' ;
11
11
12
12
const fs = require ( 'fs' ) ;
13
- const path = require ( 'path' ) ;
14
13
const sharedEntryTmpName = require ( '../utils/sharedEntryTmpName' ) ;
14
+ const RawSource = require ( 'webpack-sources/lib/RawSource' ) ;
15
15
16
- function SharedEntryConcatPlugin ( sharedEntryName , buildDir ) {
16
+ function SharedEntryConcatPlugin ( sharedEntryName ) {
17
17
this . sharedEntryName = sharedEntryName ;
18
- this . buildDir = buildDir ;
19
18
}
20
19
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 ) ;
23
22
24
23
if ( ! chunk ) {
25
24
throw new Error ( `Cannot find chunk ${ chunkName } ` ) ;
@@ -36,8 +35,21 @@ function getChunkFilename(stats, chunkName) {
36
35
return jsFiles [ 0 ] ;
37
36
}
38
37
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
+
39
51
SharedEntryConcatPlugin . prototype . apply = function ( compiler ) {
40
- const emit = ( stats ) => {
52
+ const emit = ( compilation ) => {
41
53
/*
42
54
* This is a hack. See ConfigGenerator.buildEntryConfig()
43
55
* for other details.
@@ -55,23 +67,26 @@ SharedEntryConcatPlugin.prototype.apply = function(compiler) {
55
67
* executed. This fixes that.
56
68
*/
57
69
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 ] ;
60
76
61
- if ( ! fs . existsSync ( sharedEntryOutputFile ) ) {
77
+ if ( typeof sharedEntryAsset === 'undefined' ) {
62
78
throw new Error ( `Could not find shared entry output file: ${ sharedEntryOutputFile } ` ) ;
63
79
}
64
80
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 } ` ) ;
67
83
}
68
84
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' )
72
87
) ;
73
88
74
- fs . unlinkSync ( tmpEntryBootstrapFile ) ;
89
+ delete ( assets [ tmpEntryFile ] ) ;
75
90
} ;
76
91
77
92
compiler . hooks . emit . tap (
0 commit comments