mirror of https://github.com/webpack/webpack.git
fix: respect `import.meta` name everywhere (#19726)
This commit is contained in:
parent
745d8c8af4
commit
2532bcfed5
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"));
|
||||
});
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
"use strict";
|
||||
|
||||
/** @type {import("../../../../").Configuration} */
|
||||
module.exports = {
|
||||
target: "node",
|
||||
node: {
|
||||
__filename: "eval-only",
|
||||
__dirname: "eval-only"
|
||||
}
|
||||
};
|
||||
|
|
@ -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"));
|
||||
});
|
||||
|
|
@ -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"))
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
const supportsNodePrefix = require("../../../helpers/supportsNodePrefix");
|
||||
|
||||
module.exports = () => supportsNodePrefix();
|
||||
|
|
@ -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"
|
||||
}
|
||||
};
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
module.exports = () => !process.version.startsWith("v10.");
|
||||
const supportsNodePrefix = require("../../../helpers/supportsNodePrefix");
|
||||
|
||||
module.exports = () => supportsNodePrefix();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
"use strict";
|
||||
|
||||
module.exports = function supportsNodePrefix() {
|
||||
try {
|
||||
eval("require('node:path')");
|
||||
return true;
|
||||
} catch (_err) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
Loading…
Reference in New Issue