| | |
| | | |
| | | class NodeStuffPlugin { |
| | | /** |
| | | * Creates an instance of NodeStuffPlugin. |
| | | * @param {NodeOptions} options options |
| | | */ |
| | | constructor(options) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Apply the plugin |
| | | * Applies the plugin by registering its hooks on the compiler. |
| | | * @param {Compiler} compiler the compiler instance |
| | | * @returns {void} |
| | | */ |
| | |
| | | ); |
| | | |
| | | /** |
| | | * 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 |
| | | */ |
| | |
| | | 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 |
| | |
| | | }; |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | }; |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | ); |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | 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); |
| | | |
| | |
| | | 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", |
| | | [ |
| | | { |
| | |
| | | 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 |
| | |
| | | }; |
| | | |
| | | /** |
| | | * 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 |