@@ -11,7 +11,9 @@ const {
1111const RuntimeGlobals = require ( "../../runtime/RuntimeGlobals" ) ;
1212const { recordOptimizationBailout } = require ( "../../util/buildDiagnostics" ) ;
1313const formatLocation = require ( "../../util/formatLocation" ) ;
14+ const memoize = require ( "../../util/memoize" ) ;
1415const { propertyAccess } = require ( "../../util/property" ) ;
16+
1517const RuntimeRequirementsDependency = require ( "../core/RuntimeRequirementsDependency" ) ;
1618const ESMExports = require ( "../esm/ESMExports" ) ;
1719const DynamicExports = require ( "../helpers/DynamicExports" ) ;
@@ -23,6 +25,10 @@ const CommonJsRequireDependency = require("./CommonJsRequireDependency");
2325const CommonJsSelfReferenceDependency = require ( "./CommonJsSelfReferenceDependency" ) ;
2426const ModuleDecoratorDependency = require ( "./ModuleDecoratorDependency" ) ;
2527
28+ const getRequireBindingTag = memoize (
29+ ( ) => require ( "./CommonJsImportsParserPlugin" ) . REQUIRE_BINDING_TAG
30+ ) ;
31+
2632const FUNCTION_PROPERTY_DROP_HEAD =
2733 CommonJsObjectExportDependency . FUNCTION_PROPERTY_DROP_HEAD ;
2834
@@ -47,7 +53,7 @@ const FUNCTION_PROPERTY_DROP_HEAD =
4753 */
4854/** @import { ExportInfoName } from "../../graph/Dependency" */
4955/** @import { BuildInfo } from "../../module/Module" */
50- /** @import BasicEvaluatedExpression from "../../javascript/BasicEvaluatedExpression" */
56+
5157/**
5258 * @import JavascriptParser, {
5359 * Range,
@@ -65,6 +71,7 @@ const FUNCTION_PROPERTY_DROP_HEAD =
6571 * CommonJsObjectExportKind
6672 * } from "./CommonJsObjectExportDependency"
6773 */
74+ /** @import { RequireBindingData } from "./CommonJsImportsParserPlugin" */
6875/**
6976 * @import {
7077 * JavascriptModuleBuildMeta
@@ -133,6 +140,9 @@ const getReexportOfPropertyDescriptor = (expr) => {
133140 ( getFn . type === "FunctionExpression" ||
134141 getFn . type === "ArrowFunctionExpression" )
135142 ) {
143+ // A parameter or the function's own name would shadow a `const` binding the body names.
144+ if ( getFn . params . length > 0 ) return ;
145+ if ( getFn . type === "FunctionExpression" && getFn . id ) return ;
136146 // Arrow with expression body: `() => require("./x")`.
137147 if ( getFn . body . type !== "BlockStatement" ) {
138148 return { expr : /** @type {Expression } */ ( getFn . body ) , getter : true } ;
@@ -183,10 +193,10 @@ const isFalsyLiteral = (expr) => {
183193} ;
184194
185195/**
186- * Parses require call.
196+ * Parses a ` require()` call, or a `const` binding of one, under a static member chain .
187197 * @param {JavascriptParser } parser the parser
188198 * @param {Expression } expr expression
189- * @returns {{ argument: BasicEvaluatedExpression , ids: ExportInfoName[] } | undefined } parsed call
199+ * @returns {{ request: string , ids: ExportInfoName[] } | undefined } parsed call
190200 */
191201const parseRequireCall = ( parser , expr ) => {
192202 /** @type {ExportInfoName[] } */
@@ -204,6 +214,13 @@ const parseRequireCall = (parser, expr) => {
204214 }
205215 expr = expr . object ;
206216 }
217+ if ( expr . type === "Identifier" ) {
218+ const binding =
219+ /** @type {RequireBindingData | undefined } */
220+ ( parser . getTagData ( expr . name , getRequireBindingTag ( ) ) ) ;
221+ if ( ! binding ) return ;
222+ return { request : binding . request , ids : ids . reverse ( ) } ;
223+ }
207224 if ( expr . type !== "CallExpression" || expr . arguments . length !== 1 ) return ;
208225 const callee = expr . callee ;
209226 if (
@@ -215,7 +232,11 @@ const parseRequireCall = (parser, expr) => {
215232 const arg = expr . arguments [ 0 ] ;
216233 if ( arg . type === "SpreadElement" ) return ;
217234 const argValue = parser . evaluateExpression ( arg ) ;
218- return { argument : argValue , ids : ids . reverse ( ) } ;
235+ if ( ! argValue . isString ( ) ) return ;
236+ return {
237+ request : /** @type {string } */ ( argValue . string ) ,
238+ ids : ids . reverse ( )
239+ } ;
219240} ;
220241
221242/**
@@ -474,7 +495,6 @@ class CommonJsExportsParserPlugin {
474495 const requireCall = parseRequireCall ( parser , expr . right ) ;
475496 if (
476497 requireCall &&
477- requireCall . argument . isString ( ) &&
478498 ( members . length === 0 || members [ 0 ] !== "__esModule" )
479499 ) {
480500 enableStructuredExports ( ) ;
@@ -485,7 +505,7 @@ class CommonJsExportsParserPlugin {
485505 null ,
486506 base ,
487507 members ,
488- /** @type { string } */ ( requireCall . argument . string ) ,
508+ requireCall . request ,
489509 requireCall . ids ,
490510 ! parser . isStatementLevelExpression ( expr )
491511 ) ;
@@ -676,13 +696,13 @@ class CommonJsExportsParserPlugin {
676696 const reexport = getReexportOfPropertyDescriptor ( descArg ) ;
677697 const requireCall =
678698 reexport && parseRequireCall ( parser , reexport . expr ) ;
679- if ( reexport && requireCall && requireCall . argument . isString ( ) ) {
699+ if ( reexport && requireCall ) {
680700 const dep = new CommonJsExportRequireDependency (
681701 /** @type {Range } */ ( expr . range ) ,
682702 /** @type {Range } */ ( reexport . expr . range ) ,
683703 `Object.defineProperty(${ exportsArg . identifier } )` ,
684704 [ property ] ,
685- /** @type { string } */ ( requireCall . argument . string ) ,
705+ requireCall . request ,
686706 requireCall . ids ,
687707 false
688708 ) ;
0 commit comments