From 7f28701411a2dc633c7f8b9e8d4b4771bf05ec11 Mon Sep 17 00:00:00 2001 From: Ryan Roemer Date: Tue, 20 Oct 2020 06:42:28 -0700 Subject: [PATCH] Bugfixes for new webpack5 integration (#58) - 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 --- HISTORY.md | 8 ++++ lib/stats-writer-plugin.js | 11 +++-- package.json | 2 +- test/func.spec.js | 44 +++++++++++++++++-- .../webpack5/webpack.config.contenthash.js | 43 ++++++++++++++++++ 5 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 test/scenarios/webpack5/webpack.config.contenthash.js diff --git a/HISTORY.md b/HISTORY.md index fbb872c..6badbd3 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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`. diff --git a/lib/stats-writer-plugin.js b/lib/stats-writer-plugin.js index c112823..712599f 100644 --- a/lib/stats-writer-plugin.js +++ b/lib/stats-writer-plugin.js @@ -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) ); diff --git a/package.json b/package.json index fda8ec0..8929fb5 100644 --- a/package.json +++ b/package.json @@ -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": {}, diff --git a/test/func.spec.js b/test/func.spec.js index 1cd60fc..a7a996e 100644 --- a/test/func.spec.js +++ b/test/func.spec.js @@ -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] }) } + ), {}); }; @@ -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 = {}; diff --git a/test/scenarios/webpack5/webpack.config.contenthash.js b/test/scenarios/webpack5/webpack.config.contenthash.js new file mode 100644 index 0000000..a55335b --- /dev/null +++ b/test/scenarios/webpack5/webpack.config.contenthash.js @@ -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 + } + } +};