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

Commit

Permalink
fix: dedupe extracted comments (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Dec 18, 2018
1 parent 1e58c99 commit beaf1ad
Show file tree
Hide file tree
Showing 6 changed files with 801 additions and 1,333 deletions.
79 changes: 46 additions & 33 deletions src/index.js
Expand Up @@ -267,7 +267,8 @@ class UglifyJsPlugin {

results.forEach((data, index) => {
const { file, input, inputSourceMap, commentsFile } = tasks[index];
const { error, map, code, warnings, extractedComments } = data;
const { error, map, code, warnings } = data;
let { extractedComments } = data;

let sourceMap = null;

Expand Down Expand Up @@ -306,44 +307,56 @@ class UglifyJsPlugin {

// Write extracted comments to commentsFile
if (commentsFile && extractedComments.length > 0) {
// Add a banner to the original file
if (this.options.extractComments.banner !== false) {
let banner =
this.options.extractComments.banner ||
`For license information please see ${path.posix.basename(
commentsFile
)}`;

if (typeof banner === 'function') {
banner = banner(commentsFile);
}
if (commentsFile in compilation.assets) {
const commentsFileSource = compilation.assets[
commentsFile
].source();

if (banner) {
outputSource = new ConcatSource(
`/*! ${banner} */\n`,
outputSource
);
}
extractedComments = extractedComments.filter(
(comment) => !commentsFileSource.includes(comment)
);
}

const commentsSource = new RawSource(
`${extractedComments.join('\n\n')}\n`
);
if (extractedComments.length > 0) {
// Add a banner to the original file
if (this.options.extractComments.banner !== false) {
let banner =
this.options.extractComments.banner ||
`For license information please see ${path.posix.basename(
commentsFile
)}`;

if (typeof banner === 'function') {
banner = banner(commentsFile);
}

if (banner) {
outputSource = new ConcatSource(
`/*! ${banner} */\n`,
outputSource
);
}
}

if (commentsFile in compilation.assets) {
// commentsFile already exists, append new comments...
if (compilation.assets[commentsFile] instanceof ConcatSource) {
compilation.assets[commentsFile].add('\n');
compilation.assets[commentsFile].add(commentsSource);
const commentsSource = new RawSource(
`${extractedComments.join('\n\n')}\n`
);

if (commentsFile in compilation.assets) {
// commentsFile already exists, append new comments...
if (compilation.assets[commentsFile] instanceof ConcatSource) {
compilation.assets[commentsFile].add('\n');
compilation.assets[commentsFile].add(commentsSource);
} else {
compilation.assets[commentsFile] = new ConcatSource(
compilation.assets[commentsFile],
'\n',
commentsSource
);
}
} else {
compilation.assets[commentsFile] = new ConcatSource(
compilation.assets[commentsFile],
'\n',
commentsSource
);
compilation.assets[commentsFile] = commentsSource;
}
} else {
compilation.assets[commentsFile] = commentsSource;
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/minify.js
Expand Up @@ -115,15 +115,21 @@ const buildComments = (options, uglifyOptions, extractedComments) => {
}
});

// Redefine the comments function to extract and preserve
// comments according to the two conditions
// Redefine the comments function to extract and preserve
// comments according to the two conditions
return (astNode, comment) => {
if (condition.extract(astNode, comment)) {
extractedComments.push(
const commentText =
comment.type === 'comment2'
? `/*${comment.value}*/`
: `//${comment.value}`
);
: `//${comment.value}`;

// Don't include duplicate comments
if (!extractedComments.includes(commentText)) {
extractedComments.push(commentText);
}
}

return condition.preserve(astNode, comment);
Expand Down

0 comments on commit beaf1ad

Please sign in to comment.