Skip to content

Commit 43903e1

Browse files
authored
perf: skip collecting guard when exports presence mode is none (#20433)
1 parent 5a3e6dc commit 43903e1

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

.changeset/lazy-ideas-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Revert part of the createRequire generation behavior for `require("node:...")` to keep compatibility with those modules exports, e.g. `const EventEmitter = require("node:events");`.

.changeset/silver-plants-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Skip guard collection when exports-presence mode is disabled to improve parsing performance.

lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"use strict";
77

88
const makeSerializable = require("../util/makeSerializable");
9+
const { ExportPresenceModes } = require("./HarmonyImportDependency");
910
const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
1011
const { ImportPhase } = require("./ImportPhase");
1112

@@ -44,7 +45,7 @@ class HarmonyEvaluatedImportSpecifierDependency extends HarmonyImportSpecifierDe
4445
ids,
4546
name,
4647
range,
47-
false,
48+
ExportPresenceModes.NONE,
4849
ImportPhase.Evaluation,
4950
attributes,
5051
[]

lib/dependencies/HarmonyImportDependency.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const ModuleDependency = require("./ModuleDependency");
2929
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
3030
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
3131

32-
/** @typedef {0 | 1 | 2 | 3 | false} ExportPresenceMode */
32+
/** @typedef {0 | 1 | 2 | 3} ExportPresenceMode */
3333

3434
const ExportPresenceModes = {
3535
NONE: /** @type {ExportPresenceMode} */ (0),

lib/dependencies/HarmonyImportDependencyParserPlugin.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
8181
*/
8282
constructor(options) {
8383
this.options = options;
84+
/** @type {ExportPresenceMode} */
8485
this.exportPresenceMode =
8586
options.importExportsPresence !== undefined
8687
? ExportPresenceModes.fromUserOption(options.importExportsPresence)
@@ -107,7 +108,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
107108
parser.getTagData(harmonySettings.name, harmonySpecifierGuardTag)
108109
);
109110
return data && data.guards && data.guards.has(getMembersKey(ids))
110-
? false
111+
? ExportPresenceModes.NONE
111112
: this.exportPresenceMode;
112113
}
113114

@@ -651,17 +652,19 @@ module.exports = class HarmonyImportDependencyParserPlugin {
651652
}
652653
};
653654

654-
parser.hooks.collectGuards.tap(PLUGIN_NAME, (expression) => {
655-
if (parser.scope.isAsmJs) return;
656-
/** @type {Guards} */
657-
const guards = new Map();
658-
collect(expression, guards, true);
655+
if (this.exportPresenceMode !== ExportPresenceModes.NONE) {
656+
parser.hooks.collectGuards.tap(PLUGIN_NAME, (expression) => {
657+
if (parser.scope.isAsmJs) return;
658+
/** @type {Guards} */
659+
const guards = new Map();
660+
collect(expression, guards, true);
659661

660-
if (guards.size === 0) return;
661-
return (walk) => {
662-
withGuards(guards, walk);
663-
};
664-
});
662+
if (guards.size === 0) return;
663+
return (walk) => {
664+
withGuards(guards, walk);
665+
};
666+
});
667+
}
665668
}
666669
};
667670

types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5304,7 +5304,7 @@ type ExportModeType =
53045304
| "reexport-undefined"
53055305
| "normal-reexport"
53065306
| "dynamic-reexport";
5307-
type ExportPresenceMode = false | 0 | 1 | 2 | 3;
5307+
type ExportPresenceMode = 0 | 1 | 2 | 3;
53085308
declare interface ExportSpec {
53095309
/**
53105310
* the name of the export

0 commit comments

Comments
 (0)