Skip to content

Commit

Permalink
Merge pull request #14366 from webpack/perf/define-hash
Browse files Browse the repository at this point in the history
use a hash instead of a list to track DefinePlugin dependency
  • Loading branch information
sokra committed Sep 29, 2021
2 parents e78403d + 5f22bff commit b7f3828
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 13 additions & 8 deletions lib/DefinePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
evaluateToString,
toConstantDependency
} = require("./javascript/JavascriptParserHelpers");
const { provide } = require("./util/MapHelpers");
const createHash = require("./util/createHash");

/** @typedef {import("estree").Expression} Expression */
/** @typedef {import("./Compiler")} Compiler */
Expand Down Expand Up @@ -286,12 +286,11 @@ class DefinePlugin {
);
const { runtimeTemplate } = compilation;

const mainValue = /** @type {Set<string>} */ (
provide(
compilation.valueCacheVersions,
VALUE_DEP_MAIN,
() => new Set()
)
const mainHash = createHash(compilation.outputOptions.hashFunction);
mainHash.update(
/** @type {string} */ (
compilation.valueCacheVersions.get(VALUE_DEP_MAIN)
) || ""
);

/**
Expand All @@ -300,6 +299,7 @@ class DefinePlugin {
* @returns {void}
*/
const handler = parser => {
const mainValue = compilation.valueCacheVersions.get(VALUE_DEP_MAIN);
parser.hooks.program.tap("DefinePlugin", () => {
const { buildInfo } = parser.state.module;
if (!buildInfo.valueDependencies)
Expand Down Expand Up @@ -565,7 +565,7 @@ class DefinePlugin {
const code = definitions[key];
const version = toCacheVersion(code);
const name = VALUE_DEP_PREFIX + prefix + key;
mainValue.add(name);
mainHash.update("|" + prefix + key);
const oldVersion = compilation.valueCacheVersions.get(name);
if (oldVersion === undefined) {
compilation.valueCacheVersions.set(name, version);
Expand All @@ -589,6 +589,11 @@ class DefinePlugin {
};

walkDefinitionsForValues(definitions, "");

compilation.valueCacheVersions.set(
VALUE_DEP_MAIN,
/** @type {string} */ (mainHash.digest("hex").slice(0, 8))
);
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions test/watchCases/cache/add-defines/3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import b from "./b";
import c from "./c";

it("should invalidate modules when properties are added/removed from the DefinePlugin", () => {
expect(a).toEqual([1, 2]);
expect(a).toEqual([1, 3]);
expect(b).toEqual([undefined, 3]);
expect(c).toEqual([3, 2]);
expect(c).toEqual([3, 3]);
});

5 comments on commit b7f3828

@m4r71n
Copy link

@m4r71n m4r71n commented on b7f3828 Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sokra,
since version 5.56.0 our development build (with watch) always rebuilds scss and css on changing a js file:

assets by status 1.22 MiB [cached] 6 assets
assets by status 17.9 MiB [emitted]
  assets by path *.js 17.9 MiB
    assets by info 97.1 KiB [hmr]
      asset 718d266-dev-wps-hmr.js 87.3 KiB [emitted] [hmr] (name: dev)
      asset 718d266-dependencies-wps-hmr.js 8.91 KiB [emitted] [hmr] (name: dependencies) (id hint: totalegal)
      asset 718d266-runtime-wps-hmr.js 868 bytes [emitted] [hmr] (name: runtime)
    asset dependencies.js 9.23 MiB [emitted] (name: dependencies) (id hint: totalegal)
    asset dev.js 8.54 MiB [emitted] (name: dev)
    asset runtime.js 55.4 KiB [emitted] (name: runtime)
  assets by path *.json 88 bytes
    asset runtime-718d266-wps-hmr.json 52 bytes [emitted] [hmr]
    asset 0517b57ceff480b7308e-718d266-wps-hmr.json 36 bytes [emitted] [hmr]
Entrypoint dev 18.4 MiB = 7 assets
runtime modules 158 KiB 92 modules
orphan modules 1.21 MiB [orphan] 82 modules
cached modules 3.17 KiB [cached] 15 modules
modules by path ./ 3.65 MiB (javascript) 480 KiB (css/mini-extract)
  modules by path ./node_modules/ 3.37 MiB (javascript) 35.1 KiB (css/mini-extract)
    javascript modules 3.37 MiB 931 modules
    css modules 35.1 KiB 5 modules
  modules by path ./js/ 290 KiB 24 modules
  modules by path ./sass/*.scss 387 bytes (javascript) 445 KiB (css/mini-extract)
    ./sass/screen.scss 387 bytes [built] [code generated]
    css ./node_modules/css-loader/dist/cjs.js!./node_modules/postcss-loader/dist/cjs.js!./node_modules/sass-loader/dist/cjs.js??ruleSet[1].rules[1].use[3]!./sass/screen.scss 445 KiB [built]
modules by path ../../../../../../../folders/modules/ 2.15 MiB 318 modules
0 (webpack 5.56.0) compiled successfully in 14134 ms

With version 5.55.1 only the js is rebuild:

assets by status 10.5 MiB [cached] 7 assets
assets by path . 8.68 MiB
  assets by path *.js 8.68 MiB
    assets by chunk 8.62 MiB (name: dev)
      asset dev.js 8.54 MiB [emitted] (name: dev)
      asset 6caecda-dev-wps-hmr.js 85.8 KiB [emitted] [hmr] (name: dev)
    assets by chunk 56.3 KiB (name: runtime)
      asset runtime.js 55.4 KiB [emitted] (name: runtime)
      asset 6caecda-runtime-wps-hmr.js 868 bytes [emitted] [hmr] (name: runtime)
  assets by path *.json 59 bytes
    asset runtime-6caecda-wps-hmr.json 37 bytes [emitted] [hmr]
    asset 0517b57ceff480b7308e-6caecda-wps-hmr.json 22 bytes [emitted] [hmr]
Entrypoint dev 18.4 MiB = 6 assets
cached modules 5.91 MiB (javascript) 480 KiB (css/mini-extract) 32 KiB (runtime) [cached] 1465 modules
runtime modules 37.3 KiB 20 modules
../../../../../../../folders/modules/folder/new-message/TextArea.js 24 KiB [built] [code generated]
0 (webpack 5.55.1) compiled successfully in 1353 ms

I've also tried version 5.57.1 but it is the same behaviour as with 5.56.0. Do you need additional information?

@alexander-akait
Copy link
Member

@alexander-akait alexander-akait commented on b7f3828 Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m4r71n Can you provide small repo? Does it happens for 5.55.0?

@m4r71n
Copy link

@m4r71n m4r71n commented on b7f3828 Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-akait
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m4r71n bug in webpack-plugin-serve, please report them or migrate on webpack-dev-server

@m4r71n
Copy link

@m4r71n m4r71n commented on b7f3828 Oct 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Please sign in to comment.