@@ -253,6 +253,7 @@ const printGeneratedCodeForStack = (module, code) => {
253253 * @property {boolean= } inlined inlined
254254 * @property {boolean= } inlinedInIIFE the inlined entry module is wrapped in an IIFE
255255 * @property {boolean= } needExportsDeclaration whether the top-level exports declaration needs to be generated
256+ * @property {Map<string, string>= } renamedDeclarations the inlined entry's top-level declarations renamed to avoid a collision, original to new name
256257 */
257258
258259/**
@@ -367,6 +368,7 @@ const createCompilationHooks = () => ({
367368 * @param {string } moduleIdentifier the module identifier (for scope caching)
368369 * @param {string } readableIdentifier the readable module identifier (for new names)
369370 * @param {Set<string> } allUsedNames names already used in the shared scope (mutated)
371+ * @param {Map<string, string> } renamedDeclarations the declarations renamed so far, original to new name (mutated)
370372 * @returns {void }
371373 */
372374const renameInlinedModuleVariable = (
@@ -375,7 +377,8 @@ const renameInlinedModuleVariable = (
375377 variable ,
376378 moduleIdentifier ,
377379 readableIdentifier ,
378- allUsedNames
380+ allUsedNames ,
381+ renamedDeclarations
379382) => {
380383 /** @type {UsedNamesInScopeInfo } */
381384 const usedNamesInScopeInfo = new Map ( ) ;
@@ -406,6 +409,7 @@ const renameInlinedModuleVariable = (
406409 readableIdentifier
407410 ) ;
408411 allUsedNames . add ( newName ) ;
412+ renamedDeclarations . set ( name , newName ) ;
409413 for ( const identifier of allIdentifiers ) {
410414 const r = /** @type {Range } */ ( identifier . range ) ;
411415 const path = getPathInAst ( ast , identifier ) ;
@@ -429,6 +433,12 @@ const PLUGIN_NAME = "JavascriptModulesPlugin";
429433
430434/** @typedef {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean } } Bootstrap */
431435
436+ /**
437+ * @typedef {object } RenamedInlinedModules
438+ * @property {Map<Module, Source> } sources each inlined module's rendered source, renamed where it collided
439+ * @property {Map<Module, Map<string, string>> } renamedDeclarations each renamed module's declarations, original to new name
440+ */
441+
432442class JavascriptModulesPlugin {
433443 constructor ( options = { } ) {
434444 this . options = options ;
@@ -1272,7 +1282,7 @@ class JavascriptModulesPlugin {
12721282 const startupSource = new ConcatSource ( ) ;
12731283
12741284 const avoidEntryIife = compilation . options . optimization . avoidEntryIife ;
1275- /** @type {Map<Module, Source> | false } */
1285+ /** @type {RenamedInlinedModules | false } */
12761286 let renamedInlinedModule = false ;
12771287 let inlinedInIIFE = false ;
12781288
@@ -1316,7 +1326,7 @@ class JavascriptModulesPlugin {
13161326 }
13171327
13181328 const renderedModule = renamedInlinedModule
1319- ? renamedInlinedModule . get ( m )
1329+ ? renamedInlinedModule . sources . get ( m )
13201330 : this . renderModule (
13211331 m ,
13221332 {
@@ -1373,7 +1383,10 @@ class JavascriptModulesPlugin {
13731383 ...renderContext ,
13741384 inlined : true ,
13751385 inlinedInIIFE,
1376- needExportsDeclaration : runtimeRequirements . has ( RuntimeGlobals . exports )
1386+ needExportsDeclaration : runtimeRequirements . has ( RuntimeGlobals . exports ) ,
1387+ renamedDeclarations : renamedInlinedModule
1388+ ? renamedInlinedModule . renamedDeclarations . get ( lastInlinedModule )
1389+ : undefined
13771390 } ;
13781391 let renderedStartup = hooks . renderStartup . call (
13791392 startupSource ,
@@ -2105,7 +2118,7 @@ class JavascriptModulesPlugin {
21052118 * @param {CompilationHooks } hooks hooks
21062119 * @param {boolean | undefined } allStrict allStrict
21072120 * @param {boolean } hasChunkModules hasChunkModules
2108- * @returns {Map<Module, Source> | false } renamed inlined modules
2121+ * @returns {RenamedInlinedModules | false } renamed inlined modules
21092122 */
21102123 _getRenamedInlineModule (
21112124 compilation ,
@@ -2131,6 +2144,8 @@ class JavascriptModulesPlugin {
21312144
21322145 /** @type {Map<Module, Source> } */
21332146 const renamedInlinedModules = new Map ( ) ;
2147+ /** @type {Map<Module, Map<string, string>> } */
2148+ const renamedDeclarations = new Map ( ) ;
21342149 const { runtimeTemplate } = renderContext ;
21352150
21362151 /** @typedef {{ source: Source, module: Module, ast: Program, variables: Set<Variable>, through: Set<Reference>, usedInNonInlined: Set<Variable>, moduleScope: Scope } } Info */
@@ -2243,7 +2258,10 @@ class JavascriptModulesPlugin {
22432258 for ( const [ m , moduleSource ] of inlinedModuleSources ) {
22442259 renamedInlinedModules . set ( m , moduleSource ) ;
22452260 }
2246- return renamedInlinedModules ;
2261+ return {
2262+ sources : renamedInlinedModules ,
2263+ renamedDeclarations
2264+ } ;
22472265 }
22482266
22492267 for ( const [ m , moduleSource ] of inlinedModuleSources ) {
@@ -2301,19 +2319,26 @@ class JavascriptModulesPlugin {
23012319 for ( const [ m , moduleInfo ] of inlinedModulesToInfo ) {
23022320 const { ast, source : _source } = moduleInfo ;
23032321 const source = new ReplaceSource ( _source ) ;
2322+ /** @type {Map<string, string> } */
2323+ const renamed = new Map ( ) ;
23042324 for ( const variable of moduleInfo . variables ) {
23052325 renameInlinedModuleVariable (
23062326 source ,
23072327 ast ,
23082328 variable ,
23092329 moduleInfo . module . identifier ( ) ,
23102330 m . readableIdentifier ( runtimeTemplate . requestShortener ) ,
2311- allUsedNames
2331+ allUsedNames ,
2332+ renamed
23122333 ) ;
23132334 }
23142335 renamedInlinedModules . set ( m , source ) ;
2336+ if ( renamed . size > 0 ) renamedDeclarations . set ( m , renamed ) ;
23152337 }
2316- return renamedInlinedModules ;
2338+ return {
2339+ sources : renamedInlinedModules ,
2340+ renamedDeclarations
2341+ } ;
23172342 }
23182343
23192344 for ( const [ m , moduleInfo ] of inlinedModulesToInfo ) {
@@ -2333,21 +2358,28 @@ class JavascriptModulesPlugin {
23332358 allUsedNames . add ( variable . name ) ;
23342359 }
23352360
2361+ /** @type {Map<string, string> } */
2362+ const renamed = new Map ( ) ;
23362363 for ( const variable of info . variables ) {
23372364 renameInlinedModuleVariable (
23382365 source ,
23392366 ast ,
23402367 variable ,
23412368 info . module . identifier ( ) ,
23422369 m . readableIdentifier ( runtimeTemplate . requestShortener ) ,
2343- allUsedNames
2370+ allUsedNames ,
2371+ renamed
23442372 ) ;
23452373 }
23462374
23472375 renamedInlinedModules . set ( m , source ) ;
2376+ if ( renamed . size > 0 ) renamedDeclarations . set ( m , renamed ) ;
23482377 }
23492378
2350- return renamedInlinedModules ;
2379+ return {
2380+ sources : renamedInlinedModules ,
2381+ renamedDeclarations
2382+ } ;
23512383 }
23522384}
23532385
0 commit comments