@@ -95,154 +95,170 @@ class UglifyJsPlugin {
95
95
apply ( compiler ) {
96
96
const requestShortener = new RequestShortener ( compiler . context ) ;
97
97
98
- compiler . plugin ( 'compilation' , ( compilation ) => {
99
- if ( this . options . sourceMap ) {
100
- compilation . plugin ( 'build-module' , ( moduleArg ) => {
101
- // to get detailed location info about errors
102
- moduleArg . useSourceMap = true ;
103
- } ) ;
104
- }
98
+ const buildMobuleFn = ( moduleArg ) => {
99
+ // to get detailed location info about errors
100
+ moduleArg . useSourceMap = true ;
101
+ } ;
105
102
106
- compilation . plugin ( 'optimize-chunk-assets' , ( chunks , callback ) => {
107
- const uglify = new Uglify ( {
108
- cache : this . options . cache ,
109
- parallel : this . options . parallel ,
110
- } ) ;
111
- const uglifiedAssets = new WeakSet ( ) ;
112
- const tasks = [ ] ;
113
- chunks . reduce ( ( acc , chunk ) => acc . concat ( chunk . files || [ ] ) , [ ] )
114
- . concat ( compilation . additionalChunkAssets || [ ] )
115
- . filter ( ModuleFilenameHelpers . matchObject . bind ( null , this . options ) )
116
- . forEach ( ( file ) => {
117
- let sourceMap ;
118
- const asset = compilation . assets [ file ] ;
119
- if ( uglifiedAssets . has ( asset ) ) {
120
- return ;
121
- }
103
+ const optimizeFn = ( compilation , chunks , callback ) => {
104
+ const uglify = new Uglify ( {
105
+ cache : this . options . cache ,
106
+ parallel : this . options . parallel ,
107
+ } ) ;
108
+ const uglifiedAssets = new WeakSet ( ) ;
109
+ const tasks = [ ] ;
110
+ chunks . reduce ( ( acc , chunk ) => acc . concat ( chunk . files || [ ] ) , [ ] )
111
+ . concat ( compilation . additionalChunkAssets || [ ] )
112
+ . filter ( ModuleFilenameHelpers . matchObject . bind ( null , this . options ) )
113
+ . forEach ( ( file ) => {
114
+ let sourceMap ;
115
+ const asset = compilation . assets [ file ] ;
116
+ if ( uglifiedAssets . has ( asset ) ) {
117
+ return ;
118
+ }
122
119
123
- try {
124
- let input ;
125
- let inputSourceMap ;
120
+ try {
121
+ let input ;
122
+ let inputSourceMap ;
126
123
127
- if ( this . options . sourceMap && asset . sourceAndMap ) {
128
- const { source, map } = asset . sourceAndMap ( ) ;
124
+ if ( this . options . sourceMap && asset . sourceAndMap ) {
125
+ const { source, map } = asset . sourceAndMap ( ) ;
129
126
130
- input = source ;
131
- inputSourceMap = map ;
127
+ input = source ;
128
+ inputSourceMap = map ;
132
129
133
- sourceMap = new SourceMapConsumer ( inputSourceMap ) ;
134
- } else {
135
- input = asset . source ( ) ;
136
- inputSourceMap = null ;
137
- }
130
+ sourceMap = new SourceMapConsumer ( inputSourceMap ) ;
131
+ } else {
132
+ input = asset . source ( ) ;
133
+ inputSourceMap = null ;
134
+ }
138
135
139
- // Handling comment extraction
140
- let commentsFile = false ;
141
- if ( this . options . extractComments ) {
142
- commentsFile = this . options . extractComments . filename || `${ file } .LICENSE` ;
143
- if ( typeof commentsFile === 'function' ) {
144
- commentsFile = commentsFile ( file ) ;
145
- }
136
+ // Handling comment extraction
137
+ let commentsFile = false ;
138
+ if ( this . options . extractComments ) {
139
+ commentsFile = this . options . extractComments . filename || `${ file } .LICENSE` ;
140
+ if ( typeof commentsFile === 'function' ) {
141
+ commentsFile = commentsFile ( file ) ;
146
142
}
143
+ }
147
144
148
- const task = {
149
- file,
150
- input,
151
- sourceMap,
152
- inputSourceMap,
153
- commentsFile,
154
- extractComments : this . options . extractComments ,
155
- uglifyOptions : this . options . uglifyOptions ,
156
- } ;
157
-
158
- if ( this . options . cache ) {
159
- task . cacheKey = serialize ( {
160
- 'uglify-es' : versions . uglify ,
161
- 'uglifyjs-webpack-plugin' : versions . plugin ,
162
- 'uglifyjs-webpack-plugin-options' : this . options ,
163
- path : compiler . outputPath ? `${ compiler . outputPath } /${ file } ` : file ,
164
- hash : crypto . createHash ( 'md5' ) . update ( input ) . digest ( 'hex' ) ,
165
- } ) ;
166
- }
145
+ const task = {
146
+ file,
147
+ input,
148
+ sourceMap,
149
+ inputSourceMap,
150
+ commentsFile,
151
+ extractComments : this . options . extractComments ,
152
+ uglifyOptions : this . options . uglifyOptions ,
153
+ } ;
167
154
168
- tasks . push ( task ) ;
169
- } catch ( error ) {
170
- compilation . errors . push ( UglifyJsPlugin . buildError ( error , file , sourceMap , requestShortener ) ) ;
155
+ if ( this . options . cache ) {
156
+ task . cacheKey = serialize ( {
157
+ 'uglify-es' : versions . uglify ,
158
+ 'uglifyjs-webpack-plugin' : versions . plugin ,
159
+ 'uglifyjs-webpack-plugin-options' : this . options ,
160
+ path : compiler . outputPath ? `${ compiler . outputPath } /${ file } ` : file ,
161
+ hash : crypto . createHash ( 'md5' ) . update ( input ) . digest ( 'hex' ) ,
162
+ } ) ;
171
163
}
172
- } ) ;
173
164
174
- uglify . runTasks ( tasks , ( tasksError , results ) => {
175
- if ( tasksError ) {
176
- compilation . errors . push ( tasksError ) ;
177
- return ;
165
+ tasks . push ( task ) ;
166
+ } catch ( error ) {
167
+ compilation . errors . push ( UglifyJsPlugin . buildError ( error , file , sourceMap , requestShortener ) ) ;
178
168
}
169
+ } ) ;
179
170
180
- results . forEach ( ( data , index ) => {
181
- const { file, input, sourceMap, inputSourceMap, commentsFile } = tasks [ index ] ;
182
- const { error, map, code, warnings, extractedComments } = data ;
171
+ uglify . runTasks ( tasks , ( tasksError , results ) => {
172
+ if ( tasksError ) {
173
+ compilation . errors . push ( tasksError ) ;
174
+ return ;
175
+ }
183
176
184
- // Handling results
185
- // Error case: add errors, and go to next file
186
- if ( error ) {
187
- compilation . errors . push ( UglifyJsPlugin . buildError ( error , file , sourceMap , requestShortener ) ) ;
188
- return ;
189
- }
177
+ results . forEach ( ( data , index ) => {
178
+ const { file, input, sourceMap, inputSourceMap, commentsFile } = tasks [ index ] ;
179
+ const { error, map, code, warnings, extractedComments } = data ;
190
180
191
- let outputSource ;
192
- if ( map ) {
193
- outputSource = new SourceMapSource ( code , file , JSON . parse ( map ) , input , inputSourceMap ) ;
194
- } else {
195
- outputSource = new RawSource ( code ) ;
196
- }
181
+ // Handling results
182
+ // Error case: add errors, and go to next file
183
+ if ( error ) {
184
+ compilation . errors . push ( UglifyJsPlugin . buildError ( error , file , sourceMap , requestShortener ) ) ;
185
+ return ;
186
+ }
187
+
188
+ let outputSource ;
189
+ if ( map ) {
190
+ outputSource = new SourceMapSource ( code , file , JSON . parse ( map ) , input , inputSourceMap ) ;
191
+ } else {
192
+ outputSource = new RawSource ( code ) ;
193
+ }
197
194
198
- // Write extracted comments to commentsFile
199
- if ( commentsFile && extractedComments . length > 0 ) {
200
- // Add a banner to the original file
201
- if ( this . options . extractComments . banner !== false ) {
202
- let banner = this . options . extractComments . banner || `For license information please see ${ commentsFile } ` ;
203
- if ( typeof banner === 'function' ) {
204
- banner = banner ( commentsFile ) ;
205
- }
206
- if ( banner ) {
207
- outputSource = new ConcatSource (
208
- `/*! ${ banner } */\n` , outputSource ,
209
- ) ;
210
- }
195
+ // Write extracted comments to commentsFile
196
+ if ( commentsFile && extractedComments . length > 0 ) {
197
+ // Add a banner to the original file
198
+ if ( this . options . extractComments . banner !== false ) {
199
+ let banner = this . options . extractComments . banner || `For license information please see ${ commentsFile } ` ;
200
+ if ( typeof banner === 'function' ) {
201
+ banner = banner ( commentsFile ) ;
211
202
}
203
+ if ( banner ) {
204
+ outputSource = new ConcatSource (
205
+ `/*! ${ banner } */\n` , outputSource ,
206
+ ) ;
207
+ }
208
+ }
212
209
213
- const commentsSource = new RawSource ( `${ extractedComments . join ( '\n\n' ) } \n` ) ;
214
- if ( commentsFile in compilation . assets ) {
215
- // commentsFile already exists, append new comments...
216
- if ( compilation . assets [ commentsFile ] instanceof ConcatSource ) {
217
- compilation . assets [ commentsFile ] . add ( '\n' ) ;
218
- compilation . assets [ commentsFile ] . add ( commentsSource ) ;
219
- } else {
220
- compilation . assets [ commentsFile ] = new ConcatSource (
221
- compilation . assets [ commentsFile ] , '\n' , commentsSource ,
222
- ) ;
223
- }
210
+ const commentsSource = new RawSource ( `${ extractedComments . join ( '\n\n' ) } \n` ) ;
211
+ if ( commentsFile in compilation . assets ) {
212
+ // commentsFile already exists, append new comments...
213
+ if ( compilation . assets [ commentsFile ] instanceof ConcatSource ) {
214
+ compilation . assets [ commentsFile ] . add ( '\n' ) ;
215
+ compilation . assets [ commentsFile ] . add ( commentsSource ) ;
224
216
} else {
225
- compilation . assets [ commentsFile ] = commentsSource ;
217
+ compilation . assets [ commentsFile ] = new ConcatSource (
218
+ compilation . assets [ commentsFile ] , '\n' , commentsSource ,
219
+ ) ;
226
220
}
221
+ } else {
222
+ compilation . assets [ commentsFile ] = commentsSource ;
227
223
}
224
+ }
228
225
229
- // Updating assets
230
- uglifiedAssets . add ( compilation . assets [ file ] = outputSource ) ;
226
+ // Updating assets
227
+ uglifiedAssets . add ( compilation . assets [ file ] = outputSource ) ;
231
228
232
- // Handling warnings
233
- if ( warnings ) {
234
- const warnArr = UglifyJsPlugin . buildWarnings ( warnings , file , sourceMap , this . options . warningsFilter , requestShortener ) ;
235
- if ( warnArr . length > 0 ) {
236
- compilation . warnings . push ( new Error ( `${ file } from UglifyJs\n${ warnArr . join ( '\n' ) } ` ) ) ;
237
- }
229
+ // Handling warnings
230
+ if ( warnings ) {
231
+ const warnArr = UglifyJsPlugin . buildWarnings ( warnings , file , sourceMap , this . options . warningsFilter , requestShortener ) ;
232
+ if ( warnArr . length > 0 ) {
233
+ compilation . warnings . push ( new Error ( `${ file } from UglifyJs\n${ warnArr . join ( '\n' ) } ` ) ) ;
238
234
}
239
- } ) ;
240
-
241
- uglify . exit ( ) ;
242
- callback ( ) ;
235
+ }
243
236
} ) ;
237
+
238
+ uglify . exit ( ) ;
239
+ callback ( ) ;
244
240
} ) ;
245
- } ) ;
241
+ } ;
242
+
243
+ if ( compiler . hooks ) {
244
+ const plugin = { name : 'UglifyJSPlugin' } ;
245
+
246
+ compiler . hooks . compilation . tap ( plugin , ( compilation ) => {
247
+ if ( this . options . sourceMap ) {
248
+ compilation . hooks . buildMobule . tap ( plugin , buildMobuleFn ) ;
249
+ }
250
+
251
+ compilation . hooks . optimizeChunkAssets . tapAsync ( plugin , optimizeFn . bind ( this , compilation ) ) ;
252
+ } ) ;
253
+ } else {
254
+ compiler . plugin ( 'compilation' , ( compilation ) => {
255
+ if ( this . options . sourceMap ) {
256
+ compilation . plugin ( 'build-module' , buildMobuleFn ) ;
257
+ }
258
+
259
+ compilation . plugin ( 'optimize-chunk-assets' , optimizeFn . bind ( this , compilation ) ) ;
260
+ } ) ;
261
+ }
246
262
}
247
263
}
248
264
0 commit comments