Merge pull request #14366 from webpack/perf/define-hash

use a hash instead of a list to track DefinePlugin dependency
This commit is contained in:
Tobias Koppers 2021-09-29 21:57:55 +02:00 committed by GitHub
commit b7f382878e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 10 deletions

View File

@ -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 */
@ -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)
) || ""
);
/**
@ -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)
@ -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);
@ -589,6 +589,11 @@ class DefinePlugin {
};
walkDefinitionsForValues(definitions, "");
compilation.valueCacheVersions.set(
VALUE_DEP_MAIN,
/** @type {string} */ (mainHash.digest("hex").slice(0, 8))
);
}
);
}

View File

@ -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]);
});