WXL
4 天以前 3bd962a6d7f61239c020e2dbbeb7341e5b842dd1
node_modules/webpack/lib/DefinePlugin.js
@@ -5,6 +5,7 @@
"use strict";
const { SyncWaterfallHook } = require("tapable");
const {
   JAVASCRIPT_MODULE_TYPE_AUTO,
   JAVASCRIPT_MODULE_TYPE_DYNAMIC,
@@ -32,33 +33,38 @@
/** @typedef {import("./javascript/JavascriptParser").DestructuringAssignmentProperties} DestructuringAssignmentProperties */
/** @typedef {import("./javascript/JavascriptParser").Range} Range */
/** @typedef {import("./logging/Logger").Logger} Logger */
/** @typedef {import("./Compilation")} Compilation */
/** @typedef {null | undefined | RegExp | EXPECTED_FUNCTION | string | number | boolean | bigint | undefined} CodeValuePrimitive */
/** @typedef {RecursiveArrayOrRecord<CodeValuePrimitive | RuntimeValue>} CodeValue */
/**
 * Defines the runtime value options type used by this module.
 * @typedef {object} RuntimeValueOptions
 * @property {string[]=} fileDependencies
 * @property {string[]=} contextDependencies
 * @property {string[]=} missingDependencies
 * @property {string[]=} buildDependencies
 * @property {string| (() => string)=} version
 * @property {string | (() => string)=} version
 */
/** @typedef {(value: { module: NormalModule, key: string, readonly version: ValueCacheVersion }) => CodeValuePrimitive} GeneratorFn */
class RuntimeValue {
   /**
    * Creates an instance of RuntimeValue.
    * @param {GeneratorFn} fn generator function
    * @param {true | string[] | RuntimeValueOptions=} options options
    */
   constructor(fn, options) {
      /** @type {GeneratorFn} */
      this.fn = fn;
      if (Array.isArray(options)) {
         options = {
            fileDependencies: options
         };
      }
      /** @type {true | RuntimeValueOptions} */
      this.options = options || {};
   }
@@ -67,6 +73,7 @@
   }
   /**
    * Returns code.
    * @param {JavascriptParser} parser the parser
    * @param {ValueCacheVersions} valueCacheVersions valueCacheVersions
    * @param {string} key the defined key
@@ -124,6 +131,7 @@
}
/**
 * Returns used keys.
 * @param {DestructuringAssignmentProperties | undefined} properties properties
 * @returns {Set<string> | undefined} used keys
 */
@@ -136,7 +144,8 @@
/** @typedef {boolean | undefined | null} AsiSafe */
/**
 * @param {EXPECTED_ANY[] | {[k: string]: EXPECTED_ANY}} obj obj
 * Returns code converted to string that evaluates.
 * @param {EXPECTED_ANY[] | { [k: string]: EXPECTED_ANY }} obj obj
 * @param {JavascriptParser} parser Parser
 * @param {ValueCacheVersions} valueCacheVersions valueCacheVersions
 * @param {string} key the defined key
@@ -156,6 +165,7 @@
   asiSafe,
   objKeys
) => {
   /** @type {string} */
   let code;
   const arr = Array.isArray(obj);
   if (arr) {
@@ -180,7 +190,7 @@
      code = `{${keys
         .map((key) => {
            const code = obj[key];
            return `${JSON.stringify(key)}:${toCode(
            return `${key === "__proto__" ? '["__proto__"]' : JSON.stringify(key)}:${toCode(
               code,
               parser,
               valueCacheVersions,
@@ -282,6 +292,7 @@
};
/**
 * Returns result.
 * @param {CodeValue} code code
 * @returns {string | undefined} result
 */
@@ -330,16 +341,45 @@
);
const WEBPACK_REQUIRE_IDENTIFIER_REGEXP = new RegExp(RuntimeGlobals.require);
/**
 * Defines the define plugin hooks type used by this module.
 * @typedef {object} DefinePluginHooks
 * @property {SyncWaterfallHook<[Record<string, CodeValue>]>} definitions
 */
/** @typedef {Record<string, CodeValue>} Definitions */
/** @type {WeakMap<Compilation, DefinePluginHooks>} */
const compilationHooksMap = new WeakMap();
class DefinePlugin {
   /**
    * Returns the attached hooks.
    * @param {Compilation} compilation the compilation
    * @returns {DefinePluginHooks} the attached hooks
    */
   static getCompilationHooks(compilation) {
      let hooks = compilationHooksMap.get(compilation);
      if (hooks === undefined) {
         hooks = {
            definitions: new SyncWaterfallHook(["definitions"])
         };
         compilationHooksMap.set(compilation, hooks);
      }
      return hooks;
   }
   /**
    * Create a new define plugin
    * @param {Record<string, CodeValue>} definitions A map of global object definitions
    * @param {Definitions} definitions A map of global object definitions
    */
   constructor(definitions) {
      /** @type {Definitions} */
      this.definitions = definitions;
   }
   /**
    * Returns runtime value.
    * @param {GeneratorFn} fn generator function
    * @param {true | string[] | RuntimeValueOptions=} options options
    * @returns {RuntimeValue} runtime value
@@ -349,7 +389,7 @@
   }
   /**
    * Apply the plugin
    * Applies the plugin by registering its hooks on the compiler.
    * @param {Compiler} compiler the compiler instance
    * @returns {void}
    */
@@ -358,6 +398,12 @@
         PLUGIN_NAME,
         (compilation, { normalModuleFactory }) => {
            const definitions = this.definitions;
            const hooks = DefinePlugin.getCompilationHooks(compilation);
            hooks.definitions.tap(PLUGIN_NAME, (previousDefinitions) => ({
               ...previousDefinitions,
               ...definitions
            }));
            /**
             * @type {Map<string, Set<string>>}
@@ -382,11 +428,12 @@
            );
            /**
             * Handler
             * Handles the hook callback for this code path.
             * @param {JavascriptParser} parser Parser
             * @returns {void}
             */
            const handler = (parser) => {
               /** @type {Set<string>} */
               const hooked = new Set();
               const mainValue =
                  /** @type {ValueCacheVersion} */
@@ -402,6 +449,7 @@
               });
               /**
                * Adds value dependency.
                * @param {string} key key
                */
               const addValueDependency = (key) => {
@@ -417,6 +465,7 @@
               };
               /**
                * With value dependency.
                * @template T
                * @param {string} key key
                * @param {(expression: Expression) => T} fn fn
@@ -430,8 +479,8 @@
                  };
               /**
                * Walk definitions
                * @param {Record<string, CodeValue>} definitions Definitions map
                * Processes the provided definition.
                * @param {Definitions} definitions Definitions map
                * @param {string} prefix Prefix string
                * @returns {void}
                */
@@ -445,7 +494,7 @@
                        !(code instanceof RegExp)
                     ) {
                        walkDefinitions(
                           /** @type {Record<string, CodeValue>} */ (code),
                           /** @type {Definitions} */ (code),
                           `${prefix + key}.`
                        );
                        applyObjectDefine(prefix + key, code);
@@ -457,7 +506,7 @@
               };
               /**
                * Apply define key
                * Processes the provided prefix.
                * @param {string} prefix Prefix
                * @param {string} key Key
                * @returns {void}
@@ -504,8 +553,8 @@
                                 if (destructed === undefined) {
                                    return;
                                 }
                                 /** @type {Record<string, CodeValue>} */
                                 const obj = {};
                                 /** @type {Definitions} */
                                 const obj = Object.create(null);
                                 const finalSet = finalByNestedKey.get(nested);
                                 for (const { id } of destructed) {
                                    const fullKey = `${nested}.${id}`;
@@ -542,7 +591,7 @@
               };
               /**
                * Apply Code
                * Processes the provided key.
                * @param {string} key Key
                * @param {CodeValue} code Code
                * @returns {void}
@@ -665,7 +714,7 @@
               };
               /**
                * Apply Object
                * Processes the provided key.
                * @param {string} key Key
                * @param {object} obj Object
                * @returns {void}
@@ -750,8 +799,8 @@
               .tap(PLUGIN_NAME, handler);
            /**
             * Walk definitions
             * @param {Record<string, CodeValue>} definitions Definitions map
             * Processes the provided definition.
             * @param {Definitions} definitions Definitions map
             * @param {string} prefix Prefix string
             * @returns {void}
             */
@@ -779,7 +828,7 @@
                     !(code instanceof RegExp)
                  ) {
                     walkDefinitionsForValues(
                        /** @type {Record<string, CodeValue>} */ (code),
                        /** @type {Definitions} */ (code),
                        `${prefix + key}.`
                     );
                  }
@@ -787,11 +836,13 @@
            };
            /**
             * @param {Record<string, CodeValue>} definitions Definitions map
             * Walk definitions for keys.
             * @param {Definitions} definitions Definitions map
             * @returns {void}
             */
            const walkDefinitionsForKeys = (definitions) => {
               /**
                * Adds the provided map to the define plugin.
                * @param {Map<string, Set<string>>} map Map
                * @param {string} key key
                * @param {string} value v