fix: respect `import.meta` name everywhere (#19726)

This commit is contained in:
Alexander Akait 2025-07-23 21:45:32 +03:00 committed by GitHub
parent 745d8c8af4
commit 2532bcfed5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 89 additions and 7 deletions

View File

@ -118,7 +118,8 @@ class NodeStuffPlugin {
.tap(PLUGIN_NAME, (expr) => {
const dep = new CachedConstDependency(
JSON.stringify(fn(parser.state.module)),
/** @type {Range} */ (expr.range),
/** @type {Range} */
(expr.range),
expressionName
);
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
@ -186,12 +187,17 @@ class NodeStuffPlugin {
"__filename is a Node.js feature and isn't available in browsers."
);
break;
case "node-module":
case "node-module": {
const importMetaName =
/** @type {string} */
(compilation.outputOptions.importMetaName);
setUrlModuleConstant(
"__filename",
(functionName) => `${functionName}(import.meta.url)`
(functionName) => `${functionName}(${importMetaName}.url)`
);
break;
}
case true:
setModuleConstant("__filename", (module) =>
relative(
@ -223,13 +229,18 @@ class NodeStuffPlugin {
"__dirname is a Node.js feature and isn't available in browsers."
);
break;
case "node-module":
case "node-module": {
const importMetaName =
/** @type {string} */
(compilation.outputOptions.importMetaName);
setUrlModuleConstant(
"__dirname",
(functionName) =>
`${functionName}(import.meta.url + "/..").slice(0, -1)`
`${functionName}(${importMetaName}.url + "/..").slice(0, -1)`
);
break;
}
case true:
setModuleConstant("__dirname", (module) =>
relative(
@ -246,7 +257,8 @@ class NodeStuffPlugin {
.tap(PLUGIN_NAME, (expr) => {
if (!parser.state.module) return;
return evaluateToString(
/** @type {string} */ (parser.state.module.context)
/** @type {string} */
(parser.state.module.context)
)(expr);
});
}

View File

@ -0,0 +1,6 @@
import path from "path";
it("should use custom name", () => {
expect(__dirname).toBe(__STATS__.outputPath);
expect(__filename).toBe(path.join(__STATS__.outputPath, "./bundle0.js"));
});

View File

@ -0,0 +1,10 @@
"use strict";
/** @type {import("../../../../").Configuration} */
module.exports = {
target: "node",
node: {
__filename: "eval-only",
__dirname: "eval-only"
}
};

View File

@ -0,0 +1,6 @@
import path from "path";
it("should use custom name", () => {
expect(__dirname).toBe(__STATS__.outputPath);
expect(__filename).toBe(path.join(__STATS__.outputPath, "./bundle0.mjs"));
});

View File

@ -0,0 +1,12 @@
"use strict";
const path = require("path");
const { pathToFileURL } = require("url");
module.exports = {
moduleScope(scope, options) {
scope.custom = {
url: pathToFileURL(path.join(options.output.path, "bundle0.mjs"))
};
}
};

View File

@ -0,0 +1,5 @@
"use strict";
const supportsNodePrefix = require("../../../helpers/supportsNodePrefix");
module.exports = () => supportsNodePrefix();

View File

@ -0,0 +1,17 @@
"use strict";
/** @type {import("../../../../").Configuration} */
module.exports = {
target: "node",
experiments: {
outputModule: true
},
output: {
module: true,
importMetaName: "custom"
},
node: {
__filename: "node-module",
__dirname: "node-module"
}
};

View File

@ -1,3 +1,5 @@
"use strict";
module.exports = () => !process.version.startsWith("v10.");
const supportsNodePrefix = require("../../../helpers/supportsNodePrefix");
module.exports = () => supportsNodePrefix();

View File

@ -0,0 +1,12 @@
"use strict";
module.exports = function supportsNodePrefix() {
try {
eval("require('node:path')");
return true;
} catch (_err) {
// Ignore
}
return false;
};