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} * @returns {void}
*/ */
apply(compiler) { 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( compiler.hooks.compilation.tap(
PLUGIN_NAME, PLUGIN_NAME,
(compilation, { normalModuleFactory }) => { (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"); const logger = compilation.getLogger("webpack.DefinePlugin");
compilation.dependencyTemplates.set( compilation.dependencyTemplates.set(
ConstDependency, ConstDependency,
@ -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,10 @@ class DotenvPlugin {
apply(compiler) { apply(compiler) {
/** @type {string[] | undefined} */ /** @type {string[] | undefined} */
let fileDependenciesCache; 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} */ ( const inputFileSystem = /** @type {InputFileSystem} */ (
compiler.inputFileSystem compiler.inputFileSystem
); );
@ -296,10 +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 || {});
const DefinePlugin = compiler.webpack.DefinePlugin; definePlugin.definitions = definitions;
new DefinePlugin(definitions).apply(compiler);
fileDependenciesCache = fileDependencies; fileDependenciesCache = fileDependencies;
callback(); callback();