Skip to content

Commit

Permalink
feat: improve naming of extracted file with comments (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Sep 12, 2019
1 parent cea7243 commit 5fe3337
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 13 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,11 @@ module.exports = {
new TerserPlugin({
extractComments: {
condition: /^\**!|@preserve|@license|@cc_on/i,
filename: (file) => {
return `${file}.LICENSE`;
filename: (file, fileData) => {
// A file can contain a query string (for example when you have `output.filename: '[name].js?[chunkhash]'`)
// You must consider this
// The "fileData" argument contains object with "filename", "basename", "query"
return file.replace(/\.(\w+)($|\?)/, '.$1.LICENSE$2');
},
banner: (licenseFile) => {
return `License information can be found in ${licenseFile}`;
Expand Down Expand Up @@ -518,8 +521,10 @@ module.exports = {
new TerserPlugin({
extractComments: {
condition: 'some',
filename: (file) => {
return `${file}.LICENSE`;
filename: (file, fileData) => {
// 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$2');
},
banner: (licenseFile) => {
return `License information can be found in ${licenseFile}`;
Expand All @@ -534,7 +539,9 @@ module.exports = {
##### `filename`

Type: `String|Function<(string) -> String>`
Default: `${file}.LICENSE`
Default: `[file].LICENSE[query]`

Available placeholders: `[file]`, `[query]` and `[filebase]`.

The file where the extracted comments will be stored.
Default is to append the suffix `.LICENSE` to the original filename.
Expand Down Expand Up @@ -579,8 +586,10 @@ module.exports = {
new TerserPlugin({
extractComments: {
condition: true,
filename: (file) => {
return `${file}.LICENSE`;
filename: (file, fileData) => {
// 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$2');
},
banner: (commentsFile) => {
return `My custom banner about license information ${commentsFile}`;
Expand Down
27 changes: 25 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,34 @@ class TerserPlugin {

if (this.options.extractComments) {
commentsFile =
this.options.extractComments.filename || `${file}.LICENSE`;
this.options.extractComments.filename ||
'[file].LICENSE[query]';

// Todo remove this in next major release
if (typeof commentsFile === 'function') {
commentsFile = commentsFile(file);
commentsFile = commentsFile.bind(null, file);
}

let query = '';
let filename = file;

const querySplit = filename.indexOf('?');

if (querySplit >= 0) {
query = filename.substr(querySplit);
filename = filename.substr(0, querySplit);
}

const lastSlashIndex = filename.lastIndexOf('/');

const basename =
lastSlashIndex === -1
? filename
: filename.substr(lastSlashIndex + 1);

const data = { filename, basename, query };

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

const task = {
Expand Down
Loading

0 comments on commit 5fe3337

Please sign in to comment.