Skip to content

Commit aba8ba7

Browse files
authoredSep 13, 2019
fix: correct interpretation url for extracted comment file (#157)
1 parent 2b4d2a4 commit aba8ba7

File tree

3 files changed

+174
-4
lines changed

3 files changed

+174
-4
lines changed
 

‎src/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@ class TerserPlugin {
265265

266266
const data = { filename, basename, query };
267267

268-
commentsFilename = compilation.getPath(commentsFilename, data);
268+
commentsFilename = compilation
269+
.getPath(commentsFilename, data)
270+
.replace(/\\/g, '/');
269271
}
270272

271273
if (
@@ -396,9 +398,9 @@ class TerserPlugin {
396398
if (this.options.extractComments.banner !== false) {
397399
let banner =
398400
this.options.extractComments.banner ||
399-
`For license information please see ${path.posix.basename(
400-
commentsFilename
401-
)}`;
401+
`For license information please see ${path
402+
.relative(path.dirname(file), commentsFilename)
403+
.replace(/\\/g, '/')}`;
402404

403405
if (typeof banner === 'function') {
404406
banner = banner(commentsFilename);

‎test/__snapshots__/extractComments-option.test.js.snap

+93
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,99 @@ exports[`extractComments option should match snapshot for a "function" value: er
319319

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

322+
exports[`extractComments option should match snapshot for comment file when filename is nested: assets 1`] = `
323+
Object {
324+
"nested/directory/1.js?999de273652786141058": "/*! For license information please see ../../one.js */
325+
(window.webpackJsonp=window.webpackJsonp||[]).push([[1],[,function(n,o){n.exports=Math.random()}]]);",
326+
"nested/directory/one.js?c68bb9619ed1649b68b2": "/*! For license information please see ../../one.js */
327+
!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()}]);",
328+
"one.js": "/*! Legal Comment */
329+
330+
/**
331+
* @preserve Copyright 2009 SomeThirdParty.
332+
* Here is the full license text and copyright
333+
* notice for this file. Note that the notice can span several
334+
* lines and is only terminated by the closing star and slash:
335+
*/
336+
337+
/**
338+
* Utility functions for the foo package.
339+
* @license Apache-2.0
340+
*/
341+
342+
/*! Legal Foo */
343+
344+
/** @license Copyright 2112 Moon. **/
345+
",
346+
}
347+
`;
348+
349+
exports[`extractComments option should match snapshot for comment file when filename is nested: errors 1`] = `Array []`;
350+
351+
exports[`extractComments option should match snapshot for comment file when filename is nested: warnings 1`] = `Array []`;
352+
353+
exports[`extractComments option should match snapshot for nested comment file with "\\": assets 1`] = `
354+
Object {
355+
"1.1.js": "/*! For license information please see comments/directory/one.js */
356+
(window.webpackJsonp=window.webpackJsonp||[]).push([[1],[,function(n,o){n.exports=Math.random()}]]);",
357+
"comments/directory/one.js": "/*! Legal Comment */
358+
359+
/**
360+
* @preserve Copyright 2009 SomeThirdParty.
361+
* Here is the full license text and copyright
362+
* notice for this file. Note that the notice can span several
363+
* lines and is only terminated by the closing star and slash:
364+
*/
365+
366+
/**
367+
* Utility functions for the foo package.
368+
* @license Apache-2.0
369+
*/
370+
371+
/*! Legal Foo */
372+
373+
/** @license Copyright 2112 Moon. **/
374+
",
375+
"one.js": "/*! For license information please see comments/directory/one.js */
376+
!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()}]);",
377+
}
378+
`;
379+
380+
exports[`extractComments option should match snapshot for nested comment file with "\\": errors 1`] = `Array []`;
381+
382+
exports[`extractComments option should match snapshot for nested comment file with "\\": warnings 1`] = `Array []`;
383+
384+
exports[`extractComments option should match snapshot for nested comment file: assets 1`] = `
385+
Object {
386+
"1.1.js": "/*! For license information please see comments/directory/one.js */
387+
(window.webpackJsonp=window.webpackJsonp||[]).push([[1],[,function(n,o){n.exports=Math.random()}]]);",
388+
"comments/directory/one.js": "/*! Legal Comment */
389+
390+
/**
391+
* @preserve Copyright 2009 SomeThirdParty.
392+
* Here is the full license text and copyright
393+
* notice for this file. Note that the notice can span several
394+
* lines and is only terminated by the closing star and slash:
395+
*/
396+
397+
/**
398+
* Utility functions for the foo package.
399+
* @license Apache-2.0
400+
*/
401+
402+
/*! Legal Foo */
403+
404+
/** @license Copyright 2112 Moon. **/
405+
",
406+
"one.js": "/*! For license information please see comments/directory/one.js */
407+
!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()}]);",
408+
}
409+
`;
410+
411+
exports[`extractComments option should match snapshot for nested comment file: errors 1`] = `Array []`;
412+
413+
exports[`extractComments option should match snapshot for nested comment file: warnings 1`] = `Array []`;
414+
322415
exports[`extractComments option should match snapshot for the "/Foo/" value: assets 1`] = `
323416
Object {
324417
"chunks/4.4.735e78ca27ceea7298d5.js": "(window.webpackJsonp=window.webpackJsonp||[]).push([[4],{4:function(n,o){n.exports=Math.random()}}]);",

‎test/extractComments-option.test.js

+75
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,79 @@ describe('extractComments option', () => {
455455
expect(errors).toMatchSnapshot('errors');
456456
expect(warnings).toMatchSnapshot('warnings');
457457
});
458+
459+
it('should match snapshot for nested comment file', async () => {
460+
compiler = createCompiler({
461+
entry: {
462+
one: `${__dirname}/fixtures/comments.js`,
463+
},
464+
});
465+
466+
new TerserPlugin({
467+
extractComments: {
468+
condition: true,
469+
filename: 'comments/directory/one.js',
470+
},
471+
}).apply(compiler);
472+
473+
const stats = await compile(compiler);
474+
475+
const errors = stats.compilation.errors.map(cleanErrorStack);
476+
const warnings = stats.compilation.warnings.map(cleanErrorStack);
477+
478+
expect(errors).toMatchSnapshot('errors');
479+
expect(warnings).toMatchSnapshot('warnings');
480+
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
481+
});
482+
483+
it('should match snapshot for comment file when filename is nested', async () => {
484+
compiler = createCompiler({
485+
entry: {
486+
one: `${__dirname}/fixtures/comments.js`,
487+
},
488+
output: {
489+
filename: 'nested/directory/[name].js?[chunkhash]',
490+
},
491+
});
492+
493+
new TerserPlugin({
494+
extractComments: {
495+
condition: true,
496+
filename: 'one.js',
497+
},
498+
}).apply(compiler);
499+
500+
const stats = await compile(compiler);
501+
502+
const errors = stats.compilation.errors.map(cleanErrorStack);
503+
const warnings = stats.compilation.warnings.map(cleanErrorStack);
504+
505+
expect(errors).toMatchSnapshot('errors');
506+
expect(warnings).toMatchSnapshot('warnings');
507+
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
508+
});
509+
510+
it('should match snapshot for nested comment file with "\\"', async () => {
511+
compiler = createCompiler({
512+
entry: {
513+
one: `${__dirname}/fixtures/comments.js`,
514+
},
515+
});
516+
517+
new TerserPlugin({
518+
extractComments: {
519+
condition: true,
520+
filename: 'comments\\directory\\one.js',
521+
},
522+
}).apply(compiler);
523+
524+
const stats = await compile(compiler);
525+
526+
const errors = stats.compilation.errors.map(cleanErrorStack);
527+
const warnings = stats.compilation.warnings.map(cleanErrorStack);
528+
529+
expect(errors).toMatchSnapshot('errors');
530+
expect(warnings).toMatchSnapshot('warnings');
531+
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
532+
});
458533
});

0 commit comments

Comments
 (0)
Failed to load comments.