Skip to content

Commit 2c2f4a8

Browse files
committed
feat: refactor
1 parent 5bb5abb commit 2c2f4a8

File tree

57 files changed

+2500
-2543
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2500
-2543
lines changed

apps/shared-treeshake/host/modern.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { appTools, defineConfig } from '@modern-js/app-tools';
22
import { serverPlugin } from '@modern-js/plugin-server';
33
import {
44
// DependencyReferencExportPlugin,
5-
IndependentSharePlugin,
5+
IndependentSharedPlugin,
66
ModuleFederationPlugin,
77
} from '@module-federation/enhanced';
88
import mfConfig from './module-federation.config';
@@ -74,7 +74,7 @@ export default defineConfig({
7474
// chain.optimization.minimize(false)
7575
chain.optimization.runtimeChunk(false);
7676
if (isReShake) {
77-
chain.plugin('IndependentSharePlugin').use(IndependentSharePlugin, [
77+
chain.plugin('IndependentSharedPlugin').use(IndependentSharedPlugin, [
7878
{
7979
// @ts-ignore
8080
mfConfig,

apps/shared-treeshake/host/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@babel/runtime": "7.28.2",
3131
"@modern-js/runtime": "2.68.0",
3232
"@module-federation/enhanced": "workspace:*",
33-
"antd": "4.24.15",
33+
"antd": "5.29.1",
3434
"react": "~18.3.1",
3535
"react-dom": "~18.3.1"
3636
},

apps/shared-treeshake/provider/modern.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { appTools, defineConfig } from '@modern-js/app-tools';
22
import {
33
ModuleFederationPlugin,
4-
IndependentSharePlugin,
4+
IndependentSharedPlugin,
55
} from '@module-federation/enhanced';
66
import mfConfig from './module-federation.config';
77

@@ -57,7 +57,7 @@ export default defineConfig({
5757
// chain.optimization.minimize(false)
5858
chain.optimization.runtimeChunk(false);
5959
chain.plugin('MF').use(ModuleFederationPlugin, [mfConfig]);
60-
// chain.plugin('IndependentSharePlugin').use(IndependentSharePlugin, [
60+
// chain.plugin('IndependentSharedPlugin').use(IndependentSharedPlugin, [
6161
// {
6262
// // @ts-ignore
6363
// mfConfig,

packages/enhanced/src/declarations/plugins/sharing/SharePlugin.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export interface SharedConfig {
103103

104104
treeshake?: boolean;
105105
usedExports?: string[];
106+
independentShareFileName?: string;
106107
}
107108

108109
export interface IncludeExcludeOptions {

packages/enhanced/src/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ export { default as FederationModulesPlugin } from './wrapper/FederationModulesP
1212
export { default as FederationRuntimePlugin } from './wrapper/FederationRuntimePlugin';
1313
export { default as AsyncBoundaryPlugin } from './wrapper/AsyncBoundaryPlugin';
1414
export { default as HoistContainerReferencesPlugin } from './wrapper/HoistContainerReferencesPlugin';
15-
export { default as IndependentSharePlugin } from './wrapper/IndependentSharePlugin';
1615
export { default as TreeshakeSharePlugin } from './wrapper/TreeshakeSharePlugin';
17-
export { default as TreeshakeConsumeSharedPlugin } from './wrapper/TreeshakeConsumeSharedPlugin';
18-
export { default as CollectSharedEntryPlugin } from './wrapper/CollectSharedEntryPlugin';
19-
export { default as ShareContainerPlugin } from './wrapper/ShareContainerPlugin';
2016

2117
export const dependencies = {
2218
get ContainerEntryDependency() {

packages/enhanced/src/lib/container/ContainerPlugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
MIT License http://www.opensource.org/licenses/mit-license.php
3-
Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
44
*/
55
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
66
import ContainerEntryDependency from './ContainerEntryDependency';
@@ -54,7 +54,7 @@ class ContainerPlugin {
5454
name: options.name,
5555
shareScope: options.shareScope || 'default',
5656
library: options.library || {
57-
type: 'var',
57+
type: 'global',
5858
name: options.name,
5959
},
6060
runtime: options.runtime,

packages/enhanced/src/lib/container/ModuleFederationPlugin.ts

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
2626
import FederationModulesPlugin from './runtime/FederationModulesPlugin';
2727
import { createSchemaValidation } from '../../utils';
2828
import TreeshakeSharePlugin from '../sharing/treeshake/TreeshakeSharePlugin';
29-
import { MakeRequired } from '../sharing/treeshake/IndependentSharePlugin';
29+
import { MakeRequired } from '../sharing/treeshake/IndependentSharedPlugin';
3030

3131
const 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

5251
class 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
});

packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ import type {
77
Compilation,
88
Chunk,
99
} from 'webpack';
10-
import type { EntryDescription } from 'webpack/lib/Entrypoint';
1110
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
1211
import { PrefetchPlugin } from '@module-federation/data-prefetch/cli';
1312
import { moduleFederationPlugin } from '@module-federation/sdk';
1413
import FederationRuntimeModule from './FederationRuntimeModule';
1514
import {
1615
getFederationGlobalScope,
1716
normalizeRuntimeInitOptionsWithOutShared,
18-
modifyEntry,
1917
createHash,
2018
normalizeToPosixPath,
2119
} from './utils';
@@ -73,7 +71,6 @@ class FederationRuntimePlugin {
7371
compiler: Compiler,
7472
options: moduleFederationPlugin.ModuleFederationPluginOptions,
7573
bundlerRuntimePath?: string,
76-
experiments?: moduleFederationPlugin.ModuleFederationPluginOptions['experiments'],
7774
) {
7875
// internal runtime plugin
7976
const runtimePlugins = options.runtimePlugins;
@@ -177,7 +174,6 @@ class FederationRuntimePlugin {
177174
compiler,
178175
this.options,
179176
this.bundlerRuntimePath,
180-
this.options.experiments,
181177
)}`,
182178
);
183179
entryFilePath = path.join(TEMP_DIR, `entry.${hash}.js`);
@@ -187,7 +183,6 @@ class FederationRuntimePlugin {
187183
compiler,
188184
this.options,
189185
this.bundlerRuntimePath,
190-
this.options.experiments,
191186
),
192187
)}`;
193188
}
@@ -216,7 +211,6 @@ class FederationRuntimePlugin {
216211
compiler,
217212
this.options,
218213
this.bundlerRuntimePath,
219-
this.options.experiments,
220214
),
221215
);
222216
}

packages/enhanced/src/lib/sharing/ConsumeSharedPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class ConsumeSharedPlugin {
8383

8484
constructor(options: ConsumeSharedPluginOptions) {
8585
if (typeof options !== 'string') {
86-
// validate(options);
86+
validate(options);
8787
}
8888

8989
this._consumes = parseOptions(

packages/enhanced/src/lib/sharing/ConsumeSharedRuntimeModule.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ class ConsumeSharedRuntimeModule extends RuntimeModule {
138138
'installedModules:installedModules,',
139139
`moduleToHandlerMapping,`,
140140
`webpackRequire: ${RuntimeGlobals.require}`,
141+
`asyncLoad: typeof options==='object' ? options.asyncLoad : undefined,`,
141142
]),
142143
`})`,
143144
]),
144-
'',
145+
'options',
145146
)}`,
146147
])
147148
: '// no consumes in initial chunks',

0 commit comments

Comments
 (0)