1
1
import { findRootTsConfigJson , shareAll } from './share-utils' ;
2
2
import { SharedMappings } from './shared-mappings' ;
3
+ import { ModifyEntryPlugin } from './modify-entry-plugin' ;
3
4
4
5
const ModuleFederationPlugin = require ( "webpack/lib/container/ModuleFederationPlugin" ) ;
5
6
@@ -11,37 +12,13 @@ export function withModuleFederationPlugin(config: unknown) {
11
12
const mappings = new SharedMappings ( ) ;
12
13
mappings . register ( findRootTsConfigJson ( ) , sharedMappings ) ;
13
14
14
- if ( ! config [ 'library' ] ) {
15
- config [ 'library' ] = {
16
- type : 'module'
17
- }
18
- }
19
-
20
- if ( ! config [ 'filename' ] ) {
21
- config [ 'filename' ] = 'remoteEntry.js' ;
22
- }
23
-
24
- if ( ! config [ 'shared' ] ) {
25
- config [ 'shared' ] = shareAll (
26
- { singleton : true , strictVersion : true , requiredVersion : 'auto' } ) ;
27
- }
28
-
29
- if ( typeof config [ 'shared' ] === 'object' ) {
30
- config [ 'shared' ] = {
31
- ...config [ 'shared' ] ,
32
- ...mappings . getDescriptors ( )
33
- }
34
- }
35
-
36
- if ( Array . isArray ( config [ 'shared' ] ) ) {
37
- config [ 'shared' ] = [
38
- ...config [ 'shared' ] ,
39
- mappings . getDescriptors ( )
40
- ]
41
- }
15
+ setDefaults ( config , mappings ) ;
16
+ const modifyEntryPlugin = createModifyEntryPlugin ( config ) ;
42
17
43
18
const isModule = config [ 'library' ] ?. [ 'type' ] === 'module' ;
44
19
20
+ // console.log('sharedConfig.modFed', sharedConfig.modFed);
21
+
45
22
return {
46
23
output : {
47
24
publicPath : "auto"
@@ -61,7 +38,80 @@ export function withModuleFederationPlugin(config: unknown) {
61
38
} : { } ,
62
39
plugins : [
63
40
new ModuleFederationPlugin ( config ) ,
64
- mappings . getPlugin ( )
41
+ mappings . getPlugin ( ) ,
42
+ ...( modifyEntryPlugin ) ? [ modifyEntryPlugin ] : [ ]
65
43
] ,
66
44
} ;
67
- }
45
+ }
46
+
47
+ function setDefaults ( config : unknown , mappings : SharedMappings ) {
48
+ if ( ! config [ 'library' ] ) {
49
+ config [ 'library' ] = {
50
+ type : 'module'
51
+ } ;
52
+ }
53
+
54
+ if ( ! config [ 'filename' ] ) {
55
+ config [ 'filename' ] = 'remoteEntry.js' ;
56
+ }
57
+
58
+ if ( ! config [ 'shared' ] ) {
59
+ config [ 'shared' ] = shareAll (
60
+ { singleton : true , strictVersion : true , requiredVersion : 'auto' } ) ;
61
+ }
62
+
63
+ if ( typeof config [ 'shared' ] === 'object' ) {
64
+ config [ 'shared' ] = {
65
+ ...config [ 'shared' ] ,
66
+ ...mappings . getDescriptors ( )
67
+ } ;
68
+ }
69
+
70
+ if ( Array . isArray ( config [ 'shared' ] ) ) {
71
+ config [ 'shared' ] = [
72
+ ...config [ 'shared' ] ,
73
+ mappings . getDescriptors ( )
74
+ ] ;
75
+ }
76
+ }
77
+
78
+ function createModifyEntryPlugin ( config : unknown ) {
79
+ const pinned = [ ] ;
80
+ const eager = [ ] ;
81
+ for ( const key in config [ 'shared' ] ) {
82
+ const entry = config [ 'shared' ] [ key ] ;
83
+ if ( entry . pinned ) {
84
+ pinned . push ( key ) ;
85
+ delete entry . pinned ;
86
+ }
87
+ if ( entry . eager ) {
88
+ eager . push ( key ) ;
89
+ }
90
+ }
91
+ const hasPinned = pinned . length > 0 ;
92
+ const hasEager = eager . length > 0 ;
93
+
94
+ if ( hasPinned && config [ 'remotes' ] ) {
95
+ throw new Error ( [
96
+ 'Pinned dependencies in combination with build-time remotes are not allowed. ' ,
97
+ 'Either remove "pinned: true" from all shared dependencies or delete all ' ,
98
+ 'remotes in your webpack config and use runtime remote loading instead.'
99
+ ] . join ( '' ) ) ;
100
+ }
101
+
102
+ let modifyEntryConfig = { } ;
103
+ let modifyEntryPlugin = null ;
104
+ if ( hasPinned ) {
105
+ modifyEntryConfig [ 'main' ] = { import : pinned } ;
106
+ }
107
+
108
+ if ( hasEager ) {
109
+ modifyEntryConfig [ 'styles' ] = { dependOn : [ 'main' ] } ;
110
+ modifyEntryConfig [ 'polyfills' ] = { dependOn : [ 'main' ] } ;
111
+ }
112
+
113
+ if ( hasPinned || hasEager ) {
114
+ modifyEntryPlugin = new ModifyEntryPlugin ( modifyEntryConfig ) ;
115
+ }
116
+ return modifyEntryPlugin ;
117
+ }
0 commit comments