@@ -20,13 +20,14 @@ const ConcatenatedModule = require("../optimize/ConcatenatedModule");
2020const { InlinedUsedName } = require ( "../optimize/InlineExports" ) ;
2121const RuntimeGlobals = require ( "../runtime/RuntimeGlobals" ) ;
2222const Template = require ( "../template/Template" ) ;
23+ const { someInIterable } = require ( "../util/IterableHelpers" ) ;
2324const { renameIdentifiers } = require ( "../util/concatenate" ) ;
2425const {
2526 RESERVED_IDENTIFIER ,
2627 SAFE_IDENTIFIER ,
2728 propertyAccess
2829} = require ( "../util/property" ) ;
29- const { getEntryRuntime } = require ( "../util/runtime" ) ;
30+ const { getEntryRuntime, intersectRuntime } = require ( "../util/runtime" ) ;
3031const AbstractLibraryPlugin = require ( "./AbstractLibraryPlugin" ) ;
3132
3233/** @import { Source } from "webpack-sources" */
@@ -37,7 +38,6 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
3738 * LibraryExport
3839 * } from "../../declarations/WebpackOptions"
3940 */
40- /** @import Chunk from "../graph/Chunk" */
4141/** @import CodeGenerationResults from "../module/CodeGenerationResults" */
4242/** @import Compilation from "../Compilation" */
4343/** @import Compiler from "../Compiler" */
@@ -170,6 +170,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
170170 compilation . hooks . additionalChunkRuntimeRequirements . tap (
171171 PLUGIN_NAME ,
172172 ( chunk , set , { chunkGraph, codeGenerationResults } ) => {
173+ const { moduleGraph, runtimeTemplate } = compilation ;
173174 if ( ! set . has ( RuntimeGlobals . definePropertyGetters ) ) return ;
174175 if ( chunkGraph . getNumberOfEntryModules ( chunk ) !== 1 ) return ;
175176 if (
@@ -184,6 +185,48 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
184185 if ( javascriptHooks . inlineInRuntimeBailout . isUsed ( ) ) return ;
185186
186187 const [ module ] = chunkGraph . getChunkEntryModulesIterable ( chunk ) ;
188+ if ( chunk . hasRuntime ( ) ) {
189+ // The startup inlines the entry only where nothing else loads with
190+ // it: another chunk of the entrypoint delays the execution, and a
191+ // module reading the entry needs it in the registry.
192+ for ( const [
193+ ,
194+ entrypoint
195+ ] of chunkGraph . getChunkEntryModulesWithChunkGroupIterable ( chunk ) ) {
196+ if ( entrypoint && entrypoint . chunks . some ( ( c ) => c !== chunk ) ) {
197+ return ;
198+ }
199+ }
200+ if (
201+ someInIterable (
202+ moduleGraph . getIncomingConnectionsByOriginModule ( module ) ,
203+ ( [ originModule , connections ] ) =>
204+ originModule &&
205+ originModule !== module &&
206+ connections . some ( ( c ) => c . isTargetActive ( chunk . runtime ) ) &&
207+ someInIterable (
208+ chunkGraph . getModuleRuntimes ( originModule ) ,
209+ ( runtime ) =>
210+ intersectRuntime ( runtime , chunk . runtime ) !== undefined
211+ )
212+ )
213+ ) {
214+ return ;
215+ }
216+ } else if (
217+ // A chunk whose runtime is elsewhere lifts its entry to the top
218+ // level under the same conditions, and renders it into the module
219+ // registry otherwise.
220+ JavascriptModulesPlugin . _getEsmEntryModuleToInline ( {
221+ chunk,
222+ chunkGraph,
223+ moduleGraph,
224+ runtimeTemplate,
225+ codeGenerationResults
226+ } ) !== module
227+ ) {
228+ return ;
229+ }
187230 const codeGenResult = codeGenerationResults . get (
188231 module ,
189232 chunk . runtime
@@ -212,45 +255,32 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
212255 }
213256 }
214257
215- // The helpers land in the runtime, not in this chunk, so what still
216- // needs them is asked of every chunk sharing the entrypoint — the
217- // runtime chunk, and whatever `splitChunks` moved out of the entry.
218- /** @type {Set<Chunk> } */
219- const siblings = new Set ( [ chunk ] ) ;
220- for ( const [
221- ,
222- entrypoint
223- ] of chunkGraph . getChunkEntryModulesWithChunkGroupIterable ( chunk ) ) {
224- if ( ! entrypoint ) continue ;
225- for ( const sibling of entrypoint . chunks ) siblings . add ( sibling ) ;
226- }
227-
258+ // The entrypoint loads as this chunk alone, so what still needs the
259+ // helpers is whatever else it holds beside the entry.
228260 let otherNeedsDefine = false ;
229261 let otherNeedsNamespaceObject = false ;
230262 let otherNeedsExports = false ;
231263 let otherNeedsRequireScope = false ;
232- outer: for ( const sibling of siblings ) {
233- for ( const m of chunkGraph . getChunkModulesIterable ( sibling ) ) {
234- if ( m === module ) continue ;
235- const requirements = chunkGraph . getModuleRuntimeRequirements (
236- m ,
237- sibling . runtime
238- ) ;
239- if ( ! requirements ) continue ;
240- if ( requirements . has ( RuntimeGlobals . definePropertyGetters ) ) {
241- otherNeedsDefine = true ;
242- }
243- if ( requirements . has ( RuntimeGlobals . makeNamespaceObject ) ) {
244- otherNeedsNamespaceObject = true ;
245- }
246- if ( requirements . has ( RuntimeGlobals . exports ) ) {
247- otherNeedsExports = true ;
248- }
249- if ( requirements . has ( RuntimeGlobals . requireScope ) ) {
250- otherNeedsRequireScope = true ;
251- }
252- if ( otherNeedsDefine ) break outer;
264+ for ( const m of chunkGraph . getChunkModulesIterable ( chunk ) ) {
265+ if ( m === module ) continue ;
266+ const requirements = chunkGraph . getModuleRuntimeRequirements (
267+ m ,
268+ chunk . runtime
269+ ) ;
270+ if ( ! requirements ) continue ;
271+ if ( requirements . has ( RuntimeGlobals . definePropertyGetters ) ) {
272+ otherNeedsDefine = true ;
273+ }
274+ if ( requirements . has ( RuntimeGlobals . makeNamespaceObject ) ) {
275+ otherNeedsNamespaceObject = true ;
276+ }
277+ if ( requirements . has ( RuntimeGlobals . exports ) ) {
278+ otherNeedsExports = true ;
279+ }
280+ if ( requirements . has ( RuntimeGlobals . requireScope ) ) {
281+ otherNeedsRequireScope = true ;
253282 }
283+ if ( otherNeedsDefine ) break ;
254284 }
255285
256286 if ( otherNeedsDefine ) return ;
0 commit comments