Skip to content

Commit

Permalink
feat: pass license files as related assets for webpack@5
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 10, 2020
1 parent 0459b2c commit 7d3ae95
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/index.js
Expand Up @@ -275,10 +275,14 @@ class TerserPlugin {
outputSource = new RawSource(code);
}

const assetInfo = { ...info, minimized: true };

// Write extracted comments to commentsFilename
if (hasExtractedComments) {
let banner;

assetInfo.related = { license: commentsFilename };

// Add a banner to the original file
if (hasBannerForExtractedComments) {
banner =
Expand Down Expand Up @@ -334,10 +338,7 @@ class TerserPlugin {
}
}

TerserPlugin.updateAsset(compilation, name, outputSource, {
...info,
minimized: true,
});
TerserPlugin.updateAsset(compilation, name, outputSource, assetInfo);
};

const task = {
Expand Down
18 changes: 18 additions & 0 deletions test/TerserPlugin.test.js
Expand Up @@ -825,4 +825,22 @@ describe('TerserPlugin', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work and show related assets in stats', async () => {
const compiler = getCompiler({
entry: { comments: `${__dirname}/fixtures/comments-4.js` },
devtool: 'source-map',
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(stats.toString().indexOf('2 related asset') !== -1).toBe(
!getCompiler.isWebpack4()
);
expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});
});
18 changes: 18 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack4
Expand Up @@ -126,6 +126,24 @@ exports[`TerserPlugin should work and show minimized assets in stats: errors 1`]

exports[`TerserPlugin should work and show minimized assets in stats: warnings 1`] = `Array []`;

