diff --git a/src/index.js b/src/index.js index d4aa5ec..f8335d9 100644 --- a/src/index.js +++ b/src/index.js @@ -596,7 +596,8 @@ class TerserPlugin { ); } - if (output.code) { + // Custom functions can return `undefined` or `null` + if (typeof output.code !== "undefined" && output.code !== null) { let shebang; if ( diff --git a/test/TerserPlugin.test.js b/test/TerserPlugin.test.js index 3227f43..be1122f 100644 --- a/test/TerserPlugin.test.js +++ b/test/TerserPlugin.test.js @@ -177,6 +177,20 @@ describe("TerserPlugin", () => { } }); + it("should work with empty files", async () => { + const compiler = getCompiler({ + entry: path.resolve(__dirname, "fixtures/empty.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 when some of assets do not contain source maps", async () => { const compiler = getCompiler({ devtool: "source-map", diff --git a/test/__snapshots__/TerserPlugin.test.js.snap b/test/__snapshots__/TerserPlugin.test.js.snap index 4f20bc0..4316ee9 100644 --- a/test/__snapshots__/TerserPlugin.test.js.snap +++ b/test/__snapshots__/TerserPlugin.test.js.snap @@ -1739,6 +1739,16 @@ exports[`TerserPlugin should work with child compilation: errors 1`] = `Array [] exports[`TerserPlugin should work with child compilation: warnings 1`] = `Array []`; +exports[`TerserPlugin should work with empty files: assets 1`] = ` +Object { + "main.js": "", +} +`; + +exports[`TerserPlugin should work with empty files: errors 1`] = `Array []`; + +exports[`TerserPlugin should work with empty files: warnings 1`] = `Array []`; + exports[`TerserPlugin should work with multi compiler mode with source maps: assets 1`] = ` Object { "main-1.js": "(()=>{var __webpack_modules__={921:module=>{eval(\\"{// foo\\\\n/* @preserve*/\\\\n// bar\\\\nconst a = (/* unused pure expression or super */ null && (2 + 2));\\\\n\\\\nmodule.exports = function Foo() {\\\\n const b = 2 + 2;\\\\n console.log(b + 1 + 2);\\\\n};\\\\n\\\\n\\\\n//# sourceURL=webpack://terser-webpack-plugin/./test/fixtures/entry.js?\\\\n}\\")}},__webpack_module_cache__={};function __webpack_require__(e){var _=__webpack_module_cache__[e];if(void 0!==_)return _.exports;var r=__webpack_module_cache__[e]={exports:{}};return __webpack_modules__[e](r,r.exports,__webpack_require__),r.exports}var __webpack_exports__=__webpack_require__(921)})();", @@ -3377,14 +3387,8 @@ exports[`TerserPlugin should work, extract comments in one file and use memory c 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": "/******/ (() => { // webpackBootstrap - -/******/ })() -;", - "two.js": "/******/ (() => { // webpackBootstrap - -/******/ })() -;", + "one.js": "", + "two.js": "", } `; @@ -3406,14 +3410,8 @@ exports[`TerserPlugin should write stdout and stderr of workers to stdout and st exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: assets 1`] = ` Object { - "one.js": "/******/ (() => { // webpackBootstrap - -/******/ })() -;", - "two.js": "/******/ (() => { // webpackBootstrap - -/******/ })() -;", + "one.js": "", + "two.js": "", } `;