@@ -37,6 +37,26 @@ function addDependencies(compilation: any, scripts: string[]): void {
37
37
}
38
38
}
39
39
40
+ function hook ( compiler : any , action : ( compilation : any , callback : ( err ?: Error ) => void ) => void ) {
41
+ if ( compiler . hooks ) {
42
+ // Webpack 4
43
+ compiler . hooks . thisCompilation . tap ( 'scripts-webpack-plugin' , ( compilation : any ) => {
44
+ compilation . hooks . additionalAssets . tapAsync (
45
+ 'scripts-webpack-plugin' ,
46
+ ( callback : ( err ?: Error ) => void ) => action ( compilation , callback ) ,
47
+ ) ;
48
+ } ) ;
49
+ } else {
50
+ // Webpack 3
51
+ compiler . plugin ( 'this-compilation' , ( compilation : any ) => {
52
+ compilation . plugin (
53
+ 'additional-assets' ,
54
+ ( callback : ( err ?: Error ) => void ) => action ( compilation , callback ) ,
55
+ ) ;
56
+ } ) ;
57
+ }
58
+ }
59
+
40
60
export class ScriptsWebpackPlugin {
41
61
private _lastBuildTime ?: number ;
42
62
private _cachedOutput ?: ScriptOutput ;
@@ -89,71 +109,69 @@ export class ScriptsWebpackPlugin {
89
109
. filter ( script => ! ! script )
90
110
. map ( script => path . resolve ( this . options . basePath || '' , script ) ) ;
91
111
92
- compiler . plugin ( 'this-compilation' , ( compilation : any ) => {
93
- compilation . plugin ( 'additional-assets' , ( callback : ( err ?: Error ) => void ) => {
94
- if ( this . shouldSkip ( compilation , scripts ) ) {
95
- if ( this . _cachedOutput ) {
96
- this . _insertOutput ( compilation , this . _cachedOutput , true ) ;
97
- }
112
+ hook ( compiler , ( compilation , callback ) => {
113
+ if ( this . shouldSkip ( compilation , scripts ) ) {
114
+ if ( this . _cachedOutput ) {
115
+ this . _insertOutput ( compilation , this . _cachedOutput , true ) ;
116
+ }
98
117
99
- addDependencies ( compilation , scripts ) ;
100
- callback ( ) ;
118
+ addDependencies ( compilation , scripts ) ;
119
+ callback ( ) ;
101
120
102
- return ;
103
- }
121
+ return ;
122
+ }
104
123
105
- const sourceGetters = scripts . map ( fullPath => {
106
- return new Promise < Source > ( ( resolve , reject ) => {
107
- compilation . inputFileSystem . readFile ( fullPath , ( err : Error , data : Buffer ) => {
108
- if ( err ) {
109
- reject ( err ) ;
110
- return ;
111
- }
124
+ const sourceGetters = scripts . map ( fullPath => {
125
+ return new Promise < Source > ( ( resolve , reject ) => {
126
+ compilation . inputFileSystem . readFile ( fullPath , ( err : Error , data : Buffer ) => {
127
+ if ( err ) {
128
+ reject ( err ) ;
129
+ return ;
130
+ }
112
131
113
- const content = data . toString ( ) ;
132
+ const content = data . toString ( ) ;
114
133
115
- let source ;
116
- if ( this . options . sourceMap ) {
117
- // TODO: Look for source map file (for '.min' scripts, etc.)
134
+ let source ;
135
+ if ( this . options . sourceMap ) {
136
+ // TODO: Look for source map file (for '.min' scripts, etc.)
118
137
119
- let adjustedPath = fullPath ;
120
- if ( this . options . basePath ) {
121
- adjustedPath = path . relative ( this . options . basePath , fullPath ) ;
122
- }
123
- source = new OriginalSource ( content , adjustedPath ) ;
124
- } else {
125
- source = new RawSource ( content ) ;
138
+ let adjustedPath = fullPath ;
139
+ if ( this . options . basePath ) {
140
+ adjustedPath = path . relative ( this . options . basePath , fullPath ) ;
126
141
}
142
+ source = new OriginalSource ( content , adjustedPath ) ;
143
+ } else {
144
+ source = new RawSource ( content ) ;
145
+ }
127
146
128
- resolve ( source ) ;
129
- } ) ;
147
+ resolve ( source ) ;
130
148
} ) ;
131
149
} ) ;
132
-
133
- Promise . all ( sourceGetters )
134
- . then ( sources => {
135
- const concatSource = new ConcatSource ( ) ;
136
- sources . forEach ( source => {
137
- concatSource . add ( source ) ;
138
- concatSource . add ( '\n;' ) ;
139
- } ) ;
140
-
141
- const combinedSource = new CachedSource ( concatSource ) ;
142
- const filename = interpolateName (
143
- { resourcePath : 'scripts.js' } as loader . LoaderContext ,
144
- this . options . filename ,
145
- { content : combinedSource . source ( ) } ,
146
- ) ;
147
-
148
- const output = { filename, source : combinedSource } ;
149
- this . _insertOutput ( compilation , output ) ;
150
- this . _cachedOutput = output ;
151
- addDependencies ( compilation , scripts ) ;
152
-
153
- callback ( ) ;
154
- } )
155
- . catch ( ( err : Error ) => callback ( err ) ) ;
156
150
} ) ;
151
+
152
+ Promise . all ( sourceGetters )
153
+ . then ( sources => {
154
+ const concatSource = new ConcatSource ( ) ;
155
+ sources . forEach ( source => {
156
+ concatSource . add ( source ) ;
157
+ concatSource . add ( '\n;' ) ;
158
+ } ) ;
159
+
160
+ const combinedSource = new CachedSource ( concatSource ) ;
161
+ const filename = interpolateName (
162
+ { resourcePath : 'scripts.js' } as loader . LoaderContext ,
163
+ this . options . filename ,
164
+ { content : combinedSource . source ( ) } ,
165
+ ) ;
166
+
167
+ const output = { filename, source : combinedSource } ;
168
+ this . _insertOutput ( compilation , output ) ;
169
+ this . _cachedOutput = output ;
170
+ addDependencies ( compilation , scripts ) ;
171
+
172
+ callback ( ) ;
173
+ } )
174
+ . catch ( ( err : Error ) => callback ( err ) ) ;
157
175
} ) ;
158
176
}
159
177
}
0 commit comments