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

fix: dedupe extracted comments #383

Merged
merged 1 commit into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 46 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading