@@ -154,6 +154,28 @@ class TerserPlugin {
154
154
return `Terser Plugin: ${ warningMessage } ${ locationMessage } ` ;
155
155
}
156
156
157
+ static removeQueryString ( filename ) {
158
+ let targetFilename = filename ;
159
+
160
+ const queryStringIdx = targetFilename . indexOf ( '?' ) ;
161
+
162
+ if ( queryStringIdx >= 0 ) {
163
+ targetFilename = targetFilename . substr ( 0 , queryStringIdx ) ;
164
+ }
165
+
166
+ return targetFilename ;
167
+ }
168
+
169
+ static hasAsset ( commentFilename , assets ) {
170
+ const assetFilenames = Object . keys ( assets ) . map ( ( assetFilename ) =>
171
+ TerserPlugin . removeQueryString ( assetFilename )
172
+ ) ;
173
+
174
+ return assetFilenames . includes (
175
+ TerserPlugin . removeQueryString ( commentFilename )
176
+ ) ;
177
+ }
178
+
157
179
apply ( compiler ) {
158
180
this . options . sourceMap =
159
181
typeof this . options . sourceMap === 'undefined'
@@ -212,16 +234,16 @@ class TerserPlugin {
212
234
}
213
235
214
236
// Handling comment extraction
215
- let commentsFile = false ;
237
+ let commentsFilename = false ;
216
238
217
239
if ( this . options . extractComments ) {
218
- commentsFile =
240
+ commentsFilename =
219
241
this . options . extractComments . filename ||
220
242
'[file].LICENSE[query]' ;
221
243
222
244
// Todo remove this in next major release
223
- if ( typeof commentsFile === 'function' ) {
224
- commentsFile = commentsFile . bind ( null , file ) ;
245
+ if ( typeof commentsFilename === 'function' ) {
246
+ commentsFilename = commentsFilename . bind ( null , file ) ;
225
247
}
226
248
227
249
let query = '' ;
@@ -243,14 +265,28 @@ class TerserPlugin {
243
265
244
266
const data = { filename, basename, query } ;
245
267
246
- commentsFile = compilation . getPath ( commentsFile , data ) ;
268
+ commentsFilename = compilation . getPath ( commentsFilename , data ) ;
269
+ }
270
+
271
+ if (
272
+ commentsFilename &&
273
+ TerserPlugin . hasAsset ( commentsFilename , compilation . assets )
274
+ ) {
275
+ // Todo make error and stop uglifing in next major release
276
+ compilation . warnings . push (
277
+ new Error (
278
+ `The comment file "${ TerserPlugin . removeQueryString (
279
+ commentsFilename
280
+ ) } " conflicts with an existing asset, this may lead to code corruption, please use a different name`
281
+ )
282
+ ) ;
247
283
}
248
284
249
285
const task = {
250
286
file,
251
287
input,
252
288
inputSourceMap,
253
- commentsFile ,
289
+ commentsFilename ,
254
290
extractComments : this . options . extractComments ,
255
291
terserOptions : this . options . terserOptions ,
256
292
minify : this . options . minify ,
@@ -299,7 +335,7 @@ class TerserPlugin {
299
335
await taskRunner . exit ( ) ;
300
336
301
337
completedTasks . forEach ( ( completedTask , index ) => {
302
- const { file, input, inputSourceMap, commentsFile } = tasks [ index ] ;
338
+ const { file, input, inputSourceMap, commentsFilename } = tasks [ index ] ;
303
339
const { error, map, code, warnings } = completedTask ;
304
340
let { extractedComments } = completedTask ;
305
341
@@ -339,11 +375,15 @@ class TerserPlugin {
339
375
outputSource = new RawSource ( code ) ;
340
376
}
341
377
342
- // Write extracted comments to commentsFile
343
- if ( commentsFile && extractedComments && extractedComments . length > 0 ) {
344
- if ( commentsFile in compilation . assets ) {
378
+ // Write extracted comments to commentsFilename
379
+ if (
380
+ commentsFilename &&
381
+ extractedComments &&
382
+ extractedComments . length > 0
383
+ ) {
384
+ if ( commentsFilename in compilation . assets ) {
345
385
const commentsFileSource = compilation . assets [
346
- commentsFile
386
+ commentsFilename
347
387
] . source ( ) ;
348
388
349
389
extractedComments = extractedComments . filter (
@@ -357,11 +397,11 @@ class TerserPlugin {
357
397
let banner =
358
398
this . options . extractComments . banner ||
359
399
`For license information please see ${ path . posix . basename (
360
- commentsFile
400
+ commentsFilename
361
401
) } `;
362
402
363
403
if ( typeof banner === 'function' ) {
364
- banner = banner ( commentsFile ) ;
404
+ banner = banner ( commentsFilename ) ;
365
405
}
366
406
367
407
if ( banner ) {
@@ -376,22 +416,24 @@ class TerserPlugin {
376
416
`${ extractedComments . join ( '\n\n' ) } \n`
377
417
) ;
378
418
379
- if ( commentsFile in compilation . assets ) {
419
+ if ( commentsFilename in compilation . assets ) {
380
420
// commentsFile already exists, append new comments...
381
- if ( compilation . assets [ commentsFile ] instanceof ConcatSource ) {
382
- compilation . assets [ commentsFile ] . add ( '\n' ) ;
383
- compilation . assets [ commentsFile ] . add ( commentsSource ) ;
421
+ if (
422
+ compilation . assets [ commentsFilename ] instanceof ConcatSource
423
+ ) {
424
+ compilation . assets [ commentsFilename ] . add ( '\n' ) ;
425
+ compilation . assets [ commentsFilename ] . add ( commentsSource ) ;
384
426
} else {
385
427
// eslint-disable-next-line no-param-reassign
386
- compilation . assets [ commentsFile ] = new ConcatSource (
387
- compilation . assets [ commentsFile ] ,
428
+ compilation . assets [ commentsFilename ] = new ConcatSource (
429
+ compilation . assets [ commentsFilename ] ,
388
430
'\n' ,
389
431
commentsSource
390
432
) ;
391
433
}
392
434
} else {
393
435
// eslint-disable-next-line no-param-reassign
394
- compilation . assets [ commentsFile ] = commentsSource ;
436
+ compilation . assets [ commentsFilename ] = commentsSource ;
395
437
}
396
438
}
397
439
}
0 commit comments