This commit is contained in:
xiaoxiaojx 2025-10-04 12:22:59 +08:00
parent a88a269d3a
commit 3f486d2162
2 changed files with 10 additions and 15 deletions

View File

@ -354,8 +354,6 @@ class DefinePlugin {
* @returns {void} * @returns {void}
*/ */
apply(compiler) { apply(compiler) {
const definitions = this.definitions;
/** /**
* @type {Map<string, Set<string>>} * @type {Map<string, Set<string>>}
*/ */
@ -512,11 +510,11 @@ class DefinePlugin {
if ( if (
!finalSet || !finalSet ||
!finalSet.has(id) || !finalSet.has(id) ||
!definitions[fullKey] !this.definitions[fullKey]
) { ) {
return; return;
} }
obj[id] = definitions[fullKey]; obj[id] = this.definitions[fullKey];
} }
let strCode = stringifyObj( let strCode = stringifyObj(
obj, obj,
@ -736,7 +734,7 @@ class DefinePlugin {
); );
}; };
walkDefinitions(definitions, ""); walkDefinitions(this.definitions, "");
}; };
normalModuleFactory.hooks.parser normalModuleFactory.hooks.parser
@ -825,8 +823,8 @@ class DefinePlugin {
} }
}; };
walkDefinitionsForKeys(definitions); walkDefinitionsForKeys(this.definitions);
walkDefinitionsForValues(definitions, ""); walkDefinitionsForValues(this.definitions, "");
compilation.valueCacheVersions.set( compilation.valueCacheVersions.set(
VALUE_DEP_MAIN, VALUE_DEP_MAIN,

View File

@ -251,7 +251,7 @@ const getEnvFilesForMode = (inputFileSystem, dir, mode) => {
* @param {Record<string, string>} env environment variables * @param {Record<string, string>} env environment variables
* @returns {Record<string, string>} formatted definitions * @returns {Record<string, string>} formatted definitions
*/ */
const formatDefinitions = (env) => { const envToDefinitions = (env) => {
const definitions = /** @type {Record<string, string>} */ ({}); const definitions = /** @type {Record<string, string>} */ ({});
for (const [key, value] of Object.entries(env)) { for (const [key, value] of Object.entries(env)) {
@ -278,8 +278,8 @@ class DotenvPlugin {
apply(compiler) { apply(compiler) {
/** @type {string[] | undefined} */ /** @type {string[] | undefined} */
let fileDependenciesCache; let fileDependenciesCache;
/** @type {Record<string, string> | undefined} */ const definePlugin = new compiler.webpack.DefinePlugin({});
const definitionsCache = {}; definePlugin.apply(compiler);
compiler.hooks.beforeRun.tapAsync(PLUGIN_NAME, (_params, callback) => { compiler.hooks.beforeRun.tapAsync(PLUGIN_NAME, (_params, callback) => {
const inputFileSystem = /** @type {InputFileSystem} */ ( const inputFileSystem = /** @type {InputFileSystem} */ (
@ -298,8 +298,8 @@ class DotenvPlugin {
(err, env, fileDependencies) => { (err, env, fileDependencies) => {
if (err) return callback(err); if (err) return callback(err);
const definitions = formatDefinitions(env || {}); const definitions = envToDefinitions(env || {});
Object.assign(definitionsCache, definitions); definePlugin.definitions = definitions;
fileDependenciesCache = fileDependencies; fileDependenciesCache = fileDependencies;
callback(); callback();
@ -310,9 +310,6 @@ class DotenvPlugin {
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.fileDependencies.addAll(fileDependenciesCache || []); compilation.fileDependencies.addAll(fileDependenciesCache || []);
}); });
const DefinePlugin = compiler.webpack.DefinePlugin;
new DefinePlugin(definitionsCache).apply(compiler);
} }
/** /**