Skip to content

Commit 8651ffc

Browse files
feat: handle import.meta.main (#20065)
1 parent e615952 commit 8651ffc

File tree

9 files changed

+89
-14
lines changed

9 files changed

+89
-14
lines changed

lib/dependencies/CommonJsImportsParserPlugin.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,6 @@ class CommonJsImportsParserPlugin {
126126
});
127127

128128
// #region Unsupported
129-
parser.hooks.expression
130-
.for("require.main")
131-
.tap(
132-
PLUGIN_NAME,
133-
expressionIsUnsupported(
134-
parser,
135-
"require.main is not supported by webpack."
136-
)
137-
);
138129
parser.hooks.call
139130
.for("require.main.require")
140131
.tap(

lib/dependencies/ImportMetaPlugin.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
JAVASCRIPT_MODULE_TYPE_AUTO,
1414
JAVASCRIPT_MODULE_TYPE_ESM
1515
} = require("../ModuleTypeConstants");
16+
const RuntimeGlobals = require("../RuntimeGlobals");
1617
const Template = require("../Template");
1718
const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression");
1819
const {
@@ -34,6 +35,7 @@ const ConstDependency = require("./ConstDependency");
3435
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
3536
/** @typedef {import("../javascript/JavascriptParser").Members} Members */
3637
/** @typedef {import("../javascript/JavascriptParser").DestructuringAssignmentProperty} DestructuringAssignmentProperty */
38+
/** @typedef {import("./ConstDependency").RawRuntimeRequirements} RawRuntimeRequirements */
3739

3840
const getCriticalDependencyWarning = memoize(() =>
3941
require("./CriticalDependencyWarning")
@@ -168,6 +170,9 @@ class ImportMetaPlugin {
168170
return true;
169171
}
170172

173+
/** @type {RawRuntimeRequirements} */
174+
const runtimeRequirements = [];
175+
171176
let str = "";
172177
for (const prop of referencedPropertiesInDestructuring) {
173178
const value = hooks.propertyInDestructuring.call(prop);
@@ -184,6 +189,14 @@ class ImportMetaPlugin {
184189
case "webpack":
185190
str += `webpack: ${importMetaWebpackVersion()},`;
186191
break;
192+
case "main":
193+
str += `main: ${RuntimeGlobals.moduleCache}[${RuntimeGlobals.entryModuleId}] === ${RuntimeGlobals.module},`;
194+
runtimeRequirements.push(
195+
RuntimeGlobals.moduleCache,
196+
RuntimeGlobals.entryModuleId,
197+
RuntimeGlobals.module
198+
);
199+
break;
187200
default:
188201
str += `[${JSON.stringify(
189202
prop.id
@@ -193,7 +206,8 @@ class ImportMetaPlugin {
193206
}
194207
const dep = new ConstDependency(
195208
`({${str}})`,
196-
/** @type {Range} */ (metaProperty.range)
209+
/** @type {Range} */ (metaProperty.range),
210+
runtimeRequirements
197211
);
198212
dep.loc = /** @type {DependencyLocation} */ (metaProperty.loc);
199213
parser.state.module.addPresentationalDependency(dep);
@@ -237,17 +251,17 @@ class ImportMetaPlugin {
237251
);
238252

239253
// import.meta.webpack
240-
parser.hooks.typeof
254+
parser.hooks.expression
241255
.for("import.meta.webpack")
242256
.tap(
243257
PLUGIN_NAME,
244-
toConstantDependency(parser, JSON.stringify("number"))
258+
toConstantDependency(parser, importMetaWebpackVersion())
245259
);
246-
parser.hooks.expression
260+
parser.hooks.typeof
247261
.for("import.meta.webpack")
248262
.tap(
249263
PLUGIN_NAME,
250-
toConstantDependency(parser, importMetaWebpackVersion())
264+
toConstantDependency(parser, JSON.stringify("number"))
251265
);
252266
parser.hooks.evaluateTypeof
253267
.for("import.meta.webpack")
@@ -256,6 +270,30 @@ class ImportMetaPlugin {
256270
.for("import.meta.webpack")
257271
.tap(PLUGIN_NAME, evaluateToNumber(webpackVersion));
258272

273+
parser.hooks.expression
274+
.for("import.meta.main")
275+
.tap(
276+
PLUGIN_NAME,
277+
toConstantDependency(
278+
parser,
279+
`${RuntimeGlobals.moduleCache}[${RuntimeGlobals.entryModuleId}] === ${RuntimeGlobals.module}`,
280+
[
281+
RuntimeGlobals.moduleCache,
282+
RuntimeGlobals.entryModuleId,
283+
RuntimeGlobals.module
284+
]
285+
)
286+
);
287+
parser.hooks.typeof
288+
.for("import.meta.main")
289+
.tap(
290+
PLUGIN_NAME,
291+
toConstantDependency(parser, JSON.stringify("boolean"))
292+
);
293+
parser.hooks.evaluateTypeof
294+
.for("import.meta.main")
295+
.tap(PLUGIN_NAME, evaluateToString("boolean"));
296+
259297
// Unknown properties
260298
parser.hooks.unhandledExpressionMemberChain
261299
.for("import.meta")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "1";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { main } from "./module.js";
2+
3+
it("should handle import.meta.main", async () => {
4+
expect(import.meta.main).toBe(true);
5+
expect(typeof import.meta.main).toBe("boolean");
6+
7+
// Just for test, nobody uses this in real code
8+
await import(`./${typeof import.meta.main}.js`);
9+
10+
const { main: myMain } = import.meta;
11+
expect(myMain).toBe(true);
12+
13+
expect(main).toBe(false);
14+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const main = import.meta.main;
2+
3+
export { main };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: "node",
6+
optimization: {
7+
concatenateModules: false
8+
}
9+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { main } = require("./module");
2+
3+
it("should handle require.main", async () => {
4+
expect(require.main === module).toBe(true);
5+
6+
expect(main).toBe(false);
7+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const main = require.main === module;
2+
3+
module.exports = { main };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: "node",
6+
optimization: {
7+
concatenateModules: false
8+
}
9+
};

0 commit comments

Comments
 (0)