Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit beaf1ad

Browse files
fix: dedupe extracted comments (#383)
1 parent 1e58c99 commit beaf1ad

File tree

6 files changed

+801
-1333
lines changed

6 files changed

+801
-1333
lines changed

src/index.js

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/minify.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,21 @@ const buildComments = (options, uglifyOptions, extractedComments) => {
115115
}
116116
});
117117

118+
// Redefine the comments function to extract and preserve
119+
// comments according to the two conditions
118120
// Redefine the comments function to extract and preserve
119121
// comments according to the two conditions
120122
return (astNode, comment) => {
121123
if (condition.extract(astNode, comment)) {
122-
extractedComments.push(
124+
const commentText =
123125
comment.type === 'comment2'
124126
? `/*${comment.value}*/`
125-
: `//${comment.value}`
126-
);
127+
: `//${comment.value}`;
128+
129+
// Don't include duplicate comments
130+
if (!extractedComments.includes(commentText)) {
131+
extractedComments.push(commentText);
132+
}
127133
}
128134

129135
return condition.preserve(astNode, comment);

0 commit comments

Comments
 (0)