@@ -267,7 +267,8 @@ class UglifyJsPlugin {
267267
268268 results . forEach ( ( data , index ) => {
269269 const { file, input, inputSourceMap, commentsFile } = tasks [ index ] ;
270- const { error, map, code, warnings, extractedComments } = data ;
270+ const { error, map, code, warnings } = data ;
271+ let { extractedComments } = data ;
271272
272273 let sourceMap = null ;
273274
@@ -306,44 +307,56 @@ class UglifyJsPlugin {
306307
307308 // Write extracted comments to commentsFile
308309 if ( commentsFile && extractedComments . length > 0 ) {
309- // Add a banner to the original file
310- if ( this . options . extractComments . banner !== false ) {
311- let banner =
312- this . options . extractComments . banner ||
313- `For license information please see ${ path . posix . basename (
314- commentsFile
315- ) } `;
316-
317- if ( typeof banner === 'function' ) {
318- banner = banner ( commentsFile ) ;
319- }
310+ if ( commentsFile in compilation . assets ) {
311+ const commentsFileSource = compilation . assets [
312+ commentsFile
313+ ] . source ( ) ;
320314
321- if ( banner ) {
322- outputSource = new ConcatSource (
323- `/*! ${ banner } */\n` ,
324- outputSource
325- ) ;
326- }
315+ extractedComments = extractedComments . filter (
316+ ( comment ) => ! commentsFileSource . includes ( comment )
317+ ) ;
327318 }
328319
329- const commentsSource = new RawSource (
330- `${ extractedComments . join ( '\n\n' ) } \n`
331- ) ;
320+ if ( extractedComments . length > 0 ) {
321+ // Add a banner to the original file
322+ if ( this . options . extractComments . banner !== false ) {
323+ let banner =
324+ this . options . extractComments . banner ||
325+ `For license information please see ${ path . posix . basename (
326+ commentsFile
327+ ) } `;
328+
329+ if ( typeof banner === 'function' ) {
330+ banner = banner ( commentsFile ) ;
331+ }
332+
333+ if ( banner ) {
334+ outputSource = new ConcatSource (
335+ `/*! ${ banner } */\n` ,
336+ outputSource
337+ ) ;
338+ }
339+ }
332340
333- if ( commentsFile in compilation . assets ) {
334- // commentsFile already exists, append new comments...
335- if ( compilation . assets [ commentsFile ] instanceof ConcatSource ) {
336- compilation . assets [ commentsFile ] . add ( '\n' ) ;
337- compilation . assets [ commentsFile ] . add ( commentsSource ) ;
341+ const commentsSource = new RawSource (
342+ `${ extractedComments . join ( '\n\n' ) } \n`
343+ ) ;
344+
345+ if ( commentsFile in compilation . assets ) {
346+ // commentsFile already exists, append new comments...
347+ if ( compilation . assets [ commentsFile ] instanceof ConcatSource ) {
348+ compilation . assets [ commentsFile ] . add ( '\n' ) ;
349+ compilation . assets [ commentsFile ] . add ( commentsSource ) ;
350+ } else {
351+ compilation . assets [ commentsFile ] = new ConcatSource (
352+ compilation . assets [ commentsFile ] ,
353+ '\n' ,
354+ commentsSource
355+ ) ;
356+ }
338357 } else {
339- compilation . assets [ commentsFile ] = new ConcatSource (
340- compilation . assets [ commentsFile ] ,
341- '\n' ,
342- commentsSource
343- ) ;
358+ compilation . assets [ commentsFile ] = commentsSource ;
344359 }
345- } else {
346- compilation . assets [ commentsFile ] = commentsSource ;
347360 }
348361 }
349362
0 commit comments