exports[`TerserPlugin should work and show related assets in stats: assets 1`] = `
Object {
"comments.js": "/*! For license information please see comments.js.LICENSE.txt */
!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,\\"a\\",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p=\\"\\",r(r.s=0)}([function(e,t){e.exports=Math.random()}]);
//# sourceMappingURL=comments.js.map",
"comments.js.LICENSE.txt": "/**
* Duplicate comment in difference files.
* @license MIT
*/
",
"comments.js.map": "{\\"version\\":3,\\"sources\\":[\\"webpack:///webpack/bootstrap\\",\\"webpack:///./test/fixtures/comments-4.js\\"],\\"names\\":[\\"installedModules\\",\\"__webpack_require__\\",\\"moduleId\\",\\"exports\\",\\"module\\",\\"i\\",\\"l\\",\\"modules\\",\\"call\\",\\"m\\",\\"c\\",\\"d\\",\\"name\\",\\"getter\\",\\"o\\",\\"Object\\",\\"defineProperty\\",\\"enumerable\\",\\"get\\",\\"r\\",\\"Symbol\\",\\"toStringTag\\",\\"value\\",\\"t\\",\\"mode\\",\\"__esModule\\",\\"ns\\",\\"create\\",\\"key\\",\\"bind\\",\\"n\\",\\"object\\",\\"property\\",\\"prototype\\",\\"hasOwnProperty\\",\\"p\\",\\"s\\",\\"Math\\",\\"random\\"],\\"mappings\\":\\";aACE,IAAIA,EAAmB,GAGvB,SAASC,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Ba,EAAI,SAASiB,EAAQC,GAAY,OAAOjB,OAAOkB,UAAUC,eAAe1B,KAAKuB,EAAQC,IAGzG/B,EAAoBkC,EAAI,GAIjBlC,EAAoBA,EAAoBmC,EAAI,G,gBC7ErDhC,EAAOD,QAAUkC,KAAKC\\",\\"file\\":\\"comments.js\\",\\"sourcesContent\\":[\\" \\\\t// The module cache\\\\n \\\\tvar installedModules = {};\\\\n\\\\n \\\\t// The require function\\\\n \\\\tfunction __webpack_require__(moduleId) {\\\\n\\\\n \\\\t\\\\t// Check if module is in cache\\\\n \\\\t\\\\tif(installedModules[moduleId]) {\\\\n \\\\t\\\\t\\\\treturn installedModules[moduleId].exports;\\\\n \\\\t\\\\t}\\\\n \\\\t\\\\t// Create a new module (and put it into the cache)\\\\n \\\\t\\\\tvar module = installedModules[moduleId] = {\\\\n \\\\t\\\\t\\\\ti: moduleId,\\\\n \\\\t\\\\t\\\\tl: false,\\\\n \\\\t\\\\t\\\\texports: {}\\\\n \\\\t\\\\t};\\\\n\\\\n \\\\t\\\\t// Execute the module function\\\\n \\\\t\\\\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\\\\n\\\\n \\\\t\\\\t// Flag the module as loaded\\\\n \\\\t\\\\tmodule.l = true;\\\\n\\\\n \\\\t\\\\t// Return the exports of the module\\\\n \\\\t\\\\treturn module.exports;\\\\n \\\\t}\\\\n\\\\n\\\\n \\\\t// expose the modules object (__webpack_modules__)\\\\n \\\\t__webpack_require__.m = modules;\\\\n\\\\n \\\\t// expose the module cache\\\\n \\\\t__webpack_require__.c = installedModules;\\\\n\\\\n \\\\t// define getter function for harmony exports\\\\n \\\\t__webpack_require__.d = function(exports, name, getter) {\\\\n \\\\t\\\\tif(!__webpack_require__.o(exports, name)) {\\\\n \\\\t\\\\t\\\\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\\\\n \\\\t\\\\t}\\\\n \\\\t};\\\\n\\\\n \\\\t// define __esModule on exports\\\\n \\\\t__webpack_require__.r = function(exports) {\\\\n \\\\t\\\\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\\\\n \\\\t\\\\t\\\\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\\\\n \\\\t\\\\t}\\\\n \\\\t\\\\tObject.defineProperty(exports, '__esModule', { value: true });\\\\n \\\\t};\\\\n\\\\n \\\\t// create a fake namespace object\\\\n \\\\t// mode & 1: value is a module id, require it\\\\n \\\\t// mode & 2: merge all properties of value into the ns\\\\n \\\\t// mode & 4: return value when already ns object\\\\n \\\\t// mode & 8|1: behave like require\\\\n \\\\t__webpack_require__.t = function(value, mode) {\\\\n \\\\t\\\\tif(mode & 1) value = __webpack_require__(value);\\\\n \\\\t\\\\tif(mode & 8) return value;\\\\n \\\\t\\\\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\\\\n \\\\t\\\\tvar ns = Object.create(null);\\\\n \\\\t\\\\t__webpack_require__.r(ns);\\\\n \\\\t\\\\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\\\\n \\\\t\\\\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\\\\n \\\\t\\\\treturn ns;\\\\n \\\\t};\\\\n\\\\n \\\\t// getDefaultExport function for compatibility with non-harmony modules\\\\n \\\\t__webpack_require__.n = function(module) {\\\\n \\\\t\\\\tvar getter = module && module.__esModule ?\\\\n \\\\t\\\\t\\\\tfunction getDefault() { return module['default']; } :\\\\n \\\\t\\\\t\\\\tfunction getModuleExports() { return module; };\\\\n \\\\t\\\\t__webpack_require__.d(getter, 'a', getter);\\\\n \\\\t\\\\treturn getter;\\\\n \\\\t};\\\\n\\\\n \\\\t// Object.prototype.hasOwnProperty.call\\\\n \\\\t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\\\\n\\\\n \\\\t// __webpack_public_path__\\\\n \\\\t__webpack_require__.p = \\\\\\"\\\\\\";\\\\n\\\\n\\\\n \\\\t// Load entry module and return exports\\\\n \\\\treturn __webpack_require__(__webpack_require__.s = 0);\\\\n\\",\\"/**\\\\n * Duplicate comment in difference files.\\\\n * @license MIT\\\\n */\\\\n\\\\nmodule.exports = Math.random();\\\\n\\"],\\"sourceRoot\\":\\"\\"}",
}
`;

exports[`TerserPlugin should work and show related assets in stats: errors 1`] = `Array []`;

