Skip to content
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
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ class TerserPlugin {

const data = { filename, basename, query };

commentsFilename = compilation.getPath(commentsFilename, data);
commentsFilename = compilation
.getPath(commentsFilename, data)
.replace(/\\/g, '/');
}

if (
Expand Down Expand Up @@ -396,9 +398,9 @@ class TerserPlugin {
if (this.options.extractComments.banner !== false) {
let banner =
this.options.extractComments.banner ||
`For license information please see ${path.posix.basename(
commentsFilename
)}`;
`For license information please see ${path
.relative(path.dirname(file), commentsFilename)
.replace(/\\/g, '/')}`;

if (typeof banner === 'function') {
banner = banner(commentsFilename);
Expand Down
93 changes: 93 additions & 0 deletions test/__snapshots__/extractComments-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,99 @@ exports[`extractComments option should match snapshot for a "function" value: er

exports[`extractComments option should match snapshot for a "function" value: warnings 1`] = `Array []`;

exports[`extractComments option should match snapshot for comment file when filename is nested: assets 1`] = `
Object {
"nested/directory/1.js?999de273652786141058": "/*! For license information please see ../../one.js */
(window.webpackJsonp=window.webpackJsonp||[]).push([[1],[,function(n,o){n.exports=Math.random()}]]);",
"nested/directory/one.js?c68bb9619ed1649b68b2": "/*! For license information please see ../../one.js */
!function(e){function t(t){for(var r,o,u=t[0],i=t[1],a=0,l=[];a<u.length;a++)o=u[a],Object.prototype.hasOwnProperty.call(n,o)&&n[o]&&l.push(n[o][0]),n[o]=0;for(r in i)Object.prototype.hasOwnProperty.call(i,r)&&(e[r]=i[r]);for(c&&c(t);l.length;)l.shift()()}var r={},n={0:0};function o(t){if(r[t])return r[t].exports;var n=r[t]={i:t,l:!1,exports:{}};return e[t].call(n.exports,n,n.exports,o),n.l=!0,n.exports}o.e=function(e){var t=[],r=n[e];if(0!==r)if(r)t.push(r[2]);else{var u=new Promise((function(t,o){r=n[e]=[t,o]}));t.push(r[2]=u);var i,a=document.createElement(\\"script\\");a.charset=\\"utf-8\\",a.timeout=120,o.nc&&a.setAttribute(\\"nonce\\",o.nc),a.src=function(e){return o.p+\\"nested/directory/\\"+({}[e]||e)+\\".js?\\"+{1:\\"999de273652786141058\\"}[e]}(e);var c=new Error;i=function(t){a.onerror=a.onload=null,clearTimeout(l);var r=n[e];if(0!==r){if(r){var o=t&&(\\"load\\"===t.type?\\"missing\\":t.type),u=t&&t.target&&t.target.src;c.message=\\"Loading chunk \\"+e+\\" failed.\\\\n(\\"+o+\\": \\"+u+\\")\\",c.name=\\"ChunkLoadError\\",c.type=o,c.request=u,r[1](c)}n[e]=void 0}};var l=setTimeout((function(){i({type:\\"timeout\\",target:a})}),12e4);a.onerror=a.onload=i,document.head.appendChild(a)}return Promise.all(t)},o.m=e,o.c=r,o.d=function(e,t,r){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},o.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},o.t=function(e,t){if(1&t&&(e=o(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(o.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var n in e)o.d(r,n,function(t){return e[t]}.bind(null,n));return r},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,\\"a\\",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p=\\"\\",o.oe=function(e){throw console.error(e),e};var u=window.webpackJsonp=window.webpackJsonp||[],i=u.push.bind(u);u.push=t,u=u.slice();for(var a=0;a<u.length;a++)t(u[a]);var c=i;o(o.s=0)}([function(e,t,r){r.e(1).then(r.t.bind(null,1,7)),e.exports=Math.random()}]);",
"one.js": "/*! Legal Comment */

/**
* @preserve Copyright 2009 SomeThirdParty.
* Here is the full license text and copyright
* notice for this file. Note that the notice can span several
* lines and is only terminated by the closing star and slash:
*/

/**
* Utility functions for the foo package.
* @license Apache-2.0
*/

/*! Legal Foo */

/** @license Copyright 2112 Moon. **/
",
}
`;

exports[`extractComments option should match snapshot for comment file when filename is nested: errors 1`] = `Array []`;

exports[`extractComments option should match snapshot for comment file when filename is nested: warnings 1`] = `Array []`;

exports[`extractComments option should match snapshot for nested comment file with "\\": assets 1`] = `
Object {
"1.1.js": "/*! For license information please see comments/directory/one.js */
(window.webpackJsonp=window.webpackJsonp||[]).push([[1],[,function(n,o){n.exports=Math.random()}]]);",
"comments/directory/one.js": "/*! Legal Comment */

/**
* @preserve Copyright 2009 SomeThirdParty.
* Here is the full license text and copyright
* notice for this file. Note that the notice can span several
* lines and is only terminated by the closing star and slash:
*/

/**
* Utility functions for the foo package.
* @license Apache-2.0
*/

/*! Legal Foo */

/** @license Copyright 2112 Moon. **/
",
"one.js": "/*! For license information please see comments/directory/one.js */
!function(e){function t(t){for(var r,o,u=t[0],i=t[1],a=0,l=[];a<u.length;a++)o=u[a],Object.prototype.hasOwnProperty.call(n,o)&&n[o]&&l.push(n[o][0]),n[o]=0;for(r in i)Object.prototype.hasOwnProperty.call(i,r)&&(e[r]=i[r]);for(c&&c(t);l.length;)l.shift()()}var r={},n={0:0};function o(t){if(r[t])return r[t].exports;var n=r[t]={i:t,l:!1,exports:{}};return e[t].call(n.exports,n,n.exports,o),n.l=!0,n.exports}o.e=function(e){var t=[],r=n[e];if(0!==r)if(r)t.push(r[2]);else{var u=new Promise((function(t,o){r=n[e]=[t,o]}));t.push(r[2]=u);var i,a=document.createElement(\\"script\\");a.charset=\\"utf-8\\",a.timeout=120,o.nc&&a.setAttribute(\\"nonce\\",o.nc),a.src=function(e){return o.p+\\"\\"+e+\\".\\"+({}[e]||e)+\\".js\\"}(e);var c=new Error;i=function(t){a.onerror=a.onload=null,clearTimeout(l);var r=n[e];if(0!==r){if(r){var o=t&&(\\"load\\"===t.type?\\"missing\\":t.type),u=t&&t.target&&t.target.src;c.message=\\"Loading chunk \\"+e+\\" failed.\\\\n(\\"+o+\\": \\"+u+\\")\\",c.name=\\"ChunkLoadError\\",c.type=o,c.request=u,r[1](c)}n[e]=void 0}};var l=setTimeout((function(){i({type:\\"timeout\\",target:a})}),12e4);a.onerror=a.onload=i,document.head.appendChild(a)}return Promise.all(t)},o.m=e,o.c=r,o.d=function(e,t,r){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},o.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},o.t=function(e,t){if(1&t&&(e=o(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(o.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var n in e)o.d(r,n,function(t){return e[t]}.bind(null,n));return r},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,\\"a\\",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p=\\"\\",o.oe=function(e){throw console.error(e),e};var u=window.webpackJsonp=window.webpackJsonp||[],i=u.push.bind(u);u.push=t,u=u.slice();for(var a=0;a<u.length;a++)t(u[a]);var c=i;o(o.s=0)}([function(e,t,r){r.e(1).then(r.t.bind(null,1,7)),e.exports=Math.random()}]);",
}
`;

exports[`extractComments option should match snapshot for nested comment file with "\\": errors 1`] = `Array []`;

exports[`extractComments option should match snapshot for nested comment file with "\\": warnings 1`] = `Array []`;

exports[`extractComments option should match snapshot for nested comment file: assets 1`] = `
Object {
"1.1.js": "/*! For license information please see comments/directory/one.js */
(window.webpackJsonp=window.webpackJsonp||[]).push([[1],[,function(n,o){n.exports=Math.random()}]]);",
"comments/directory/one.js": "/*! Legal Comment */

/**
* @preserve Copyright 2009 SomeThirdParty.
* Here is the full license text and copyright
* notice for this file. Note that the notice can span several
* lines and is only terminated by the closing star and slash:
*/

/**
* Utility functions for the foo package.
* @license Apache-2.0
*/

/*! Legal Foo */

/** @license Copyright 2112 Moon. **/
",
"one.js": "/*! For license information please see comments/directory/one.js */
!function(e){function t(t){for(var r,o,u=t[0],i=t[1],a=0,l=[];a<u.length;a++)o=u[a],Object.prototype.hasOwnProperty.call(n,o)&&n[o]&&l.push(n[o][0]),n[o]=0;for(r in i)Object.prototype.hasOwnProperty.call(i,r)&&(e[r]=i[r]);for(c&&c(t);l.length;)l.shift()()}var r={},n={0:0};function o(t){if(r[t])return r[t].exports;var n=r[t]={i:t,l:!1,exports:{}};return e[t].call(n.exports,n,n.exports,o),n.l=!0,n.exports}o.e=function(e){var t=[],r=n[e];if(0!==r)if(r)t.push(r[2]);else{var u=new Promise((function(t,o){r=n[e]=[t,o]}));t.push(r[2]=u);var i,a=document.createElement(\\"script\\");a.charset=\\"utf-8\\",a.timeout=120,o.nc&&a.setAttribute(\\"nonce\\",o.nc),a.src=function(e){return o.p+\\"\\"+e+\\".\\"+({}[e]||e)+\\".js\\"}(e);var c=new Error;i=function(t){a.onerror=a.onload=null,clearTimeout(l);var r=n[e];if(0!==r){if(r){var o=t&&(\\"load\\"===t.type?\\"missing\\":t.type),u=t&&t.target&&t.target.src;c.message=\\"Loading chunk \\"+e+\\" failed.\\\\n(\\"+o+\\": \\"+u+\\")\\",c.name=\\"ChunkLoadError\\",c.type=o,c.request=u,r[1](c)}n[e]=void 0}};var l=setTimeout((function(){i({type:\\"timeout\\",target:a})}),12e4);a.onerror=a.onload=i,document.head.appendChild(a)}return Promise.all(t)},o.m=e,o.c=r,o.d=function(e,t,r){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},o.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},o.t=function(e,t){if(1&t&&(e=o(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(o.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var n in e)o.d(r,n,function(t){return e[t]}.bind(null,n));return r},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,\\"a\\",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p=\\"\\",o.oe=function(e){throw console.error(e),e};var u=window.webpackJsonp=window.webpackJsonp||[],i=u.push.bind(u);u.push=t,u=u.slice();for(var a=0;a<u.length;a++)t(u[a]);var c=i;o(o.s=0)}([function(e,t,r){r.e(1).then(r.t.bind(null,1,7)),e.exports=Math.random()}]);",
}
`;

exports[`extractComments option should match snapshot for nested comment file: errors 1`] = `Array []`;

exports[`extractComments option should match snapshot for nested comment file: warnings 1`] = `Array []`;

exports[`extractComments option should match snapshot for the "/Foo/" value: assets 1`] = `
Object {
"chunks/4.4.735e78ca27ceea7298d5.js": "(window.webpackJsonp=window.webpackJsonp||[]).push([[4],{4:function(n,o){n.exports=Math.random()}}]);",
Expand Down
75 changes: 75 additions & 0 deletions test/extractComments-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,79 @@ describe('extractComments option', () => {
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
});

it('should match snapshot for nested comment file', async () => {
compiler = createCompiler({
entry: {
one: `${__dirname}/fixtures/comments.js`,
},
});

new TerserPlugin({
extractComments: {
condition: true,
filename: 'comments/directory/one.js',
},
}).apply(compiler);

const stats = await compile(compiler);

const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);

expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
});

it('should match snapshot for comment file when filename is nested', async () => {
compiler = createCompiler({
entry: {
one: `${__dirname}/fixtures/comments.js`,
},
output: {
filename: 'nested/directory/[name].js?[chunkhash]',
},
});

new TerserPlugin({
extractComments: {
condition: true,
filename: 'one.js',
},
}).apply(compiler);

const stats = await compile(compiler);

const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);

expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
});

it('should match snapshot for nested comment file with "\\"', async () => {
compiler = createCompiler({
entry: {
one: `${__dirname}/fixtures/comments.js`,
},
});

new TerserPlugin({
extractComments: {
condition: true,
filename: 'comments\\directory\\one.js',
},
}).apply(compiler);

const stats = await compile(compiler);

const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);

expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
});
});