11import path from 'node:path' ;
22import type { ResolveAlias , Compiler as RspackCompiler } from '@rspack/core' ;
33import type { Compiler as WebpackCompiler } from 'webpack' ;
4- import { isRspackCompiler , moveElementBefore } from '../helpers/index.js' ;
4+ import { isRspackCompiler , moveElementBefore } from '../../helpers/index.js' ;
5+ import { makePolyfillsRuntimeModule } from './PolyfillsRuntimeModule.js' ;
56
67export interface NativeEntryPluginConfig {
78 /**
@@ -55,18 +56,37 @@ export class NativeEntryPlugin {
5556 path . join ( reactNativePath , 'Libraries/Core/InitializeCore.js' ) ;
5657
5758 const initializeScriptManagerPath = require . resolve (
58- '../modules/InitializeScriptManager.js'
59+ '../../ modules/InitializeScriptManager.js'
5960 ) ;
6061
61- const includeModulesPath = require . resolve ( '../modules/IncludeModules.js' ) ;
62+ const includeModulesPath = require . resolve (
63+ '../../modules/IncludeModules.js'
64+ ) ;
65+
66+ const polyfillPaths = getReactNativePolyfills ( ) ;
6267
6368 const nativeEntries = [
64- ...getReactNativePolyfills ( ) ,
69+ ...polyfillPaths ,
6570 initializeCorePath ,
6671 initializeScriptManagerPath ,
6772 includeModulesPath ,
6873 ] ;
6974
75+ // Polyfills are entry modules (processed by loaders), but we also require them
76+ // from a runtime module to guarantee they execute before Module Federation's
77+ // startup wrapper. The duplicate require during startup is a cache hit.
78+ compiler . hooks . compilation . tap ( 'RepackNativeEntryPlugin' , ( compilation ) => {
79+ compilation . hooks . additionalTreeRuntimeRequirements . tap (
80+ 'RepackNativeEntryPlugin' ,
81+ ( chunk ) => {
82+ compilation . addRuntimeModule (
83+ chunk ,
84+ makePolyfillsRuntimeModule ( compiler , { polyfillPaths } )
85+ ) ;
86+ }
87+ ) ;
88+ } ) ;
89+
7090 compiler . hooks . entryOption . tap (
7191 { name : 'RepackNativeEntryPlugin' , before : 'RepackDevelopmentPlugin' } ,
7292 ( _ , entry ) => {
@@ -76,14 +96,12 @@ export class NativeEntryPlugin {
7696 ) ;
7797 }
7898
99+ // add native entries (including polyfills) to each declared entry point
79100 Object . keys ( entry ) . forEach ( ( entryName ) => {
80- // runtime property defines the chunk name, otherwise it defaults to the entry key
81101 const entryChunkName = entry [ entryName ] . runtime || entryName ;
82-
83- // add native entries to all declared entry points
84102 for ( const nativeEntry of nativeEntries ) {
85103 new compiler . webpack . EntryPlugin ( compiler . context , nativeEntry , {
86- name : entryChunkName , // prepends the entry to the chunk of specified name
104+ name : entryChunkName ,
87105 } ) . apply ( compiler ) ;
88106 }
89107 } ) ;
0 commit comments