exports[`TerserPlugin should work and show related assets in stats: warnings 1`] = `Array []`;

exports[`TerserPlugin should work as a minimizer: assets 1`] = `
Object {
"main.js": "!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);",
Expand Down
18 changes: 18 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack5
Expand Up @@ -126,6 +126,24 @@ exports[`TerserPlugin should work and show minimized assets in stats: errors 1`]

exports[`TerserPlugin should work and show minimized assets in stats: warnings 1`] = `Array []`;

exports[`TerserPlugin should work and show related assets in stats: assets 1`] = `
Object {
"comments.js": "/*! For license information please see comments.js.LICENSE.txt */
(()=>{var r={712:r=>{r.exports=Math.random()}},t={};!function e(o){if(t[o])return t[o].exports;var n=t[o]={exports:{}};return r[o](n,n.exports,e),n.exports}(712)})();
//# sourceMappingURL=comments.js.map",
"comments.js.LICENSE.txt": "/**
* Duplicate comment in difference files.
* @license MIT
*/
",
"comments.js.map": "{\\"version\\":3,\\"sources\\":[\\"webpack://terser-webpack-plugin/./test/fixtures/comments-4.js\\",\\"webpack://terser-webpack-plugin/webpack/bootstrap\\",\\"webpack://terser-webpack-plugin/webpack/startup\\"],\\"names\\":[\\"module\\",\\"exports\\",\\"Math\\",\\"random\\",\\"__webpack_module_cache__\\",\\"__webpack_require__\\",\\"moduleId\\",\\"__webpack_modules__\\"],\\"mappings\\":\\";qBAKAA,EAAOC,QAAUC,KAAKC,WCJlBC,EAA2B,IAG/B,SAASC,EAAoBC,GAE5B,GAAGF,EAAyBE,GAC3B,OAAOF,EAAyBE,GAAUL,QAG3C,IAAID,EAASI,EAAyBE,GAAY,CAGjDL,QAAS,IAOV,OAHAM,EAAoBD,GAAUN,EAAQA,EAAOC,QAASI,GAG/CL,EAAOC,QCjBfI,CAAoB,M\\",\\"file\\":\\"comments.js\\",\\"sourcesContent\\":[\\"/**\\\\n * Duplicate comment in difference files.\\\\n * @license MIT\\\\n */\\\\n\\\\nmodule.exports = Math.random();\\\\n\\",\\"// The module cache\\\\nvar __webpack_module_cache__ = {};\\\\n\\\\n// The require function\\\\nfunction __webpack_require__(moduleId) {\\\\n\\\\t// Check if module is in cache\\\\n\\\\tif(__webpack_module_cache__[moduleId]) {\\\\n\\\\t\\\\treturn __webpack_module_cache__[moduleId].exports;\\\\n\\\\t}\\\\n\\\\t// Create a new module (and put it into the cache)\\\\n\\\\tvar module = __webpack_module_cache__[moduleId] = {\\\\n\\\\t\\\\t// no module.id needed\\\\n\\\\t\\\\t// no module.loaded needed\\\\n\\\\t\\\\texports: {}\\\\n\\\\t};\\\\n\\\\n\\\\t// Execute the module function\\\\n\\\\t__webpack_modules__[moduleId](module, module.exports, __webpack_require__);\\\\n\\\\n\\\\t// Return the exports of the module\\\\n\\\\treturn module.exports;\\\\n}\\\\n\\\\n\\",\\"// startup\\\\n// Load entry module\\\\n// This entry module is referenced by other modules so it can't be inlined\\\\n__webpack_require__(712);\\\\n\\"],\\"sourceRoot\\":\\"\\"}",
}
`;

exports[`TerserPlugin should work and show related assets in stats: errors 1`] = `Array []`;

exports[`TerserPlugin should work and show related assets in stats: warnings 1`] = `Array []`;

exports[`TerserPlugin should work as a minimizer: assets 1`] = `
Object {
"main.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();",
Expand Down

0 comments on commit 7d3ae95

Please sign in to comment.