@@ -32,15 +32,33 @@ class SharePlugin {
32
32
private _shareScope : string | string [ ] ;
33
33
private _consumes : Record < string , ConsumesConfig > [ ] ;
34
34
private _provides : Record < string , ProvidesConfig > [ ] ;
35
+ private _options : SharePluginOptions ;
35
36
36
37
constructor ( options : SharePluginOptions ) {
38
+ // Validate options first
37
39
validate ( options ) ;
38
40
41
+ // Store options for debugging and introspection
42
+ this . _options = options ;
43
+
44
+ // Validate shared configuration
45
+ if (
46
+ ! options . shared ||
47
+ ( typeof options . shared === 'object' &&
48
+ Object . keys ( options . shared ) . length === 0 )
49
+ ) {
50
+ throw new Error (
51
+ 'SharePlugin requires at least one shared module configuration' ,
52
+ ) ;
53
+ }
54
+
39
55
const sharedOptions : [ string , SharedConfig ] [ ] = parseOptions (
40
56
options . shared ,
41
57
( item , key ) => {
42
58
if ( typeof item !== 'string' )
43
- throw new Error ( 'Unexpected array in shared' ) ;
59
+ throw new Error (
60
+ `Unexpected array in shared configuration for key "${ key } "` ,
61
+ ) ;
44
62
const config : SharedConfig =
45
63
item === key || ! isRequiredVersion ( item )
46
64
? {
@@ -52,7 +70,15 @@ class SharePlugin {
52
70
} ;
53
71
return config ;
54
72
} ,
55
- ( item ) => item ,
73
+ ( item , key ) => {
74
+ // Enhanced validation for shared config
75
+ if ( item . include && item . exclude ) {
76
+ throw new Error (
77
+ `Cannot specify both include and exclude filters for shared module "${ key } "` ,
78
+ ) ;
79
+ }
80
+ return item ;
81
+ } ,
56
82
) ;
57
83
const consumes : Record < string , ConsumesConfig > [ ] = sharedOptions . map (
58
84
( [ key , options ] ) => ( {
@@ -100,6 +126,62 @@ class SharePlugin {
100
126
this . _provides = provides ;
101
127
}
102
128
129
+ /**
130
+ * Get the configuration used to initialize this plugin
131
+ * @returns The original SharePluginOptions
132
+ */
133
+ getOptions ( ) : SharePluginOptions {
134
+ return this . _options ;
135
+ }
136
+
137
+ /**
138
+ * Get the share scope name(s) used by this plugin
139
+ * @returns The share scope name or array of names
140
+ */
141
+ getShareScope ( ) : string | string [ ] {
142
+ return this . _shareScope ;
143
+ }
144
+
145
+ /**
146
+ * Get the consume configurations generated by this plugin
147
+ * @returns Array of consume configurations
148
+ */
149
+ getConsumes ( ) : Record < string , ConsumesConfig > [ ] {
150
+ return this . _consumes ;
151
+ }
152
+
153
+ /**
154
+ * Get the provide configurations generated by this plugin
155
+ * @returns Array of provide configurations
156
+ */
157
+ getProvides ( ) : Record < string , ProvidesConfig > [ ] {
158
+ return this . _provides ;
159
+ }
160
+
161
+ /**
162
+ * Get information about shared modules
163
+ * @returns Object containing shared module information
164
+ */
165
+ getSharedInfo ( ) : {
166
+ totalShared : number ;
167
+ consumeOnly : number ;
168
+ provideAndConsume : number ;
169
+ shareScopes : string [ ] ;
170
+ } {
171
+ const consumeOnlyCount = this . _consumes . length - this . _provides . length ;
172
+ const provideAndConsumeCount = this . _provides . length ;
173
+ const shareScopes = Array . isArray ( this . _shareScope )
174
+ ? this . _shareScope
175
+ : [ this . _shareScope ] ;
176
+
177
+ return {
178
+ totalShared : this . _consumes . length ,
179
+ consumeOnly : consumeOnlyCount ,
180
+ provideAndConsume : provideAndConsumeCount ,
181
+ shareScopes : shareScopes ,
182
+ } ;
183
+ }
184
+
103
185
/**
104
186
* Applies the plugin to the webpack compiler instance
105
187
* @param compiler - The webpack compiler instance
@@ -108,14 +190,27 @@ class SharePlugin {
108
190
process . env [ 'FEDERATION_WEBPACK_PATH' ] =
109
191
process . env [ 'FEDERATION_WEBPACK_PATH' ] || getWebpackPath ( compiler ) ;
110
192
111
- new ConsumeSharedPlugin ( {
112
- shareScope : this . _shareScope ,
113
- consumes : this . _consumes ,
114
- } ) . apply ( compiler ) ;
115
- new ProvideSharedPlugin ( {
116
- shareScope : this . _shareScope ,
117
- provides : this . _provides ,
118
- } ) . apply ( compiler ) ;
193
+ // Apply ConsumeSharedPlugin with enhanced error handling
194
+ try {
195
+ new ConsumeSharedPlugin ( {
196
+ shareScope : this . _shareScope ,
197
+ consumes : this . _consumes ,
198
+ } ) . apply ( compiler ) ;
199
+ } catch ( error ) {
200
+ const message = error instanceof Error ? error . message : String ( error ) ;
201
+ throw new Error ( `Failed to apply ConsumeSharedPlugin: ${ message } ` ) ;
202
+ }
203
+
204
+ // Apply ProvideSharedPlugin with enhanced error handling
205
+ try {
206
+ new ProvideSharedPlugin ( {
207
+ shareScope : this . _shareScope ,
208
+ provides : this . _provides ,
209
+ } ) . apply ( compiler ) ;
210
+ } catch ( error ) {
211
+ const message = error instanceof Error ? error . message : String ( error ) ;
212
+ throw new Error ( `Failed to apply ProvideSharedPlugin: ${ message } ` ) ;
213
+ }
119
214
}
120
215
}
121
216
0 commit comments