Skip to content

Commit 3c67023

Browse files
fix: do not crash on buffer assets
1 parent 0bf5158 commit 3c67023

13 files changed

+1306
-908
lines changed

package-lock.json

Lines changed: 63 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"schema-utils": "^2.6.6",
4949
"serialize-javascript": "^4.0.0",
5050
"source-map": "^0.6.1",
51-
"terser": "^4.6.13",
51+
"terser": "^4.8.0",
5252
"webpack-sources": "^1.4.3"
5353
},
5454
"devDependencies": {
@@ -66,9 +66,10 @@
6666
"eslint": "^7.2.0",
6767
"eslint-config-prettier": "^6.11.0",
6868
"eslint-plugin-import": "^2.21.2",
69+
"file-loader": "^6.0.0",
6970
"husky": "^4.2.5",
7071
"jest": "^26.0.1",
71-
"lint-staged": "^10.2.10",
72+
"lint-staged": "^10.2.11",
7273
"memfs": "^3.2.0",
7374
"npm-run-all": "^4.1.5",
7475
"prettier": "^2.0.5",

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ class TerserPlugin {
227227
inputSourceMap = null;
228228
}
229229

230+
if (Buffer.isBuffer(input)) {
231+
input = input.toString();
232+
}
233+
230234
// Handling comment extraction
231235
let commentsFilename = false;
232236

test/TerserPlugin.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,52 @@ describe('TerserPlugin', () => {
339339
expect(getWarnings(stats)).toMatchSnapshot('warnings');
340340
});
341341

342+
it('should work with "file-loader"', async () => {
343+
const compiler = getCompiler({
344+
entry: path.resolve(__dirname, 'fixtures/file-loader.js'),
345+
});
346+
347+
new TerserPlugin().apply(compiler);
348+
349+
const stats = await compile(compiler);
350+
351+
expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
352+
expect(getErrors(stats)).toMatchSnapshot('errors');
353+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
354+
});
355+
356+
it('should work with "asset" module type', async () => {
357+
if (getCompiler.isWebpack4()) {
358+
expect(true).toBe(true);
359+
} else {
360+
const compiler = getCompiler({
361+
entry: path.resolve(__dirname, 'fixtures/asset-resource.js'),
362+
experiments: {
363+
asset: true,
364+
},
365+
module: {
366+
rules: [
367+
{
368+
test: /emitted\.js$/i,
369+
type: 'asset/resource',
370+
generator: {
371+
filename: '[name][ext]',
372+
},
373+
},
374+
],
375+
},
376+
});
377+
378+
new TerserPlugin().apply(compiler);
379+
380+
const stats = await compile(compiler);
381+
382+
expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
383+
expect(getErrors(stats)).toMatchSnapshot('errors');
384+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
385+
}
386+
});
387+
342388
it('should work and respect "terser" errors (the "parallel" option is "true")', async () => {
343389
const compiler = getCompiler();
344390

test/__snapshots__/TerserPlugin.test.js.snap.webpack4

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,18 @@ exports[`TerserPlugin should work when some of assets do not contain source maps
575575

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

578+
exports[`TerserPlugin should work with "file-loader": assets 1`] = `
579+
Object {
580+
"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}]);",
581+
"test/fixtures/emitted.js": "console.log('HERE');
582+
",
583+
}
584+
`;
585+
586+
exports[`TerserPlugin should work with "file-loader": errors 1`] = `Array []`;
587+
588+
exports[`TerserPlugin should work with "file-loader": warnings 1`] = `Array []`;
589+
578590
exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: assets 1`] = `
579591
Object {
580592
"one.js": "",

test/__snapshots__/TerserPlugin.test.js.snap.webpack5

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ exports[`TerserPlugin should emit an error on a broken code in parallel mode: wa
104104

105105
exports[`TerserPlugin should regenerate hash: assets 1`] = `
106106
Object {
107-
"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\\"}}]);",
108-
"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\\")})})();",
109-
"importExport.e2f22a3d7de2bb697fe3.js": "(()=>{\\"use strict\\"})();",
110-
"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)})();",
111-
"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)})();",
107+
"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\\"}}]);",
108+
"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\\")})})();",
109+
"importExport.4dbd083d0540b53c8bf4.js": "(()=>{\\"use strict\\"})();",
110+
"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)})();",
111+
"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)})();",
112112
}
113113
`;
114114

@@ -322,6 +322,7 @@ Object {
322322
"entryOption": 1,
323323
"environment": 0,
324324
"failed": 0,
325+
"finishMake": 0,
325326
"infrastructureLog": 0,
326327
"initialize": 0,
327328
"invalid": 1,
@@ -435,6 +436,7 @@ Object {
435436
"entryOption": 1,
436437
"environment": 0,
437438
"failed": 0,
439+
"finishMake": 0,
438440
"infrastructureLog": 0,
439441
"initialize": 0,
440442
"invalid": 1,
@@ -473,6 +475,28 @@ exports[`TerserPlugin should work when some of assets do not contain source maps
473475

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

478+
exports[`TerserPlugin should work with "asset" module type: assets 1`] = `
479+
Object {
480+
"emitted.js": "console.log(\\"HERE\\");",
481+
"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)})();",
482+
}
483+
`;
484+
485+
exports[`TerserPlugin should work with "asset" module type: errors 1`] = `Array []`;
486+
487+
exports[`TerserPlugin should work with "asset" module type: warnings 1`] = `Array []`;
488+
489+
exports[`TerserPlugin should work with "file-loader": assets 1`] = `
490+
Object {
491+
"main.js": "(()=>{\\"use strict\\";var t={p:\\"\\"};const s=t.p+\\"test/fixtures/emitted.js\\";console.log(12,s)})();",
492+
"test/fixtures/emitted.js": "console.log(\\"HERE\\");",
493+
}
494+
`;
495+
496+
exports[`TerserPlugin should work with "file-loader": errors 1`] = `Array []`;
497+
498+
exports[`TerserPlugin should work with "file-loader": warnings 1`] = `Array []`;
499+
476500
exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: assets 1`] = `
477501
Object {
478502
"one.js": "",

0 commit comments

Comments
 (0)