Compare commits

...

7 Commits

Author SHA1 Message Date
xiaoxiaojx 0020eb88b0 update 2025-10-04 12:33:47 +08:00
xiaoxiaojx 3f486d2162 update 2025-10-04 12:22:59 +08:00
xiaoxiaojx a88a269d3a update 2025-10-04 11:36:34 +08:00
xiaoxiaojx 077eea1a50 update 2025-10-04 11:17:09 +08:00
xiaoxiaojx 595f13fd70 update 2025-10-04 10:57:05 +08:00
xiaoxiaojx 3405021731 update 2025-10-04 10:35:21 +08:00
xiaoxiaojx 6f986b86f6 update 2025-10-04 10:28:27 +08:00
2 changed files with 20 additions and 22 deletions

View File

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

View File

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