@@ -9,6 +9,7 @@ const { evaluateToString } = require("../javascript/JavascriptParserHelpers");
99const RuntimeGlobals = require ( "../runtime/RuntimeGlobals" ) ;
1010const { recordOptimizationBailout } = require ( "../util/buildDiagnostics" ) ;
1111const formatLocation = require ( "../util/formatLocation" ) ;
12+ const memoize = require ( "../util/memoize" ) ;
1213const { propertyAccess } = require ( "../util/property" ) ;
1314const CommonJsExportRequireDependency = require ( "./CommonJsExportRequireDependency" ) ;
1415const CommonJsExportsDependency = require ( "./CommonJsExportsDependency" ) ;
@@ -21,6 +22,10 @@ const HarmonyExports = require("./HarmonyExports");
2122const ModuleDecoratorDependency = require ( "./ModuleDecoratorDependency" ) ;
2223const RuntimeRequirementsDependency = require ( "./RuntimeRequirementsDependency" ) ;
2324
25+ const getRequireBindingTag = memoize (
26+ ( ) => require ( "./CommonJsImportsParserPlugin" ) . REQUIRE_BINDING_TAG
27+ ) ;
28+
2429const FUNCTION_PROPERTY_DROP_HEAD =
2530 CommonJsObjectExportDependency . FUNCTION_PROPERTY_DROP_HEAD ;
2631
@@ -45,7 +50,6 @@ const FUNCTION_PROPERTY_DROP_HEAD =
4550 */
4651/** @import { ExportInfoName } from "../graph/Dependency" */
4752/** @import { BuildInfo } from "../module/Module" */
48- /** @import BasicEvaluatedExpression from "../javascript/BasicEvaluatedExpression" */
4953/**
5054 * @import JavascriptParser, {
5155 * Range,
@@ -63,6 +67,7 @@ const FUNCTION_PROPERTY_DROP_HEAD =
6367 * CommonJsObjectExportKind
6468 * } from "./CommonJsObjectExportDependency"
6569 */
70+ /** @import { RequireBindingData } from "./CommonJsImportsParserPlugin" */
6671/**
6772 * @import {
6873 * JavascriptModuleBuildMeta
@@ -131,6 +136,9 @@ const getReexportOfPropertyDescriptor = (expr) => {
131136 ( getFn . type === "FunctionExpression" ||
132137 getFn . type === "ArrowFunctionExpression" )
133138 ) {
139+ // A parameter or the function's own name would shadow a `const` binding the body names.
140+ if ( getFn . params . length > 0 ) return ;
141+ if ( getFn . type === "FunctionExpression" && getFn . id ) return ;
134142 // Arrow with expression body: `() => require("./x")`.
135143 if ( getFn . body . type !== "BlockStatement" ) {
136144 return { expr : /** @type {Expression } */ ( getFn . body ) , getter : true } ;
@@ -181,10 +189,10 @@ const isFalsyLiteral = (expr) => {
181189} ;
182190
183191/**
184- * Parses require call.
192+ * Parses a ` require()` call, or a `const` binding of one, under a static member chain .
185193 * @param {JavascriptParser } parser the parser
186194 * @param {Expression } expr expression
187- * @returns {{ argument: BasicEvaluatedExpression , ids: ExportInfoName[] } | undefined } parsed call
195+ * @returns {{ request: string , ids: ExportInfoName[] } | undefined } parsed call
188196 */
189197const parseRequireCall = ( parser , expr ) => {
190198 /** @type {ExportInfoName[] } */
@@ -202,6 +210,13 @@ const parseRequireCall = (parser, expr) => {
202210 }
203211 expr = expr . object ;
204212 }
213+ if ( expr . type === "Identifier" ) {
214+ const binding =
215+ /** @type {RequireBindingData | undefined } */
216+ ( parser . getTagData ( expr . name , getRequireBindingTag ( ) ) ) ;
217+ if ( ! binding ) return ;
218+ return { request : binding . request , ids : ids . reverse ( ) } ;
219+ }
205220 if ( expr . type !== "CallExpression" || expr . arguments . length !== 1 ) return ;
206221 const callee = expr . callee ;
207222 if (
@@ -213,7 +228,11 @@ const parseRequireCall = (parser, expr) => {
213228 const arg = expr . arguments [ 0 ] ;
214229 if ( arg . type === "SpreadElement" ) return ;
215230 const argValue = parser . evaluateExpression ( arg ) ;
216- return { argument : argValue , ids : ids . reverse ( ) } ;
231+ if ( ! argValue . isString ( ) ) return ;
232+ return {
233+ request : /** @type {string } */ ( argValue . string ) ,
234+ ids : ids . reverse ( )
235+ } ;
217236} ;
218237
219238/**
@@ -472,7 +491,6 @@ class CommonJsExportsParserPlugin {
472491 const requireCall = parseRequireCall ( parser , expr . right ) ;
473492 if (
474493 requireCall &&
475- requireCall . argument . isString ( ) &&
476494 ( members . length === 0 || members [ 0 ] !== "__esModule" )
477495 ) {
478496 enableStructuredExports ( ) ;
@@ -483,7 +501,7 @@ class CommonJsExportsParserPlugin {
483501 null ,
484502 base ,
485503 members ,
486- /** @type { string } */ ( requireCall . argument . string ) ,
504+ requireCall . request ,
487505 requireCall . ids ,
488506 ! parser . isStatementLevelExpression ( expr )
489507 ) ;
@@ -674,13 +692,13 @@ class CommonJsExportsParserPlugin {
674692 const reexport = getReexportOfPropertyDescriptor ( descArg ) ;
675693 const requireCall =
676694 reexport && parseRequireCall ( parser , reexport . expr ) ;
677- if ( reexport && requireCall && requireCall . argument . isString ( ) ) {
695+ if ( reexport && requireCall ) {
678696 const dep = new CommonJsExportRequireDependency (
679697 /** @type {Range } */ ( expr . range ) ,
680698 /** @type {Range } */ ( reexport . expr . range ) ,
681699 `Object.defineProperty(${ exportsArg . identifier } )` ,
682700 [ property ] ,
683- /** @type { string } */ ( requireCall . argument . string ) ,
701+ requireCall . request ,
684702 requireCall . ids ,
685703 false
686704 ) ;
0 commit comments