From fea6f201488d5abc5aa695085ea329353adca547 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Fri, 8 Jan 2021 21:44:29 +0300 Subject: [PATCH] feat: optimize JS assets added later by plugins (#373) --- src/index.js | 15 ++++++++-- test/TerserPlugin.test.js | 16 ++++++++++ test/__snapshots__/TerserPlugin.test.js.snap | 11 +++++++ test/helpers/EmitNewAsset.js | 31 ++++++++++++++++++++ test/helpers/index.js | 2 ++ 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 test/helpers/EmitNewAsset.js diff --git a/src/index.js b/src/index.js index 710a05b9..90dbf202 100644 --- a/src/index.js +++ b/src/index.js @@ -202,11 +202,17 @@ class TerserPlugin { .filter((name) => { const { info } = compilation.getAsset(name); - // Skip double minimize assets from child compilation - if (info.minimized) { + if ( + // Skip double minimize assets from child compilation + info.minimized || + // Skip minimizing for extracted comments assets + info.extractedComments + ) { return false; } + console.log(info); + if ( !compiler.webpack.ModuleFilenameHelpers.matchObject.bind( // eslint-disable-next-line no-undefined @@ -552,7 +558,9 @@ class TerserPlugin { }; } - compilation.emitAsset(commentsFilename, extractedCommentsSource); + compilation.emitAsset(commentsFilename, extractedCommentsSource, { + extractedComments: true, + }); return { commentsFilename, @@ -626,6 +634,7 @@ class TerserPlugin { name: pluginName, stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, + additionalAssets: true, }, (assets) => this.optimize(compiler, compilation, assets, { diff --git a/test/TerserPlugin.test.js b/test/TerserPlugin.test.js index 1948fe54..ba86514b 100644 --- a/test/TerserPlugin.test.js +++ b/test/TerserPlugin.test.js @@ -14,6 +14,7 @@ import { ModifyExistingAsset, compile, countPlugins, + EmitNewAsset, getCompiler, getErrors, getWarnings, @@ -1608,4 +1609,19 @@ describe("TerserPlugin", () => { expect(getErrors(stats)).toMatchSnapshot("errors"); expect(getWarnings(stats)).toMatchSnapshot("warnings"); }); + + it("should run plugin against assets added later by plugins", async () => { + const compiler = getCompiler({ + entry: path.resolve(__dirname, "./fixtures/entry.js"), + }); + + new TerserPlugin().apply(compiler); + new EmitNewAsset({ name: "newFile.js" }).apply(compiler); + + const stats = await compile(compiler); + + expect(readsAssets(compiler, stats)).toMatchSnapshot("assets"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + }); }); diff --git a/test/__snapshots__/TerserPlugin.test.js.snap b/test/__snapshots__/TerserPlugin.test.js.snap index eb3a27fb..9fce0a34 100644 --- a/test/__snapshots__/TerserPlugin.test.js.snap +++ b/test/__snapshots__/TerserPlugin.test.js.snap @@ -88,6 +88,17 @@ exports[`TerserPlugin should respect the hash options #1: errors 1`] = `Array [] exports[`TerserPlugin should respect the hash options #1: warnings 1`] = `Array []`; +exports[`TerserPlugin should run plugin against assets added later by plugins: 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)})();", + "newFile.js": "var foo=\\"bar\\",bar=\\"foo\\";", +} +`; + +exports[`TerserPlugin should run plugin against assets added later by plugins: errors 1`] = `Array []`; + +exports[`TerserPlugin should run plugin against assets added later by plugins: warnings 1`] = `Array []`; + exports[`TerserPlugin should work (without options): 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)})();", diff --git a/test/helpers/EmitNewAsset.js b/test/helpers/EmitNewAsset.js new file mode 100644 index 00000000..c951494b --- /dev/null +++ b/test/helpers/EmitNewAsset.js @@ -0,0 +1,31 @@ +export default class EmitNewAsset { + constructor(options = {}) { + this.options = options; + } + + apply(compiler) { + const pluginName = this.constructor.name; + + const { RawSource } = compiler.webpack.sources; + + compiler.hooks.compilation.tap(pluginName, (compilation) => { + compilation.hooks.processAssets.tap( + { + name: pluginName, + stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT, + }, + () => { + // eslint-disable-next-line no-param-reassign + compilation.emitAsset( + this.options.name, + new RawSource(` +var foo = 'bar'; + +var bar = 'foo'; + `) + ); + } + ); + }); + } +} diff --git a/test/helpers/index.js b/test/helpers/index.js index d9c05d08..113f7388 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -1,6 +1,7 @@ import BrokenCodePlugin from "./BrokenCodePlugin"; import compile from "./compile"; import countPlugins from "./countPlugins"; +import EmitNewAsset from "./EmitNewAsset"; import execute from "./execute"; import ExistingCommentsFile from "./ExistingCommentsFile"; import getCompiler from "./getCompiler"; @@ -15,6 +16,7 @@ export { BrokenCodePlugin, compile, countPlugins, + EmitNewAsset, ExistingCommentsFile, execute, getCompiler,