@@ -107,54 +107,57 @@ export function handleServerExternals(
107
107
compiler : Compiler ,
108
108
options : moduleFederationPlugin . ModuleFederationPluginOptions ,
109
109
) : void {
110
- if (
111
- Array . isArray ( compiler . options . externals ) &&
112
- typeof compiler . options . externals [ 0 ] === 'function'
113
- ) {
114
- const originalExternals = compiler . options . externals [ 0 ] as (
115
- data : ExternalItemFunctionData ,
116
- callback : any ,
117
- ) => undefined | string ;
110
+ if ( Array . isArray ( compiler . options . externals ) ) {
111
+ const functionIndex = compiler . options . externals . findIndex (
112
+ ( external ) => typeof external === 'function' ,
113
+ ) ;
118
114
119
- compiler . options . externals [ 0 ] = async function (
120
- ctx : ExternalItemFunctionData ,
121
- callback : any ,
122
- ) {
123
- const fromNext = await originalExternals ( ctx , callback ) ;
124
- if ( ! fromNext ) {
125
- return ;
126
- }
127
- const req = fromNext . split ( ' ' ) [ 1 ] ;
128
- if (
129
- ctx . request &&
130
- ( ctx . request . includes ( '@module-federation/utilities' ) ||
131
- Object . keys ( options . shared || { } ) . some ( ( key ) => {
132
- const sharedOptions = options . shared as Record <
133
- string ,
134
- { import : boolean }
135
- > ;
136
- return (
137
- sharedOptions [ key ] ?. import !== false &&
138
- ( key . endsWith ( '/' ) ? req . includes ( key ) : req === key )
139
- ) ;
140
- } ) ||
141
- ctx . request . includes ( '@module-federation/' ) )
142
- ) {
143
- return ;
144
- }
115
+ if ( functionIndex !== - 1 ) {
116
+ const originalExternals = compiler . options . externals [ functionIndex ] as (
117
+ data : ExternalItemFunctionData ,
118
+ callback : any ,
119
+ ) => undefined | string ;
145
120
146
- if (
147
- req . startsWith ( 'next' ) ||
148
- req . startsWith ( 'react/' ) ||
149
- req . startsWith ( 'react-dom/' ) ||
150
- req === 'react' ||
151
- req === 'styled-jsx/style' ||
152
- req === 'react-dom'
121
+ compiler . options . externals [ functionIndex ] = async function (
122
+ ctx : ExternalItemFunctionData ,
123
+ callback : any ,
153
124
) {
154
- return fromNext ;
155
- }
156
- return ;
157
- } ;
125
+ const fromNext = await originalExternals ( ctx , callback ) ;
126
+ if ( ! fromNext ) {
127
+ return ;
128
+ }
129
+ const req = fromNext . split ( ' ' ) [ 1 ] ;
130
+ if (
131
+ ctx . request &&
132
+ ( ctx . request . includes ( '@module-federation/utilities' ) ||
133
+ Object . keys ( options . shared || { } ) . some ( ( key ) => {
134
+ const sharedOptions = options . shared as Record <
135
+ string ,
136
+ { import : boolean }
137
+ > ;
138
+ return (
139
+ sharedOptions [ key ] ?. import !== false &&
140
+ ( key . endsWith ( '/' ) ? req . includes ( key ) : req === key )
141
+ ) ;
142
+ } ) ||
143
+ ctx . request . includes ( '@module-federation/' ) )
144
+ ) {
145
+ return ;
146
+ }
147
+
148
+ if (
149
+ req . startsWith ( 'next' ) ||
150
+ req . startsWith ( 'react/' ) ||
151
+ req . startsWith ( 'react-dom/' ) ||
152
+ req === 'react' ||
153
+ req === 'styled-jsx/style' ||
154
+ req === 'react-dom'
155
+ ) {
156
+ return fromNext ;
157
+ }
158
+ return ;
159
+ } ;
160
+ }
158
161
}
159
162
}
160
163
0 commit comments