11/*
2- MIT License http://www.opensource.org/licenses/mit-license.php
3- Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
2+ MIT License http://www.opensource.org/licenses/mit-license.php
3+ Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
44*/
55
66'use strict' ;
@@ -26,7 +26,7 @@ import StartupChunkDependenciesPlugin from '../startup/MfStartupChunkDependencie
2626import FederationModulesPlugin from './runtime/FederationModulesPlugin' ;
2727import { createSchemaValidation } from '../../utils' ;
2828import TreeshakeSharePlugin from '../sharing/treeshake/TreeshakeSharePlugin' ;
29- import { MakeRequired } from '../sharing/treeshake/IndependentSharePlugin ' ;
29+ import { MakeRequired } from '../sharing/treeshake/IndependentSharedPlugin ' ;
3030
3131const isValidExternalsType = require (
3232 normalizeWebpackPath (
@@ -38,16 +38,15 @@ const { ExternalsPlugin } = require(
3838 normalizeWebpackPath ( 'webpack' ) ,
3939) as typeof import ( 'webpack' ) ;
4040
41- // TODO: remove the comment
42- // const validate = createSchemaValidation(
43- // //eslint-disable-next-line
44- // require('../../schemas/container/ModuleFederationPlugin.check.js').validate,
45- // () => require('../../schemas/container/ModuleFederationPlugin').default,
46- // {
47- // name: 'Module Federation Plugin',
48- // baseDataPath: 'options',
49- // },
50- // );
41+ const validate = createSchemaValidation (
42+ //eslint-disable-next-line
43+ require ( '../../schemas/container/ModuleFederationPlugin.check.js' ) . validate ,
44+ ( ) => require ( '../../schemas/container/ModuleFederationPlugin' ) . default ,
45+ {
46+ name : 'Module Federation Plugin' ,
47+ baseDataPath : 'options' ,
48+ } ,
49+ ) ;
5150
5251class ModuleFederationPlugin implements WebpackPluginInstance {
5352 private _options : moduleFederationPlugin . ModuleFederationPluginOptions ;
@@ -56,8 +55,7 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
5655 * @param {moduleFederationPlugin.ModuleFederationPluginOptions } options options
5756 */
5857 constructor ( options : moduleFederationPlugin . ModuleFederationPluginOptions ) {
59- // TODO: remove the comment
60- // validate(options);
58+ validate ( options ) ;
6159 this . _options = options ;
6260 }
6361
@@ -113,16 +111,18 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
113111 'EnhancedModuleFederationPlugin' ,
114112 ) ;
115113 const { _options : options } = this ;
116- if ( ! options . name ) {
114+ const { name, experiments, exposes, dts, remotes, shared, shareScope } =
115+ options ;
116+ if ( ! name ) {
117117 // TODO: remove the comment
118- // throw new Error('ModuleFederationPlugin name is required');
118+ throw new Error ( 'ModuleFederationPlugin name is required' ) ;
119119 }
120120 // must before ModuleFederationPlugin
121121 ( new RemoteEntryPlugin ( options ) as unknown as WebpackPluginInstance ) . apply (
122122 compiler ,
123123 ) ;
124- if ( options . experiments ?. provideExternalRuntime ) {
125- if ( options . exposes ) {
124+ if ( experiments ?. provideExternalRuntime ) {
125+ if ( exposes ) {
126126 throw new Error (
127127 'You can only set provideExternalRuntime: true in pure consumer which not expose modules.' ,
128128 ) ;
@@ -135,7 +135,7 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
135135 ) ;
136136 }
137137
138- if ( options . experiments ?. externalRuntime === true ) {
138+ if ( experiments ?. externalRuntime === true ) {
139139 const Externals = compiler . webpack . ExternalsPlugin || ExternalsPlugin ;
140140 new Externals ( compiler . options . externalsType || 'global' , {
141141 '@module-federation/runtime-core' : '_FEDERATION_RUNTIME_CORE' ,
@@ -145,39 +145,40 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
145145 // federation hooks
146146 new FederationModulesPlugin ( ) . apply ( compiler ) ;
147147
148- if ( options . experiments ?. asyncStartup ) {
148+ if ( experiments ?. asyncStartup ) {
149149 new StartupChunkDependenciesPlugin ( {
150150 asyncChunkLoading : true ,
151151 } ) . apply ( compiler ) ;
152152 }
153153
154- if ( options . dts !== false ) {
154+ if ( dts !== false ) {
155155 const dtsPlugin = new DtsPlugin ( options ) ;
156156 dtsPlugin . apply ( compiler ) ;
157157 dtsPlugin . addRuntimePlugins ( ) ;
158158 }
159+ // TODO: REMOVE in next major version
159160 if ( options . dataPrefetch ) {
160161 new PrefetchPlugin ( options ) . apply ( compiler ) ;
161162 }
162163
163164 new FederationRuntimePlugin ( options ) . apply ( compiler ) ;
164165
165- const library = options . library || { type : 'var' , name : options . name } ;
166+ const library = options . library || { type : 'var' , name : name } ;
166167 const remoteType =
167168 options . remoteType ||
168169 ( options . library && isValidExternalsType ( options . library . type )
169170 ? ( options . library . type as ExternalsType )
170171 : ( 'script' as ExternalsType ) ) ;
171172
172173 const useContainerPlugin =
173- options . exposes &&
174- ( Array . isArray ( options . exposes )
175- ? options . exposes . length > 0
176- : Object . keys ( options . exposes ) . length > 0 ) ;
174+ exposes &&
175+ ( Array . isArray ( exposes )
176+ ? exposes . length > 0
177+ : Object . keys ( exposes ) . length > 0 ) ;
177178
178179 let disableManifest = options . manifest === false ;
179180 if ( useContainerPlugin ) {
180- ContainerPlugin . patchChunkSplit ( compiler , this . _options . name ! ) ;
181+ ContainerPlugin . patchChunkSplit ( compiler , name ) ;
181182 }
182183 this . _patchBundlerConfig ( compiler ) ;
183184 if ( ! disableManifest && useContainerPlugin ) {
@@ -204,37 +205,34 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
204205 compiler . hooks . afterPlugins . tap ( 'ModuleFederationPlugin' , ( ) => {
205206 if ( useContainerPlugin ) {
206207 new ContainerPlugin ( {
207- name : options . name ! ,
208+ name,
208209 library,
209210 filename : options . filename ,
210211 runtime : options . runtime ,
211212 shareScope : options . shareScope ,
212- exposes : options . exposes ! ,
213+ exposes,
213214 runtimePlugins : options . runtimePlugins ,
214215 } ) . apply ( compiler ) ;
215216 }
216217 if (
217- options . remotes &&
218- ( Array . isArray ( options . remotes )
219- ? options . remotes . length > 0
220- : Object . keys ( options . remotes ) . length > 0 )
218+ remotes &&
219+ ( Array . isArray ( remotes )
220+ ? remotes . length > 0
221+ : Object . keys ( remotes ) . length > 0 )
221222 ) {
222223 new ContainerReferencePlugin ( {
223224 remoteType,
224- shareScope : options . shareScope ,
225- remotes : options . remotes ,
225+ shareScope,
226+ remotes,
226227 } ) . apply ( compiler ) ;
227228 }
228- if ( options . shared ) {
229- new SharePlugin ( {
230- shared : options . shared ,
231- shareScope : options . shareScope ,
232- } ) . apply ( compiler ) ;
229+ if ( shared ) {
233230 new TreeshakeSharePlugin ( {
234- mfConfig : options as MakeRequired <
235- moduleFederationPlugin . ModuleFederationPluginOptions ,
236- 'shared' | 'name'
237- > ,
231+ mfConfig : options ,
232+ } ) . apply ( compiler ) ;
233+ new SharePlugin ( {
234+ shared,
235+ shareScope,
238236 } ) . apply ( compiler ) ;
239237 }
240238 } ) ;
0 commit comments