Skip to content

Commit 5fe3337

Browse files
authoredSep 12, 2019
feat: improve naming of extracted file with comments (#154)
1 parent cea7243 commit 5fe3337

File tree

4 files changed

+332
-13
lines changed

4 files changed

+332
-13
lines changed
 

‎README.md

+16-7
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,11 @@ module.exports = {
489489
new TerserPlugin({
490490
extractComments: {
491491
condition: /^\**!|@preserve|@license|@cc_on/i,
492-
filename: (file) => {
493-
return `${file}.LICENSE`;
492+
filename: (file, fileData) => {
493+
// A file can contain a query string (for example when you have `output.filename: '[name].js?[chunkhash]'`)
494+
// You must consider this
495+
// The "fileData" argument contains object with "filename", "basename", "query"
496+
return file.replace(/\.(\w+)($|\?)/, '.$1.LICENSE$2');
494497
},
495498
banner: (licenseFile) => {
496499
return `License information can be found in ${licenseFile}`;
@@ -518,8 +521,10 @@ module.exports = {
518521
new TerserPlugin({
519522
extractComments: {
520523
condition: 'some',
521-
filename: (file) => {
522-
return `${file}.LICENSE`;
524+
filename: (file, fileData) => {
525+
// A file can contain a query string (for example when you have `output.filename: '[name].js?[chunkhash]'`)
526+
// You must consider this
527+
return file.replace(/\.(\w+)($|\?)/, '.$1.LICENSE$2');
523528
},
524529
banner: (licenseFile) => {
525530
return `License information can be found in ${licenseFile}`;
@@ -534,7 +539,9 @@ module.exports = {
534539
##### `filename`
535540

536541
Type: `String|Function<(string) -> String>`
537-
Default: `${file}.LICENSE`
542+
Default: `[file].LICENSE[query]`
543+
544+
Available placeholders: `[file]`, `[query]` and `[filebase]`.
538545

539546
The file where the extracted comments will be stored.
540547
Default is to append the suffix `.LICENSE` to the original filename.
@@ -579,8 +586,10 @@ module.exports = {
579586
new TerserPlugin({
580587
extractComments: {
581588
condition: true,
582-
filename: (file) => {
583-
return `${file}.LICENSE`;
589+
filename: (file, fileData) => {
590+
// A file can contain a query string (for example when you have `output.filename: '[name].js?[chunkhash]'`)
591+
// You must consider this
592+
return file.replace(/\.(\w+)($|\?)/, '.$1.LICENSE$2');
584593
},
585594
banner: (commentsFile) => {
586595
return `My custom banner about license information ${commentsFile}`;

‎src/index.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,34 @@ class TerserPlugin {
216216

217217
if (this.options.extractComments) {
218218
commentsFile =
219-
this.options.extractComments.filename || `${file}.LICENSE`;
219+
this.options.extractComments.filename ||
220+
'[file].LICENSE[query]';
220221

222+
// Todo remove this in next major release
221223
if (typeof commentsFile === 'function') {
222-
commentsFile = commentsFile(file);
224+
commentsFile = commentsFile.bind(null, file);
223225
}
226+
227+
let query = '';
228+
let filename = file;
229+
230+
const querySplit = filename.indexOf('?');
231+
232+
if (querySplit >= 0) {
233+
query = filename.substr(querySplit);
234+
filename = filename.substr(0, querySplit);
235+
}
236+
237+
const lastSlashIndex = filename.lastIndexOf('/');
238+
239+
const basename =
240+
lastSlashIndex === -1
241+
? filename
242+
: filename.substr(lastSlashIndex + 1);
243+
244+
const data = { filename, basename, query };
245+
246+
commentsFile = compilation.getPath(commentsFile, data);
224247
}
225248

226249
const task = {

0 commit comments

Comments
 (0)
Failed to load comments.