Repository: webpack/webpack
File: lib/NodeStuffPlugin.js (JavaScript)

Code before:

						parser.hooks.evaluateIdentifier
							.for(dirname)
							.tap(PLUGIN_NAME, (expr) => {
								if (!parser.state.module) return;
								return evaluateToString(
									/** @type {string} */
									(parser.state.module.context)
								)(expr);
							});
					}

					parser.hooks.expression
						.for("require.extensions")
						.tap(
							PLUGIN_NAME,
							expressionIsUnsupported(
								parser,
								"require.extensions is not supported by webpack. Use a loader instead."
							)
						);
				};

				normalModuleFactory.hooks.parser
					.for(JAVASCRIPT_MODULE_TYPE_AUTO)
					.tap(PLUGIN_NAME, (parser, parserOptions) => {
						handler(parser, parserOptions, {
							dirname: "__dirname",
							filename: "__filename"
						});
						handler(parser, parserOptions, {
							dirname: "import.meta.dirname",
							filename: "import.meta.filename"
						});
					});
				normalModuleFactory.hooks.parser
					.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
					.tap(PLUGIN_NAME, (parser, parserOptions) => {
						handler(parser, parserOptions, {
							dirname: "__dirname",
							filename: "__filename"
						});
					});
				normalModuleFactory.hooks.parser
					.for(JAVASCRIPT_MODULE_TYPE_ESM)
					.tap(PLUGIN_NAME, (parser, parserOptions) => {
						handler(parser, parserOptions, {
							dirname: "import.meta.dirname",
							filename: "import.meta.filename"
						});
					});

Review comment (suggestion):
Using regexp to replace the last `/file.ext` to get a right value for dirname

Code after:
					) {
						parser.hooks.expression
							.for(dirname)
							.tap(PLUGIN_NAME, toConstantDependency(parser, dirname));
					}

					if (localOptions.__dirname) {
						switch (localOptions.__dirname) {
							case "mock":
								setConstant(dirname, "/", "__webpack_dirname__");
								break;
							case "warn-mock":
								setConstant(
									dirname,
									"/",
									"__webpack_dirname__",
									"__dirname is a Node.js feature and isn't available in browsers."
								);
								break;
							case "node-module": {
								const importMetaName = compilation.outputOptions.importMetaName;

								setUrlModuleConstant(
									dirname,
									(functionName) =>
										`${functionName}(${importMetaName}.url.replace(/\\/(?:[^\\/]*)$/, ""))`,
									"dirname"
								);
								break;
							}
							case "eval-only":
								// Keep `import.meta.dirname` in the source code for the ES module output and replace `__dirname` on `import.meta.dirname`
								if (compilation.outputOptions.module) {
									const { importMetaName } = compilation.outputOptions;

									parser.hooks.expression
										.for(dirname)
										.tap(
											PLUGIN_NAME,
											toConstantDependency(parser, `${importMetaName}.dirname`)
										);
								}
								// Replace `import.meta.dirname` with `__dirname` for the non-ES module output
								else if (dirname === "import.meta.dirname") {
									parser.hooks.expression
										.for(dirname)
										.tap(
											PLUGIN_NAME,
											toConstantDependency(parser, "__dirname")
										);
								}