9
9
10
10
'use strict' ;
11
11
12
- const fs = require ( 'fs' ) ;
13
- const path = require ( 'path' ) ;
14
12
const sharedEntryTmpName = require ( '../utils/sharedEntryTmpName' ) ;
13
+ const RawSource = require ( 'webpack-sources/lib/RawSource' ) ;
15
14
16
- function SharedEntryConcatPlugin ( sharedEntryName , buildDir ) {
15
+ function SharedEntryConcatPlugin ( sharedEntryName ) {
17
16
this . sharedEntryName = sharedEntryName ;
18
- this . buildDir = buildDir ;
19
17
}
20
18
21
- function getChunkFilename ( stats , chunkName ) {
22
- const chunk = stats . compilation . namedChunks . get ( chunkName ) ;
19
+ function getChunkFilename ( compilation , chunkName ) {
20
+ const chunk = compilation . namedChunks . get ( chunkName ) ;
23
21
24
22
if ( ! chunk ) {
25
23
throw new Error ( `Cannot find chunk ${ chunkName } ` ) ;
@@ -36,12 +34,21 @@ function getChunkFilename(stats, chunkName) {
36
34
return jsFiles [ 0 ] ;
37
35
}
38
36
39
- SharedEntryConcatPlugin . prototype . apply = function ( compiler ) {
40
- const done = ( stats ) => {
41
- if ( stats . hasErrors ( ) ) {
42
- return ;
43
- }
37
+ /**
38
+ * @param {Source } asset
39
+ * @return {string }
40
+ */
41
+ function getAssetSource ( asset ) {
42
+ let content = asset . source ( ) ;
43
+ if ( Buffer . isBuffer ( content ) ) {
44
+ content = Buffer . toString ( 'utf-8' ) ;
45
+ }
44
46
47
+ return content ;
48
+ }
49
+
50
+ SharedEntryConcatPlugin . prototype . apply = function ( compiler ) {
51
+ const emit = ( compilation ) => {
45
52
/*
46
53
* This is a hack. See ConfigGenerator.buildEntryConfig()
47
54
* for other details.
@@ -59,28 +66,31 @@ SharedEntryConcatPlugin.prototype.apply = function(compiler) {
59
66
* executed. This fixes that.
60
67
*/
61
68
62
- const sharedEntryOutputFile = path . join ( this . buildDir , getChunkFilename ( stats , this . sharedEntryName ) ) ;
63
- const tmpEntryBootstrapFile = path . join ( this . buildDir , getChunkFilename ( stats , sharedEntryTmpName ) ) ;
69
+ const sharedEntryOutputFile = getChunkFilename ( compilation , this . sharedEntryName ) ;
70
+ const tmpEntryFile = getChunkFilename ( compilation , sharedEntryTmpName ) ;
71
+ const assets = compilation . assets ;
72
+
73
+ const sharedEntryAsset = assets [ sharedEntryOutputFile ] ;
74
+ const tmpEntryAsset = assets [ tmpEntryFile ] ;
64
75
65
- if ( ! fs . existsSync ( sharedEntryOutputFile ) ) {
76
+ if ( typeof sharedEntryAsset === 'undefined' ) {
66
77
throw new Error ( `Could not find shared entry output file: ${ sharedEntryOutputFile } ` ) ;
67
78
}
68
79
69
- if ( ! fs . existsSync ( tmpEntryBootstrapFile ) ) {
70
- throw new Error ( `Could not find temporary shared entry bootstrap file: ${ tmpEntryBootstrapFile } ` ) ;
80
+ if ( typeof assets [ tmpEntryFile ] === 'undefined' ) {
81
+ throw new Error ( `Could not find temporary shared entry bootstrap file: ${ tmpEntryFile } ` ) ;
71
82
}
72
83
73
- fs . writeFileSync (
74
- sharedEntryOutputFile ,
75
- [ fs . readFileSync ( sharedEntryOutputFile ) , fs . readFileSync ( tmpEntryBootstrapFile ) ] . join ( '\n' )
84
+ assets [ sharedEntryOutputFile ] = new RawSource (
85
+ [ getAssetSource ( sharedEntryAsset ) , getAssetSource ( tmpEntryAsset ) ] . join ( '\n' )
76
86
) ;
77
87
78
- fs . unlinkSync ( tmpEntryBootstrapFile ) ;
88
+ delete ( assets [ tmpEntryFile ] ) ;
79
89
} ;
80
90
81
- compiler . hooks . done . tap (
91
+ compiler . hooks . emit . tap (
82
92
{ name : 'SharedEntryConcatPlugin' } ,
83
- done
93
+ emit
84
94
) ;
85
95
} ;
86
96
0 commit comments