Skip to content

Commit

Permalink
feat: compress assets added later by plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Dec 10, 2020
1 parent efadca8 commit 5b8b356
Show file tree
Hide file tree
Showing 17 changed files with 549 additions and 408 deletions.
71 changes: 54 additions & 17 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.1.2",
"standard-version": "^9.0.0",
"webpack": "^5.9.0",
"webpack": "^5.10.0",
"workbox-webpack-plugin": "^6.0.0-alpha.3"
},
"keywords": [
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ class CompressionPlugin {
name: pluginName,
stage:
compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER,
additionalAssets: true,
},
(assets) => this.compress(compiler, compilation, assets)
);
Expand Down
28 changes: 25 additions & 3 deletions test/CompressionPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
compile,
CopyPluginWithAssetInfo,
ModifyExistingAsset,
EmitNewAsset,
getAssetsNameAndSize,
getCompiler,
getErrors,
Expand Down Expand Up @@ -171,9 +172,7 @@ describe("CompressionPlugin", () => {
const stringStats = stats.toString({ relatedAssets: true });
const printedCompressed = stringStats.match(/\[compressed]/g);

expect(printedCompressed ? printedCompressed.length : 0).toBe(
getCompiler.isWebpack4() ? 0 : 3
);
expect(printedCompressed ? printedCompressed.length : 0).toBe(3);
expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot("assets");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
Expand Down Expand Up @@ -407,4 +406,27 @@ describe("CompressionPlugin", () => {
resolve();
});
});

it("should run plugin against assets added later by plugins", async () => {
const compiler = getCompiler(
"./number.js",
{},
{
output: {
path: path.resolve(__dirname, "./outputs"),
filename: "[name].js",
chunkFilename: "[id].js",
},
}
);

new CompressionPlugin({ minRatio: 10 }).apply(compiler);
new EmitNewAsset({ name: "newFile.js" }).apply(compiler);

const stats = await compile(compiler);

expect(getAssetsNameAndSize(stats, compiler)).toMatchSnapshot("assets");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
});
Loading

0 comments on commit 5b8b356

Please sign in to comment.