Skip to content

Commit

Permalink
feat: optimize JS assets added later by plugins (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jan 8, 2021
1 parent e10b8b4 commit fea6f20
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/index.js
Expand Up @@ -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);

This comment has been minimized.

Copy link
@nilennoct

nilennoct Jan 9, 2021

Should this log be removed in next release?


if (
!compiler.webpack.ModuleFilenameHelpers.matchObject.bind(
// eslint-disable-next-line no-undefined
Expand Down Expand Up @@ -552,7 +558,9 @@ class TerserPlugin {
};
}

compilation.emitAsset(commentsFilename, extractedCommentsSource);
compilation.emitAsset(commentsFilename, extractedCommentsSource, {
extractedComments: true,
});

return {
commentsFilename,
Expand Down Expand Up @@ -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, {
Expand Down
16 changes: 16 additions & 0 deletions test/TerserPlugin.test.js
Expand Up @@ -14,6 +14,7 @@ import {
ModifyExistingAsset,
compile,
countPlugins,
EmitNewAsset,
getCompiler,
getErrors,
getWarnings,
Expand Down Expand Up @@ -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");
});
});
11 changes: 11 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap
Expand Up @@ -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)})();",
Expand Down
31 changes: 31 additions & 0 deletions 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';
`)
);
}
);
});
}
}
2 changes: 2 additions & 0 deletions 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";
Expand All @@ -15,6 +16,7 @@ export {
BrokenCodePlugin,
compile,
countPlugins,
EmitNewAsset,
ExistingCommentsFile,
execute,
getCompiler,
Expand Down

0 comments on commit fea6f20

Please sign in to comment.