Skip to content

Commit

Permalink
refactor: extractComments.filename option
Browse files Browse the repository at this point in the history
BREAKING CHANGE: if the value from the `extractComments.filename` option conflicts with existing assets, an error will be thrown instead of a warning
  • Loading branch information
evilebottnawi authored Apr 30, 2020
1 parent d509135 commit 974bc19
Show file tree
Hide file tree
Showing 5 changed files with 1,137 additions and 33 deletions.
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,9 @@ module.exports = {
new TerserPlugin({
extractComments: {
condition: 'some',
filename: (file, fileData) => {
// ⚠ webpack 5: there is only fileData parameter

// A file can contain a query string (for example when you have `output.filename: '[name].js?[chunkhash]'`)
// You must consider this
return file.replace(/\.(\w+)($|\?)/, '.$1.LICENSE.txt$2');
filename: (fileData) => {
// The "fileData" argument contains object with "filename", "basename", "query" and "hash"
return `${fileData.filename}.LICENSE.txt${fileData.query}`;
},
banner: (licenseFile) => {
return `License information can be found in ${licenseFile}`;
Expand Down Expand Up @@ -590,12 +587,9 @@ module.exports = {
new TerserPlugin({
extractComments: {
condition: true,
filename: (file, fileData) => {
// ⚠ webpack 5: there is only fileData parameter

// A file can contain a query string (for example when you have `output.filename: '[name].js?[chunkhash]'`)
// You must consider this
return file.replace(/\.(\w+)($|\?)/, '.$1.LICENSE.txt$2');
filename: (fileData) => {
// The "fileData" argument contains object with "filename", "basename", "query" and "hash"
return `${fileData.filename}.LICENSE.txt${fileData.query}`;
},
banner: (commentsFile) => {
return `My custom banner about license information ${commentsFile}`;
Expand Down
24 changes: 11 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,20 +256,18 @@ class TerserPlugin {
const data = { filename, basename, query };

commentsFilename = compilation.getPath(commentsFilename, data);
}

if (
commentsFilename &&
TerserPlugin.hasAsset(commentsFilename, compilation.assets)
) {
// Todo make error and stop uglifing in next major release
compilation.warnings.push(
new Error(
`The comment file "${TerserPlugin.removeQueryString(
commentsFilename
)}" conflicts with an existing asset, this may lead to code corruption, please use a different name`
)
);
if (TerserPlugin.hasAsset(commentsFilename, compilation.assets)) {
compilation.errors.push(
new Error(
`The comment file "${TerserPlugin.removeQueryString(
commentsFilename
)}" conflicts with an existing asset, this may lead to code corruption, please use a different name`
)
);

yield false;
}
}

const callback = (taskResult) => {
Expand Down
Loading

0 comments on commit 974bc19

Please sign in to comment.