@@ -1797,8 +1797,8 @@ class ConcatenatedModule extends Module {
17971797 /** @type {Set<ConcatenatedModuleInfo | ExternalModuleInfo> } */
17981798 const neededEscapeNamespaceObjects = new Set ( ) ;
17991799
1800- // #region [1] ANALYSE: run each inner module's own codeGeneration; a
1801- // non-wrapped body is parsed and scope-analysed so [3] can rename its locals
1800+ // #region [1] ANALYSE: run each inner module's own codeGeneration and
1801+ // scope-analyse the body; a non-wrapped one keeps its scopes for [3]
18021802 for ( const info of moduleToInfoMap . values ( ) ) {
18031803 this . _analyseModule (
18041804 moduleToInfoMap ,
@@ -1895,8 +1895,18 @@ class ConcatenatedModule extends Module {
18951895 ) ) {
18961896 allUsedNames . add ( name ) ;
18971897 }
1898- // A wrapped body has no scope analysis, so its references are found
1899- // textually. They still have to be resolved here: binding one is what
1898+ // only its free globals reach freeNames; the reference tokens among
1899+ // them are resolved textually below and patched the same way in [4]
1900+ for ( const reference of /** @type {Reference[] } */ (
1901+ info . unresolvedReferences
1902+ ) ) {
1903+ const name = reference . identifier . name ;
1904+ if ( ! ConcatenationScope . isModuleReference ( name ) ) {
1905+ freeNames . add ( name ) ;
1906+ }
1907+ }
1908+ // A wrapped body's references are found textually, as [4] patches
1909+ // them. They still have to be resolved here: binding one is what
19001910 // flags the interop objects it needs, and [3] allocates their names.
19011911 /** @type {Set<string> } */
19021912 const visitedReferences = new Set ( ) ;
@@ -2358,8 +2368,8 @@ class ConcatenatedModule extends Module {
23582368 }
23592369 }
23602370 } else {
2361- // Wrapped bodies are not re-parsed: find their reference tokens
2362- // textually . [2] scanned the same source, so this reuses its result.
2371+ // A wrapped body's reference tokens are found textually rather than
2372+ // in its AST . [2] scanned the same source, so this reuses its result.
23632373 /** @type {Map<string, string> } */
23642374 const finalNames = new Map ( ) ;
23652375 const source = /** @type {ReplaceSource } */ ( info . source ) ;
@@ -3058,123 +3068,92 @@ ${defineGetters}`
30583068 codeGenerationResults ,
30593069 usedNames
30603070 ) {
3061- if ( info . type === "concatenated" ) {
3062- const m = info . module ;
3063- if ( info . wrapped ) {
3064- // the wrapper isolates the body's top-level declarations, so only a
3065- // concatenation scope is needed, rewriting in-scope `require()` targets.
3066- // Stage 2 claims its identifiers textually, in place of that analysis.
3067- const concatenationScope = new ConcatenationScope (
3068- modulesMap ,
3069- info ,
3070- usedNames
3071- ) ;
3072- info . concatenationScope = concatenationScope ;
3073- const codeGenResult = m . codeGeneration ( {
3074- dependencyTemplates,
3075- runtimeTemplate,
3076- moduleGraph,
3077- chunkGraph,
3078- runtime,
3079- runtimes,
3080- concatenationScope,
3081- codeGenerationResults,
3082- sourceTypes : JAVASCRIPT_TYPES
3083- } ) ;
3084- const source =
3085- /** @type {Source } */
3086- ( codeGenResult . sources . get ( JAVASCRIPT_TYPE ) ) ;
3087- const data = codeGenResult . data ;
3088- info . runtimeRequirements =
3089- /** @type {ReadOnlyRuntimeRequirements } */
3090- ( codeGenResult . runtimeRequirements ) ;
3091- info . internalSource = source ;
3092- info . source = new ReplaceSource ( source ) ;
3093- info . chunkInitFragments = data && data . get ( "chunkInitFragments" ) ;
3094- } else {
3095- try {
3096- // Create a concatenation scope to track and capture information
3097- const concatenationScope = new ConcatenationScope (
3098- modulesMap ,
3099- info ,
3100- usedNames
3101- ) ;
3071+ if ( info . type !== "concatenated" ) return ;
3072+ const m = info . module ;
3073+ try {
3074+ // Create a concatenation scope to track and capture information
3075+ const concatenationScope = new ConcatenationScope (
3076+ modulesMap ,
3077+ info ,
3078+ usedNames
3079+ ) ;
31023080
3103- // Not cached: Compilation memoizes the outer codeGeneration per
3104- // (module, runtime), so inner generation never recomputes usefully.
3105- const codeGenResult = m . codeGeneration ( {
3106- dependencyTemplates,
3107- runtimeTemplate,
3108- moduleGraph,
3109- chunkGraph,
3110- runtime,
3111- runtimes,
3112- concatenationScope,
3113- codeGenerationResults,
3114- sourceTypes : JAVASCRIPT_TYPES
3115- } ) ;
3116- const source =
3117- /** @type {Source } */
3118- ( codeGenResult . sources . get ( JAVASCRIPT_TYPE ) ) ;
3119- const data = codeGenResult . data ;
3120- const chunkInitFragments = data && data . get ( "chunkInitFragments" ) ;
3121- const code = source . source ( ) . toString ( ) ;
3122-
3123- /** @type {Program } */
3124- let ast ;
3125-
3126- try {
3127- const { experiments } = this . compilation . options ;
3128-
3129- ( { ast } = JavascriptParser . _parse (
3130- code ,
3131- {
3132- sourceType : "module" ,
3133- ranges : true ,
3134- // generated code contains phase imports when the experiments are on
3135- importPhases : Boolean (
3136- experiments . deferImport || experiments . sourceImport
3137- )
3138- } ,
3139- JavascriptParser . _getModuleParseFunction ( this . compilation , m )
3140- ) ) ;
3141- } catch ( _err ) {
3142- const err =
3143- /** @type {Error & { loc?: { line: number, column: number } } } */
3144- ( _err ) ;
3145- if (
3146- err . loc &&
3147- typeof err . loc === "object" &&
3148- typeof err . loc . line === "number"
3149- ) {
3150- const lineNumber = err . loc . line ;
3151- const lines = code . split ( "\n" ) ;
3152- err . message += `\n| ${ lines
3153- . slice ( Math . max ( 0 , lineNumber - 3 ) , lineNumber + 2 )
3154- . join ( "\n| " ) } `;
3155- }
3156- throw err ;
3157- }
3158- const analysis = analyzeScope ( ast ) ;
3159- const resultSource = new ReplaceSource ( source ) ;
3160- info . runtimeRequirements =
3161- /** @type {ReadOnlyRuntimeRequirements } */
3162- ( codeGenResult . runtimeRequirements ) ;
3163- info . ast = ast ;
3164- info . internalSource = source ;
3165- info . source = resultSource ;
3166- info . chunkInitFragments = chunkInitFragments ;
3167- info . globalScope = analysis . globalScope ;
3168- info . moduleScope = analysis . moduleScope ;
3169- info . unresolvedReferences = analysis . unresolvedReferences ;
3170- info . concatenationScope = concatenationScope ;
3171- } catch ( err ) {
3172- /** @type {Error } */
3173- ( err ) . message +=
3174- `\nwhile analyzing module ${ m . identifier ( ) } for concatenation` ;
3175- throw err ;
3081+ // Not cached: Compilation memoizes the outer codeGeneration per
3082+ // (module, runtime), so inner generation never recomputes usefully.
3083+ const codeGenResult = m . codeGeneration ( {
3084+ dependencyTemplates,
3085+ runtimeTemplate,
3086+ moduleGraph,
3087+ chunkGraph,
3088+ runtime,
3089+ runtimes,
3090+ concatenationScope,
3091+ codeGenerationResults,
3092+ sourceTypes : JAVASCRIPT_TYPES
3093+ } ) ;
3094+ const source =
3095+ /** @type {Source } */
3096+ ( codeGenResult . sources . get ( JAVASCRIPT_TYPE ) ) ;
3097+ const data = codeGenResult . data ;
3098+ const chunkInitFragments = data && data . get ( "chunkInitFragments" ) ;
3099+ const code = source . source ( ) . toString ( ) ;
3100+
3101+ /** @type {Program } */
3102+ let ast ;
3103+
3104+ try {
3105+ const { experiments } = this . compilation . options ;
3106+
3107+ ( { ast } = JavascriptParser . _parse (
3108+ code ,
3109+ {
3110+ // a wrapped CommonJS body may `return` at its top level
3111+ sourceType : info . wrapped ? "auto" : "module" ,
3112+ ranges : true ,
3113+ // generated code contains phase imports when the experiments are on
3114+ importPhases : Boolean (
3115+ experiments . deferImport || experiments . sourceImport
3116+ )
3117+ } ,
3118+ JavascriptParser . _getModuleParseFunction ( this . compilation , m )
3119+ ) ) ;
3120+ } catch ( _err ) {
3121+ const err =
3122+ /** @type {Error & { loc?: { line: number, column: number } } } */
3123+ ( _err ) ;
3124+ if (
3125+ err . loc &&
3126+ typeof err . loc === "object" &&
3127+ typeof err . loc . line === "number"
3128+ ) {
3129+ const lineNumber = err . loc . line ;
3130+ const lines = code . split ( "\n" ) ;
3131+ err . message += `\n| ${ lines
3132+ . slice ( Math . max ( 0 , lineNumber - 3 ) , lineNumber + 2 )
3133+ . join ( "\n| " ) } `;
31763134 }
3135+ throw err ;
31773136 }
3137+ const analysis = analyzeScope ( ast ) ;
3138+ info . runtimeRequirements =
3139+ /** @type {ReadOnlyRuntimeRequirements } */
3140+ ( codeGenResult . runtimeRequirements ) ;
3141+ info . internalSource = source ;
3142+ info . source = new ReplaceSource ( source ) ;
3143+ info . chunkInitFragments = chunkInitFragments ;
3144+ info . unresolvedReferences = analysis . unresolvedReferences ;
3145+ info . concatenationScope = concatenationScope ;
3146+ // the wrapper isolates the body's declarations, so [3] renames nothing in
3147+ // it and its scopes are not kept; [2] claims its identifiers textually
3148+ if ( info . wrapped ) return ;
3149+ info . ast = ast ;
3150+ info . globalScope = analysis . globalScope ;
3151+ info . moduleScope = analysis . moduleScope ;
3152+ } catch ( err ) {
3153+ /** @type {Error } */
3154+ ( err ) . message +=
3155+ `\nwhile analyzing module ${ m . identifier ( ) } for concatenation` ;
3156+ throw err ;
31783157 }
31793158 }
31803159
0 commit comments