mirror of https://github.com/webpack/webpack.git
feat: handle `import.meta.main` (#20065)
This commit is contained in:
parent
e61595200c
commit
8651ffc23a
|
|
@ -126,15 +126,6 @@ class CommonJsImportsParserPlugin {
|
||||||
});
|
});
|
||||||
|
|
||||||
// #region Unsupported
|
// #region Unsupported
|
||||||
parser.hooks.expression
|
|
||||||
.for("require.main")
|
|
||||||
.tap(
|
|
||||||
PLUGIN_NAME,
|
|
||||||
expressionIsUnsupported(
|
|
||||||
parser,
|
|
||||||
"require.main is not supported by webpack."
|
|
||||||
)
|
|
||||||
);
|
|
||||||
parser.hooks.call
|
parser.hooks.call
|
||||||
.for("require.main.require")
|
.for("require.main.require")
|
||||||
.tap(
|
.tap(
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ const {
|
||||||
JAVASCRIPT_MODULE_TYPE_AUTO,
|
JAVASCRIPT_MODULE_TYPE_AUTO,
|
||||||
JAVASCRIPT_MODULE_TYPE_ESM
|
JAVASCRIPT_MODULE_TYPE_ESM
|
||||||
} = require("../ModuleTypeConstants");
|
} = require("../ModuleTypeConstants");
|
||||||
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
||||||
const Template = require("../Template");
|
const Template = require("../Template");
|
||||||
const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression");
|
const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression");
|
||||||
const {
|
const {
|
||||||
|
|
@ -34,6 +35,7 @@ const ConstDependency = require("./ConstDependency");
|
||||||
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
||||||
/** @typedef {import("../javascript/JavascriptParser").Members} Members */
|
/** @typedef {import("../javascript/JavascriptParser").Members} Members */
|
||||||
/** @typedef {import("../javascript/JavascriptParser").DestructuringAssignmentProperty} DestructuringAssignmentProperty */
|
/** @typedef {import("../javascript/JavascriptParser").DestructuringAssignmentProperty} DestructuringAssignmentProperty */
|
||||||
|
/** @typedef {import("./ConstDependency").RawRuntimeRequirements} RawRuntimeRequirements */
|
||||||
|
|
||||||
const getCriticalDependencyWarning = memoize(() =>
|
const getCriticalDependencyWarning = memoize(() =>
|
||||||
require("./CriticalDependencyWarning")
|
require("./CriticalDependencyWarning")
|
||||||
|
|
@ -168,6 +170,9 @@ class ImportMetaPlugin {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @type {RawRuntimeRequirements} */
|
||||||
|
const runtimeRequirements = [];
|
||||||
|
|
||||||
let str = "";
|
let str = "";
|
||||||
for (const prop of referencedPropertiesInDestructuring) {
|
for (const prop of referencedPropertiesInDestructuring) {
|
||||||
const value = hooks.propertyInDestructuring.call(prop);
|
const value = hooks.propertyInDestructuring.call(prop);
|
||||||
|
|
@ -184,6 +189,14 @@ class ImportMetaPlugin {
|
||||||
case "webpack":
|
case "webpack":
|
||||||
str += `webpack: ${importMetaWebpackVersion()},`;
|
str += `webpack: ${importMetaWebpackVersion()},`;
|
||||||
break;
|
break;
|
||||||
|
case "main":
|
||||||
|
str += `main: ${RuntimeGlobals.moduleCache}[${RuntimeGlobals.entryModuleId}] === ${RuntimeGlobals.module},`;
|
||||||
|
runtimeRequirements.push(
|
||||||
|
RuntimeGlobals.moduleCache,
|
||||||
|
RuntimeGlobals.entryModuleId,
|
||||||
|
RuntimeGlobals.module
|
||||||
|
);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
str += `[${JSON.stringify(
|
str += `[${JSON.stringify(
|
||||||
prop.id
|
prop.id
|
||||||
|
|
@ -193,7 +206,8 @@ class ImportMetaPlugin {
|
||||||
}
|
}
|
||||||
const dep = new ConstDependency(
|
const dep = new ConstDependency(
|
||||||
`({${str}})`,
|
`({${str}})`,
|
||||||
/** @type {Range} */ (metaProperty.range)
|
/** @type {Range} */ (metaProperty.range),
|
||||||
|
runtimeRequirements
|
||||||
);
|
);
|
||||||
dep.loc = /** @type {DependencyLocation} */ (metaProperty.loc);
|
dep.loc = /** @type {DependencyLocation} */ (metaProperty.loc);
|
||||||
parser.state.module.addPresentationalDependency(dep);
|
parser.state.module.addPresentationalDependency(dep);
|
||||||
|
|
@ -237,18 +251,18 @@ class ImportMetaPlugin {
|
||||||
);
|
);
|
||||||
|
|
||||||
// import.meta.webpack
|
// import.meta.webpack
|
||||||
parser.hooks.typeof
|
|
||||||
.for("import.meta.webpack")
|
|
||||||
.tap(
|
|
||||||
PLUGIN_NAME,
|
|
||||||
toConstantDependency(parser, JSON.stringify("number"))
|
|
||||||
);
|
|
||||||
parser.hooks.expression
|
parser.hooks.expression
|
||||||
.for("import.meta.webpack")
|
.for("import.meta.webpack")
|
||||||
.tap(
|
.tap(
|
||||||
PLUGIN_NAME,
|
PLUGIN_NAME,
|
||||||
toConstantDependency(parser, importMetaWebpackVersion())
|
toConstantDependency(parser, importMetaWebpackVersion())
|
||||||
);
|
);
|
||||||
|
parser.hooks.typeof
|
||||||
|
.for("import.meta.webpack")
|
||||||
|
.tap(
|
||||||
|
PLUGIN_NAME,
|
||||||
|
toConstantDependency(parser, JSON.stringify("number"))
|
||||||
|
);
|
||||||
parser.hooks.evaluateTypeof
|
parser.hooks.evaluateTypeof
|
||||||
.for("import.meta.webpack")
|
.for("import.meta.webpack")
|
||||||
.tap(PLUGIN_NAME, evaluateToString("number"));
|
.tap(PLUGIN_NAME, evaluateToString("number"));
|
||||||
|
|
@ -256,6 +270,30 @@ class ImportMetaPlugin {
|
||||||
.for("import.meta.webpack")
|
.for("import.meta.webpack")
|
||||||
.tap(PLUGIN_NAME, evaluateToNumber(webpackVersion));
|
.tap(PLUGIN_NAME, evaluateToNumber(webpackVersion));
|
||||||
|
|
||||||
|
parser.hooks.expression
|
||||||
|
.for("import.meta.main")
|
||||||
|
.tap(
|
||||||
|
PLUGIN_NAME,
|
||||||
|
toConstantDependency(
|
||||||
|
parser,
|
||||||
|
`${RuntimeGlobals.moduleCache}[${RuntimeGlobals.entryModuleId}] === ${RuntimeGlobals.module}`,
|
||||||
|
[
|
||||||
|
RuntimeGlobals.moduleCache,
|
||||||
|
RuntimeGlobals.entryModuleId,
|
||||||
|
RuntimeGlobals.module
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
parser.hooks.typeof
|
||||||
|
.for("import.meta.main")
|
||||||
|
.tap(
|
||||||
|
PLUGIN_NAME,
|
||||||
|
toConstantDependency(parser, JSON.stringify("boolean"))
|
||||||
|
);
|
||||||
|
parser.hooks.evaluateTypeof
|
||||||
|
.for("import.meta.main")
|
||||||
|
.tap(PLUGIN_NAME, evaluateToString("boolean"));
|
||||||
|
|
||||||
// Unknown properties
|
// Unknown properties
|
||||||
parser.hooks.unhandledExpressionMemberChain
|
parser.hooks.unhandledExpressionMemberChain
|
||||||
.for("import.meta")
|
.for("import.meta")
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
export default "1";
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { main } from "./module.js";
|
||||||
|
|
||||||
|
it("should handle import.meta.main", async () => {
|
||||||
|
expect(import.meta.main).toBe(true);
|
||||||
|
expect(typeof import.meta.main).toBe("boolean");
|
||||||
|
|
||||||
|
// Just for test, nobody uses this in real code
|
||||||
|
await import(`./${typeof import.meta.main}.js`);
|
||||||
|
|
||||||
|
const { main: myMain } = import.meta;
|
||||||
|
expect(myMain).toBe(true);
|
||||||
|
|
||||||
|
expect(main).toBe(false);
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
const main = import.meta.main;
|
||||||
|
|
||||||
|
export { main };
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
/** @type {import("../../../../").Configuration} */
|
||||||
|
module.exports = {
|
||||||
|
target: "node",
|
||||||
|
optimization: {
|
||||||
|
concatenateModules: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
const { main } = require("./module");
|
||||||
|
|
||||||
|
it("should handle require.main", async () => {
|
||||||
|
expect(require.main === module).toBe(true);
|
||||||
|
|
||||||
|
expect(main).toBe(false);
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
const main = require.main === module;
|
||||||
|
|
||||||
|
module.exports = { main };
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
/** @type {import("../../../../").Configuration} */
|
||||||
|
module.exports = {
|
||||||
|
target: "node",
|
||||||
|
optimization: {
|
||||||
|
concatenateModules: false
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue