@@ -252,19 +252,15 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
252252 * @param {LibraryContext<T> } libraryContext context
253253 * @returns {Source } source with library export
254254 */
255- renderStartup (
256- source ,
257- module ,
258- {
255+ renderStartup ( source , module , renderContext , { options, compilation } ) {
256+ const {
259257 moduleGraph,
260258 chunk,
261259 codeGenerationResults,
262260 inlined,
263261 inlinedInIIFE,
264262 runtimeTemplate
265- } ,
266- { options, compilation }
267- ) {
263+ } = renderContext ;
268264 let result = new ConcatSource ( source ) ;
269265 const exportInfos = options . export
270266 ? [
@@ -283,8 +279,11 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
283279 ) ) ||
284280 { } ;
285281
286- const definitions =
287- inlined && ! inlinedInIIFE ? exportsFinalNameByRuntime : { } ;
282+ const isInlinedEntryWithoutIIFE = inlined && ! inlinedInIIFE ;
283+ // Direct export bindings from on-demand concatenation
284+ const definitions = isInlinedEntryWithoutIIFE
285+ ? exportsFinalNameByRuntime
286+ : { } ;
288287
289288 /** @type {string[] } */
290289 const shortHandedExports = [ ] ;
@@ -299,23 +298,37 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
299298 module . buildMeta && module . buildMeta . treatAsCommonJs ;
300299 const skipRenderDefaultExport = Boolean ( treatAsCommonJs ) ;
301300
301+ const moduleExportsInfo = moduleGraph . getExportsInfo ( module ) ;
302+
303+ // Define ESM compatibility flag will rely on `__webpack_exports__`
304+ const needHarmonyCompatibilityFlag =
305+ moduleExportsInfo . otherExportsInfo . getUsed ( chunk . runtime ) !==
306+ UsageState . Unused ||
307+ moduleExportsInfo
308+ . getReadOnlyExportInfo ( "__esModule" )
309+ . getUsed ( chunk . runtime ) !== UsageState . Unused ;
310+
311+ let needExportsDeclaration =
312+ ! isInlinedEntryWithoutIIFE || isAsync || needHarmonyCompatibilityFlag ;
313+
302314 if ( isAsync ) {
303315 result . add (
304316 `${ RuntimeGlobals . exports } = await ${ RuntimeGlobals . exports } ;\n`
305317 ) ;
306318 }
307319
320+ // Try to find all known exports of the entry module
308321 outer: for ( const exportInfo of exportInfos ) {
309322 if ( ! exportInfo . provided ) continue ;
310323
311324 const originalName = exportInfo . name ;
312-
325+ // Skip rendering the default export in some cases
313326 if ( skipRenderDefaultExport && originalName === "default" ) continue ;
314327
328+ // Try to find all exports from the reexported modules
315329 const target = exportInfo . findTarget ( moduleGraph , ( _m ) => true ) ;
316330 if ( target ) {
317331 const reexportsInfo = moduleGraph . getExportsInfo ( target . module ) ;
318-
319332 for ( const reexportInfo of reexportsInfo . orderedExports ) {
320333 if (
321334 reexportInfo . provided === false &&
@@ -332,14 +345,16 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
332345 ( exportInfo . getUsedName ( originalName , chunk . runtime ) ) ;
333346 /** @type {string | undefined } */
334347 const definition = definitions [ usedName ] ;
335-
336348 /** @type {string | undefined } */
337349 let finalName ;
338350
339351 if ( definition ) {
340352 finalName = definition ;
341353 } else {
354+ // Fallback to `__webpack_exports__` property access
355+ // when no direct export binding was found
342356 finalName = `${ RuntimeGlobals . exports } ${ Template . toIdentifier ( originalName ) } ` ;
357+ needExportsDeclaration = true ;
343358 result . add (
344359 `${ runtimeTemplate . renderConst ( ) } ${ finalName } = ${ RuntimeGlobals . exports } ${ propertyAccess (
345360 [ usedName ]
@@ -348,6 +363,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
348363 }
349364
350365 if (
366+ // If the name includes `property access` and `call expressions`
351367 finalName &&
352368 ( finalName . includes ( "." ) ||
353369 finalName . includes ( "[" ) ||
@@ -382,7 +398,9 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
382398 alreadyRenderedExports . add ( originalName ) ;
383399 }
384400
401+ // Add default export `__webpack_exports__` statement to keep better compatibility
385402 if ( treatAsCommonJs ) {
403+ needExportsDeclaration = true ;
386404 shortHandedExports . push ( `${ RuntimeGlobals . exports } as default` ) ;
387405 }
388406
@@ -405,6 +423,10 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
405423 ) ;
406424 }
407425
426+ if ( ! needExportsDeclaration ) {
427+ renderContext . needExportsDeclaration = false ;
428+ }
429+
408430 return result ;
409431 }
410432
0 commit comments