Skip to content

Commit

Permalink
Bugfixes for new webpack5 integration (#58)
Browse files Browse the repository at this point in the history
- Switch from `compilation` to `thisCompilation` hook to capture compilation for later use. Fixes #57, fixes #55 
- Switch `processAssets` stage from `PROCESS_ASSETS_STAGE_SUMMARIZE` to `PROCESS_ASSETS_STAGE_REPORT`. Fixes #56
  • Loading branch information
ryan-roemer authored Oct 20, 2020
1 parent 9d63bea commit 7f28701
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
8 changes: 8 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
History
=======

## UNRELEASED

* *Bug*: Fix multiple stats output issue.
[#55](https://github.com/FormidableLabs/webpack-stats-plugin/issues/55)
[#57](https://github.com/FormidableLabs/webpack-stats-plugin/issues/57)
* *Bug*: Change `processAssets` hook stage to `PROCESS_ASSETS_STAGE_REPORT` to correctly get hashed asset names.
[#56](https://github.com/FormidableLabs/webpack-stats-plugin/issues/56)

## 1.0.0

* *Feature*: Support `webpack@5`.
Expand Down
11 changes: 8 additions & 3 deletions lib/stats-writer-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ class StatsWriterPlugin {

apply(compiler) {
if (compiler.hooks) {
compiler.hooks.compilation.tap("stats-writer-plugin", (compilation) => {
compiler.hooks.thisCompilation.tap("stats-writer-plugin", (compilation) => {
if (compilation.hooks.processAssets) {
// Modern: `processAssets` is one of the last hooks before frozen assets.
// See: https://webpack.js.org/api/compilation-hooks/#processassets
// We choose `PROCESS_ASSETS_STAGE_REPORT` which is the last possible
// stage after which to emit.
//
// See:
// - https://webpack.js.org/api/compilation-hooks/#processassets
// - https://github.com/FormidableLabs/webpack-stats-plugin/issues/56
compilation.hooks.processAssets.tapPromise(
{
name: "stats-writer-plugin",
stage: compilation.constructor.PROCESS_ASSETS_STAGE_SUMMARIZE
stage: compilation.constructor.PROCESS_ASSETS_STAGE_REPORT
},
() => this.emitStats(compilation)
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"test:run": "mocha 'test/**/*.spec.js'",
"test:clean": "rm -rf test/scenarios/webpack*/build*",
"test:build:single": "node node_modules/webpack${VERS}/index.js --config test/scenarios/webpack${VERS}/webpack.config${WP_EXTRA}.js",
"test:build": "builder envs test:build:single '[{\"VERS\":1},{\"VERS\":2},{\"VERS\":3},{\"VERS\":4},{\"VERS\":5}]' --buffer",
"test:build": "builder envs test:build:single '[{\"VERS\":1},{\"VERS\":2},{\"VERS\":3},{\"VERS\":4},{\"VERS\":5},{\"VERS\":5,\"WP_EXTRA\":\".contenthash\"}]' --buffer",
"check": "yarn run lint && yarn run test"
},
"dependencies": {},
Expand Down
44 changes: 40 additions & 4 deletions test/func.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,11 @@ const readBuild = async (buildDir) => {
files.map((file) => fs.readFile(path.join(__dirname, buildDir, file)))
);

return data.reduce((m, v, i) => ({
...m,
[files[i]]: normalizeFile({ data: v.toString(),
name: files[i] }) }),
return data.reduce((m, v, i) => Object.assign(
m,
{ [files[i]]: normalizeFile({ data: v.toString(),
name: files[i] }) }
),
{});
};

Expand Down Expand Up @@ -242,6 +243,41 @@ describe("failures", function () {
});
});

// Specific tests.
describe("fixtures", () => {
describe("webpack5", () => {
const webpack = "webpack5";
let buildFiles;
const actuals = {};

before(async () => {
const buildDir = path.join(__dirname, "scenarios", webpack, "build");
buildFiles = await fs.readdir(buildDir);

await Promise.all(buildFiles
.filter((name) => (/\.json$/).test(name))
.map((name) => fs.readFile(path.join(buildDir, name))
// eslint-disable-next-line promise/always-return
.then((buf) => {
actuals[name] = JSON.parse(buf.toString());
})
)
);
});

// https://github.com/FormidableLabs/webpack-stats-plugin/issues/56
it("matches the correct hashed file name in stats object", () => {
const assetNames = buildFiles.filter((name) => (/^contenthash\./).test(name));
expect(assetNames).to.have.length(1);

const assetName = assetNames[0];
expect(actuals["stats-contenthash.json"])
.to.have.nested.property("entrypoints.main.assets[0].name")
.that.equals(assetName);
});
});
});

(async () => {
const expecteds = await readBuild("expected");
const actuals = {};
Expand Down
43 changes: 43 additions & 0 deletions test/scenarios/webpack5/webpack.config.contenthash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@


"use strict";

/**
* Regression test for: https://github.com/FormidableLabs/webpack-stats-plugin/issues/56
*/
const path = require("path");
const { StatsWriterPlugin } = require("../../../index");

module.exports = {
mode: "production",
context: __dirname,
entry: {
main: "../../src/main.js"
},
output: {
path: path.join(__dirname, "build"),
publicPath: "/website-o-doom/",
filename: "contenthash.[contenthash].main.js"
},
devtool: false,
plugins: [
new StatsWriterPlugin({
filename: "stats-contenthash.json",
fields: ["entrypoints"]
})
],
optimization: {
splitChunks: {
cacheGroups: {
vendors: {
priority: -10,
test: /[\\/]node_modules[\\/]/
}
},
chunks: "async",
minChunks: 1,
minSize: 30000,
name: false
}
}
};

0 comments on commit 7f28701

Please sign in to comment.