WXL
3 天以前 3bd962a6d7f61239c020e2dbbeb7341e5b842dd1
node_modules/webpack/lib/NodeStuffPlugin.js
@@ -36,6 +36,7 @@
class NodeStuffPlugin {
   /**
    * Creates an instance of NodeStuffPlugin.
    * @param {NodeOptions} options options
    */
   constructor(options) {
@@ -43,7 +44,7 @@
   }
   /**
    * Apply the plugin
    * Applies the plugin by registering its hooks on the compiler.
    * @param {Compiler} compiler the compiler instance
    * @returns {void}
    */
@@ -63,12 +64,14 @@
            );
            /**
             * Processes the provided parser.
             * @param {JavascriptParser} parser the parser
             * @param {NodeOptions} nodeOptions options
             * @returns {void}
             */
            const globalHandler = (parser, nodeOptions) => {
               /**
                * Returns const dependency.
                * @param {Expression} expr expression
                * @returns {ConstDependency} const dependency
                */
@@ -116,6 +119,7 @@
            const hooks = ImportMetaPlugin.getCompilationHooks(compilation);
            /**
             * Sets module constant.
             * @param {JavascriptParser} parser the parser
             * @param {"__filename" | "__dirname" | "import.meta.filename" | "import.meta.dirname"} expressionName expression name
             * @param {(module: NormalModule) => string} fn function
@@ -149,6 +153,7 @@
            };
            /**
             * Sets cached module constant.
             * @param {JavascriptParser} parser the parser
             * @param {"__filename" | "__dirname" | "import.meta.filename" | "import.meta.dirname"} expressionName expression name
             * @param {(module: NormalModule) => string} fn function
@@ -207,6 +212,7 @@
            };
            /**
             * Updates constant using the provided parser.
             * @param {JavascriptParser} parser the parser
             * @param {"__filename" | "__dirname" | "import.meta.filename" | "import.meta.dirname"} expressionName expression name
             * @param {string} value value
@@ -230,6 +236,7 @@
               );
            /**
             * Sets url module constant.
             * @param {JavascriptParser} parser the parser
             * @param {"__filename" | "__dirname" | "import.meta.filename" | "import.meta.dirname"} expressionName expression name
             * @param {"dirname" | "filename"} property property
@@ -245,45 +252,46 @@
               parser.hooks.expression
                  .for(expressionName)
                  .tap(PLUGIN_NAME, (expr) => {
                     // We use `CachedConstDependency` because of `eval` devtool, there is no `import.meta` inside `eval()`
                     const { importMetaName, environment, module } =
                        compilation.outputOptions;
                     if (
                        module &&
                        importMetaName === "import.meta" &&
                        (expressionName === "import.meta.filename" ||
                           expressionName === "import.meta.dirname") &&
                        environment.importMetaDirnameAndFilename
                     ) {
                        return true;
                     }
                     // Generate `import.meta.dirname` and `import.meta.filename` when:
                     // - they are supported by the environment
                     // - it is a universal target, because we can't use `import mod from "node:url"; ` at the top file
                     const dep =
                     if (
                        environment.importMetaDirnameAndFilename ||
                        (compiler.platform.web === null &&
                           compiler.platform.node === null &&
                           module)
                           ? new ConstDependency(
                                 `${importMetaName}.${property}`,
                                 /** @type {Range} */
                                 (expr.range)
                              )
                           : new ExternalModuleDependency(
                                 "url",
                                 [
                                    {
                                       name: "fileURLToPath",
                                       value: URL_MODULE_CONSTANT_FUNCTION_NAME
                                    }
                                 ],
                                 undefined,
                                 `${URL_MODULE_CONSTANT_FUNCTION_NAME}(${value()})`,
                                 /** @type {Range} */ (expr.range),
                                 `__webpack_${property}__`
                              );
                     ) {
                        const dep = new CachedConstDependency(
                           `${importMetaName}.${property}`,
                           /** @type {Range} */
                           (expr.range),
                           `__webpack_${property}__`,
                           CachedConstDependency.PLACE_CHUNK
                        );
                        dep.loc = /** @type {DependencyLocation} */ (expr.loc);
                        parser.state.module.addPresentationalDependency(dep);
                        return;
                     }
                     const dep = new ExternalModuleDependency(
                        "url",
                        [
                           {
                              name: "fileURLToPath",
                              value: URL_MODULE_CONSTANT_FUNCTION_NAME
                           }
                        ],
                        undefined,
                        `${URL_MODULE_CONSTANT_FUNCTION_NAME}(${value()})`,
                        /** @type {Range} */ (expr.range),
                        `__webpack_${property}__`,
                        ExternalModuleDependency.PLACE_CHUNK
                     );
                     dep.loc = /** @type {DependencyLocation} */ (expr.loc);
                     parser.state.module.addPresentationalDependency(dep);
@@ -300,20 +308,25 @@
                           compilation.outputOptions;
                        if (
                           module &&
                           importMetaName === "import.meta" &&
                           (expressionName === "import.meta.filename" ||
                              expressionName === "import.meta.dirname") &&
                           environment.importMetaDirnameAndFilename
                           environment.importMetaDirnameAndFilename ||
                           (compiler.platform.web === null &&
                              compiler.platform.node === null &&
                              module)
                        ) {
                           return `${property}: ${importMetaName}.${property},`;
                           const dep = new CachedConstDependency(
                              `${importMetaName}.${property}`,
                              null,
                              `__webpack_${property}__`,
                              CachedConstDependency.PLACE_CHUNK
                           );
                           dep.loc = /** @type {DependencyLocation} */ (
                              usingProperty.loc
                           );
                           parser.state.module.addPresentationalDependency(dep);
                           return `${property}: __webpack_${property}__,`;
                        }
                        if (environment.importMetaDirnameAndFilename) {
                           return `${property}: ${importMetaName}.${property},`;
                        }
                        const dep = new ExternalModuleInitFragmentDependency(
                        const dep = new ExternalModuleDependency(
                           "url",
                           [
                              {
@@ -321,19 +334,24 @@
                                 value: URL_MODULE_CONSTANT_FUNCTION_NAME
                              }
                           ],
                           undefined
                           undefined,
                           `${URL_MODULE_CONSTANT_FUNCTION_NAME}(${value()})`,
                           null,
                           `__webpack_${property}__`,
                           ExternalModuleDependency.PLACE_CHUNK
                        );
                        dep.loc = /** @type {DependencyLocation} */ (usingProperty.loc);
                        parser.state.module.addPresentationalDependency(dep);
                        return `${property}: ${URL_MODULE_CONSTANT_FUNCTION_NAME}(${value()}),`;
                        return `${property}: __webpack_${property}__,`;
                     }
                  });
               }
            };
            /**
             * Dirname and filename handler.
             * @param {JavascriptParser} parser the parser
             * @param {NodeOptions} nodeOptions options
             * @param {{ dirname: "__dirname" | "import.meta.dirname", filename: "__filename" | "import.meta.filename" }} identifiers options
@@ -506,6 +524,7 @@
            };
            /**
             * Handles the hook callback for this code path.
             * @param {JavascriptParser} parser the parser
             * @param {JavascriptParserOptions} parserOptions the javascript parser options
             * @param {boolean} a true when we need to handle `__filename` and `__dirname`, otherwise false