Skip to content

Commit

Permalink
fix: do not crash on buffer assets
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jun 18, 2020
1 parent 0bf5158 commit 3c67023
Show file tree
Hide file tree
Showing 13 changed files with 1,306 additions and 908 deletions.
93 changes: 63 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"schema-utils": "^2.6.6",
"serialize-javascript": "^4.0.0",
"source-map": "^0.6.1",
"terser": "^4.6.13",
"terser": "^4.8.0",
"webpack-sources": "^1.4.3"
},
"devDependencies": {
Expand All @@ -66,9 +66,10 @@
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.21.2",
"file-loader": "^6.0.0",
"husky": "^4.2.5",
"jest": "^26.0.1",
"lint-staged": "^10.2.10",
"lint-staged": "^10.2.11",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ class TerserPlugin {
inputSourceMap = null;
}

if (Buffer.isBuffer(input)) {
input = input.toString();
}

// Handling comment extraction
let commentsFilename = false;

Expand Down
46 changes: 46 additions & 0 deletions test/TerserPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,52 @@ describe('TerserPlugin', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work with "file-loader"', async () => {
const compiler = getCompiler({
entry: path.resolve(__dirname, 'fixtures/file-loader.js'),
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work with "asset" module type', async () => {
if (getCompiler.isWebpack4()) {
expect(true).toBe(true);
} else {
const compiler = getCompiler({
entry: path.resolve(__dirname, 'fixtures/asset-resource.js'),
experiments: {
asset: true,
},
module: {
rules: [
{
test: /emitted\.js$/i,
type: 'asset/resource',
generator: {
filename: '[name][ext]',
},
},
],
},
});

new TerserPlugin().apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
}
});

it('should work and respect "terser" errors (the "parallel" option is "true")', async () => {
const compiler = getCompiler();

Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,18 @@ exports[`TerserPlugin should work when some of assets do not contain source maps

exports[`TerserPlugin should work when some of assets do not contain source maps: warnings 1`] = `Array []`;

exports[`TerserPlugin should work with "file-loader": assets 1`] = `
Object {
"main.js": "!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,r){\\"use strict\\";r.r(t);var n=r.p+\\"test/fixtures/emitted.js\\";console.log(12,n);t.default=12}]);",
"test/fixtures/emitted.js": "console.log('HERE');
",
}
`;

exports[`TerserPlugin should work with "file-loader": errors 1`] = `Array []`;

exports[`TerserPlugin should work with "file-loader": warnings 1`] = `Array []`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: assets 1`] = `
Object {
"one.js": "",
Expand Down
34 changes: 29 additions & 5 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ exports[`TerserPlugin should emit an error on a broken code in parallel mode: wa

exports[`TerserPlugin should regenerate hash: assets 1`] = `
Object {
"598.598.653c4778b7f6e9b7f5b4.js": "(window.webpackJsonpterser_webpack_plugin=window.webpackJsonpterser_webpack_plugin||[]).push([[598],{598:(e,p,s)=>{\\"use strict\\";s.r(p),s.d(p,{default:()=>n});const n=\\"async-dep\\"}}]);",
"AsyncImportExport.73968d91d66e50b17fcc.js": "(()=>{\\"use strict\\";var e={},r={};function o(t){if(r[t])return r[t].exports;var n=r[t]={exports:{}};return e[t](n,n.exports,o),n.exports}o.m=e,o.d=(e,r)=>{for(var t in r)o.o(r,t)&&!o.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},o.f={},o.e=e=>Promise.all(Object.keys(o.f).reduce((r,t)=>(o.f[t](e,r),r),[])),o.u=e=>e+\\".\\"+e+\\".653c4778b7f6e9b7f5b4.js\\",o.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),o.r=e=>{\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},o.p=\\"\\",(()=>{var e={988:0};o.f.j=(r,t)=>{var n=o.o(e,r)?e[r]:void 0;if(0!==n)if(n)t.push(n[2]);else{var a=new Promise((o,t)=>{n=e[r]=[o,t]});t.push(n[2]=a);var i,s=o.p+o.u(r),u=document.createElement(\\"script\\");u.charset=\\"utf-8\\",u.timeout=120,o.nc&&u.setAttribute(\\"nonce\\",o.nc),u.src=s;var p=new Error;i=t=>{i=()=>{},u.onerror=u.onload=null,clearTimeout(c);var a=(()=>{if(o.o(e,r)&&(0!==(n=e[r])&&(e[r]=void 0),n))return n[1]})();if(a){var s=t&&(\\"load\\"===t.type?\\"missing\\":t.type),l=t&&t.target&&t.target.src;p.message=\\"Loading chunk \\"+r+\\" failed.\\\\n(\\"+s+\\": \\"+l+\\")\\",p.name=\\"ChunkLoadError\\",p.type=s,p.request=l,a(p)}};var c=setTimeout(()=>{i({type:\\"timeout\\",target:u})},12e4);u.onerror=u.onload=i,document.head.appendChild(u)}};var r=window.webpackJsonpterser_webpack_plugin=window.webpackJsonpterser_webpack_plugin||[],t=r.push.bind(r);r.push=function(r){for(var t,a,i=r[0],s=r[1],u=r[3],p=0,c=[];p<i.length;p++)a=i[p],o.o(e,a)&&e[a]&&c.push(e[a][0]),e[a]=0;for(t in s)o.o(s,t)&&(o.m[t]=s[t]);for(u&&u(o),n&&n(r);c.length;)c.shift()()};var n=t})(),o.e(598).then(o.bind(o,598)).then(()=>{console.log(\\"Good\\")})})();",
"importExport.e2f22a3d7de2bb697fe3.js": "(()=>{\\"use strict\\"})();",
"js.5e1a960903604420bf56.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)})();",
"mjs.6b3e8ecfe8c17f0cb389.js": "(()=>{var r={631: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}(631)})();",
"598.598.70ff6985755ae80c6ef7.js": "(window.webpackJsonpterser_webpack_plugin=window.webpackJsonpterser_webpack_plugin||[]).push([[598],{598:(e,p,s)=>{\\"use strict\\";s.r(p),s.d(p,{default:()=>n});const n=\\"async-dep\\"}}]);",
"AsyncImportExport.aa25d3131bd99b7ed107.js": "(()=>{\\"use strict\\";var e,t={},r={};function o(e){if(r[e])return r[e].exports;var a=r[e]={exports:{}};return t[e](a,a.exports,o),a.exports}o.m=t,o.d=(e,t)=>{for(var r in t)o.o(t,r)&&!o.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o.f={},o.e=e=>Promise.all(Object.keys(o.f).reduce((t,r)=>(o.f[r](e,t),t),[])),o.u=e=>e+\\".\\"+e+\\".70ff6985755ae80c6ef7.js\\",o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),e={},o.l=(t,r,a)=>{if(e[t])e[t].push(r);else{var n,i;if(void 0!==a)for(var s=document.getElementsByTagName(\\"script\\"),u=0;u<s.length;u++){var p=s[u];if(p.getAttribute(\\"src\\")==t||p.getAttribute(\\"data-webpack\\")==a){n=p;break}}n||(i=!0,(n=document.createElement(\\"script\\")).charset=\\"utf-8\\",n.timeout=120,o.nc&&n.setAttribute(\\"nonce\\",o.nc),n.setAttribute(\\"data-webpack\\",a),n.src=t),e[t]=[r];var c=r=>{c=()=>{},n.onerror=n.onload=null,clearTimeout(l);var o=e[t];delete e[t],n.parentNode.removeChild(n),o&&o.forEach(e=>e(r))},l=setTimeout(()=>{c({type:\\"timeout\\",target:n})},12e4);n.onerror=n.onload=c,i&&document.head.appendChild(n)}},o.r=e=>{\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},o.p=\\"\\",(()=>{var e={988:0};o.f.j=(t,r)=>{var a=o.o(e,t)?e[t]:void 0;if(0!==a)if(a)r.push(a[2]);else{var n=new Promise((r,o)=>{a=e[t]=[r,o]});r.push(a[2]=n);var i=o.p+o.u(t),s=new Error;o.l(i,r=>{if(o.o(e,t)&&(0!==(a=e[t])&&(e[t]=void 0),a)){var n=r&&(\\"load\\"===r.type?\\"missing\\":r.type),i=r&&r.target&&r.target.src;s.message=\\"Loading chunk \\"+t+\\" failed.\\\\n(\\"+n+\\": \\"+i+\\")\\",s.name=\\"ChunkLoadError\\",s.type=n,s.request=i,a[1](s)}},\\"chunk-\\"+t)}};var t=window.webpackJsonpterser_webpack_plugin=window.webpackJsonpterser_webpack_plugin||[],r=t.push.bind(t);t.push=function(t){for(var r,n,i=t[0],s=t[1],u=t[3],p=0,c=[];p<i.length;p++)n=i[p],o.o(e,n)&&e[n]&&c.push(e[n][0]),e[n]=0;for(r in s)o.o(s,r)&&(o.m[r]=s[r]);for(u&&u(o),a&&a(t);c.length;)c.shift()()};var a=r})(),o.e(598).then(o.bind(o,598)).then(()=>{console.log(\\"Good\\")})})();",
"importExport.4dbd083d0540b53c8bf4.js": "(()=>{\\"use strict\\"})();",
"js.d1bc0c3718c3f8ac4e41.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)})();",
"mjs.2b79abcd1a51a2d3d42f.js": "(()=>{var r={631: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}(631)})();",
}
`;

Expand Down Expand Up @@ -322,6 +322,7 @@ Object {
"entryOption": 1,
"environment": 0,
"failed": 0,
"finishMake": 0,
"infrastructureLog": 0,
"initialize": 0,
"invalid": 1,
Expand Down Expand Up @@ -435,6 +436,7 @@ Object {
"entryOption": 1,
"environment": 0,
"failed": 0,
"finishMake": 0,
"infrastructureLog": 0,
"initialize": 0,
"invalid": 1,
Expand Down Expand Up @@ -473,6 +475,28 @@ exports[`TerserPlugin should work when some of assets do not contain source maps

exports[`TerserPlugin should work when some of assets do not contain source maps: warnings 1`] = `Array []`;

exports[`TerserPlugin should work with "asset" module type: assets 1`] = `
Object {
"emitted.js": "console.log(\\"HERE\\");",
"main.js": "(()=>{\\"use strict\\";var r,t={484:(r,t,e)=>{r.exports=e.p+\\"emitted.js\\"}},e={};function o(r){if(e[r])return e[r].exports;var s=e[r]={exports:{}};return t[r](s,s.exports,o),s.exports}o.p=\\"\\",r=o(484),console.log(12,r)})();",
}
`;

exports[`TerserPlugin should work with "asset" module type: errors 1`] = `Array []`;

exports[`TerserPlugin should work with "asset" module type: warnings 1`] = `Array []`;

exports[`TerserPlugin should work with "file-loader": assets 1`] = `
Object {
"main.js": "(()=>{\\"use strict\\";var t={p:\\"\\"};const s=t.p+\\"test/fixtures/emitted.js\\";console.log(12,s)})();",
"test/fixtures/emitted.js": "console.log(\\"HERE\\");",
}
`;

exports[`TerserPlugin should work with "file-loader": errors 1`] = `Array []`;

exports[`TerserPlugin should work with "file-loader": warnings 1`] = `Array []`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: assets 1`] = `
Object {
"one.js": "",
Expand Down

0 comments on commit 3c67023

Please sign in to comment.