From fa787dc35e1c29acb398048968e2f85719f619b9 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 14:49:48 -0500 Subject: [PATCH 01/15] refactor: split federation runtinme plugin into separate plugins: --- .../src/modules/FederationRuntimePlugin.ts | 53 ------------------- .../FederationRuntimePlugins/CorePlugin.ts | 32 +++++++++++ .../ResolverPlugin.ts | 29 ++++++++++ 3 files changed, 61 insertions(+), 53 deletions(-) delete mode 100644 packages/repack/src/modules/FederationRuntimePlugin.ts create mode 100644 packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts create mode 100644 packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts diff --git a/packages/repack/src/modules/FederationRuntimePlugin.ts b/packages/repack/src/modules/FederationRuntimePlugin.ts deleted file mode 100644 index e118e496d..000000000 --- a/packages/repack/src/modules/FederationRuntimePlugin.ts +++ /dev/null @@ -1,53 +0,0 @@ -import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; -import type * as RepackClient from './ScriptManager'; - -const repackFederationRuntimePlugin: () => FederationRuntimePlugin = () => ({ - name: 'repack-federation-runtime-plugin', - afterResolve(args) { - const { ScriptManager } = require('./ScriptManager') as typeof RepackClient; - const { remoteInfo } = args; - - ScriptManager.shared.addResolver( - async (scriptId, caller, referenceUrl) => { - if (scriptId === remoteInfo.entryGlobalName) { - return { url: remoteInfo.entry }; - } - - if (referenceUrl && caller === remoteInfo.entryGlobalName) { - const publicPath = remoteInfo.entry.split('/').slice(0, -1).join('/'); - const bundlePath = scriptId + referenceUrl.split(scriptId)[1]; - return { url: publicPath + '/' + bundlePath }; - } - }, - { key: remoteInfo.entryGlobalName } - ); - - return args; - }, - loadEntry: async ({ remoteInfo }) => { - const client = require('./ScriptManager') as typeof RepackClient; - const { ScriptManager, getWebpackContext } = client; - const { entry, entryGlobalName } = remoteInfo; - - try { - await ScriptManager.shared.loadScript( - entryGlobalName, - undefined, - getWebpackContext(), - entry - ); - - // @ts-ignore - if (!globalThis[entryGlobalName]) { - throw new Error(); - } - - // @ts-ignore - return globalThis[entryGlobalName]; - } catch { - console.error(`Failed to load ${entryGlobalName} entry`); - } - }, -}); - -export default repackFederationRuntimePlugin; diff --git a/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts b/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts new file mode 100644 index 000000000..23a293d87 --- /dev/null +++ b/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts @@ -0,0 +1,32 @@ +import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; +import type * as RepackClient from '../ScriptManager'; + +const repackCorePlugin: () => FederationRuntimePlugin = () => ({ + name: 'repack-core-plugin', + loadEntry: async ({ remoteInfo }) => { + const client = require('./ScriptManager') as typeof RepackClient; + const { ScriptManager, getWebpackContext } = client; + const { entry, entryGlobalName } = remoteInfo; + + try { + await ScriptManager.shared.loadScript( + entryGlobalName, + undefined, + getWebpackContext(), + entry + ); + + // @ts-ignore + if (!globalThis[entryGlobalName]) { + throw new Error(); + } + + // @ts-ignore + return globalThis[entryGlobalName]; + } catch { + console.error(`Failed to load remote entry: ${entryGlobalName}`); + } + }, +}); + +export default repackCorePlugin; diff --git a/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts b/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts new file mode 100644 index 000000000..eb9390d2d --- /dev/null +++ b/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts @@ -0,0 +1,29 @@ +import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; +import type * as RepackClient from '../ScriptManager'; + +const repackResolverPlugin: () => FederationRuntimePlugin = () => ({ + name: 'repack-resolver-plugin', + afterResolve(args) { + const { ScriptManager } = require('./ScriptManager') as typeof RepackClient; + const { remoteInfo } = args; + + ScriptManager.shared.addResolver( + async (scriptId, caller, referenceUrl) => { + if (scriptId === remoteInfo.entryGlobalName) { + return { url: remoteInfo.entry }; + } + + if (referenceUrl && caller === remoteInfo.entryGlobalName) { + const publicPath = remoteInfo.entry.split('/').slice(0, -1).join('/'); + const bundlePath = scriptId + referenceUrl.split(scriptId)[1]; + return { url: publicPath + '/' + bundlePath }; + } + }, + { key: remoteInfo.entryGlobalName } + ); + + return args; + }, +}); + +export default repackResolverPlugin; From 6a0755cff1168319bea528ad6259bb7fb598cc58 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 15:12:38 -0500 Subject: [PATCH 02/15] chore: cjs exports --- packages/repack/federation-runtime-plugin.d.ts | 2 -- packages/repack/federation-runtime-plugin.js | 2 -- packages/repack/mf/core-runtime-plugin.js | 2 ++ packages/repack/mf/resolver-runtime-plugin.js | 2 ++ 4 files changed, 4 insertions(+), 4 deletions(-) delete mode 100644 packages/repack/federation-runtime-plugin.d.ts delete mode 100644 packages/repack/federation-runtime-plugin.js create mode 100644 packages/repack/mf/core-runtime-plugin.js create mode 100644 packages/repack/mf/resolver-runtime-plugin.js diff --git a/packages/repack/federation-runtime-plugin.d.ts b/packages/repack/federation-runtime-plugin.d.ts deleted file mode 100644 index 04d05a2c2..000000000 --- a/packages/repack/federation-runtime-plugin.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -import FederationRuntimePlugin from './dist/modules/FederationRuntimePlugin'; -export default FederationRuntimePlugin; diff --git a/packages/repack/federation-runtime-plugin.js b/packages/repack/federation-runtime-plugin.js deleted file mode 100644 index 04d05a2c2..000000000 --- a/packages/repack/federation-runtime-plugin.js +++ /dev/null @@ -1,2 +0,0 @@ -import FederationRuntimePlugin from './dist/modules/FederationRuntimePlugin'; -export default FederationRuntimePlugin; diff --git a/packages/repack/mf/core-runtime-plugin.js b/packages/repack/mf/core-runtime-plugin.js new file mode 100644 index 000000000..2ff260776 --- /dev/null +++ b/packages/repack/mf/core-runtime-plugin.js @@ -0,0 +1,2 @@ +import CoreRuntimePlugin from '../dist/modules/FederationRuntimePlugins/CorePlugin'; +export default CoreRuntimePlugin; diff --git a/packages/repack/mf/resolver-runtime-plugin.js b/packages/repack/mf/resolver-runtime-plugin.js new file mode 100644 index 000000000..2a5efbecd --- /dev/null +++ b/packages/repack/mf/resolver-runtime-plugin.js @@ -0,0 +1,2 @@ +import ResolverRuntimePlugin from '../dist/modules/FederationRuntimePlugins/ResolverPlugin'; +export default ResolverRuntimePlugin; From 7f9614a8ff8386ef8d2df6f4f1a815fb2d1ffb24 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 15:23:17 -0500 Subject: [PATCH 03/15] chore: package exports --- packages/repack/package.json | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/repack/package.json b/packages/repack/package.json index 1568b8362..337853171 100644 --- a/packages/repack/package.json +++ b/packages/repack/package.json @@ -14,9 +14,13 @@ "types": "./dist/modules/ScriptManager/index.d.ts", "default": "./dist/modules/ScriptManager/index.js" }, - "./federation-runtime-plugin": { - "types": "./dist/modules/FederationRuntimePlugin.d.ts", - "default": "./dist/modules/FederationRuntimePlugin.js" + "./mf/core-plugin": { + "types": "./dist/modules/FederationRuntimePlugins/CorePlugin.d.ts", + "default": "./dist/modules/FederationRuntimePlugins/CorePlugin.js" + }, + "./mf/resolver-plugin": { + "types": "./dist/modules/FederationRuntimePlugins/ResolverPlugin.d.ts", + "default": "./dist/modules/FederationRuntimePlugins/ResolverPlugin.js" }, "./commands/webpack": { "types": "./dist/commands/webpack/index.d.ts", From 0d08fce43cff7d1d5ca7b87fb73889b50eda9a98 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 16:30:19 -0500 Subject: [PATCH 04/15] feat: add option to disable injecting runtime plugins --- .../src/plugins/ModuleFederationPluginV2.ts | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/repack/src/plugins/ModuleFederationPluginV2.ts b/packages/repack/src/plugins/ModuleFederationPluginV2.ts index 5a7cb57b7..d5c5d0843 100644 --- a/packages/repack/src/plugins/ModuleFederationPluginV2.ts +++ b/packages/repack/src/plugins/ModuleFederationPluginV2.ts @@ -11,6 +11,8 @@ import { isRspackCompiler } from './utils/isRspackCompiler'; */ export interface ModuleFederationPluginV2Config extends MF.ModuleFederationPluginOptions { + /** Whether to disable adding default runtime plugins to the configuration. */ + disableDefaultRuntimePlugins?: boolean; /** Enable or disable adding React Native deep imports to shared dependencies. Defaults to true */ reactNativeDeepImports?: boolean; } @@ -83,11 +85,14 @@ export interface ModuleFederationPluginV2Config export class ModuleFederationPluginV2 implements RspackPluginInstance { public config: MF.ModuleFederationPluginOptions; private deepImports: boolean; + private disableDefaultRuntimePlugins: boolean; constructor(pluginConfig: ModuleFederationPluginV2Config) { - const { reactNativeDeepImports, ...config } = pluginConfig; + const { disableDefaultRuntimePlugins, reactNativeDeepImports, ...config } = + pluginConfig; this.config = config; this.deepImports = reactNativeDeepImports ?? true; + this.disableDefaultRuntimePlugins = disableDefaultRuntimePlugins ?? false; } private ensureModuleFederationPackageInstalled(context: string) { @@ -105,9 +110,14 @@ export class ModuleFederationPluginV2 implements RspackPluginInstance { context: string, runtimePlugins: string[] | undefined = [] ) { - const repackRuntimePlugin = require.resolve( - '../modules/FederationRuntimePlugin' - ); + if (this.disableDefaultRuntimePlugins) { + return runtimePlugins; + } + + const defaultRuntimePlugins = [ + require.resolve('../modules/FederationRuntimePlugins/CorePlugin'), + require.resolve('../modules/FederationRuntimePlugins/ResolverPlugin'), + ]; const plugins = runtimePlugins .map((pluginPath) => { @@ -121,11 +131,13 @@ export class ModuleFederationPluginV2 implements RspackPluginInstance { }) .filter((pluginPath) => !!pluginPath) as string[]; - if (!plugins.includes(repackRuntimePlugin)) { - return [repackRuntimePlugin, ...runtimePlugins]; + for (const plugin of defaultRuntimePlugins) { + if (!plugins.includes(plugin)) { + plugins.unshift(plugin); + } } - return runtimePlugins; + return plugins; } private getModuleFederationPlugin(compiler: Compiler) { From 3cb567c3143ab906b4c6d72cdec7ac8c3502341f Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 17:28:48 -0500 Subject: [PATCH 05/15] feat: make resolver plugin configurable --- .../ResolverPlugin.ts | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts b/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts index eb9390d2d..3644c920f 100644 --- a/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts +++ b/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts @@ -1,7 +1,27 @@ import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; import type * as RepackClient from '../ScriptManager'; -const repackResolverPlugin: () => FederationRuntimePlugin = () => ({ +export type RepackResolverPluginConfiguration = + | Omit + | ((url: string) => Promise); + +const createScriptLocator = async ( + entryUrl: string, + config?: RepackResolverPluginConfiguration +) => { + if (typeof config === 'function') { + const locator = await config(entryUrl); + return locator; + } + if (typeof config === 'object') { + return { url: entryUrl, ...config }; + } + return { url: entryUrl }; +}; + +export const repackResolverPlugin: ( + config?: RepackResolverPluginConfiguration +) => FederationRuntimePlugin = (config) => ({ name: 'repack-resolver-plugin', afterResolve(args) { const { ScriptManager } = require('./ScriptManager') as typeof RepackClient; @@ -9,14 +29,19 @@ const repackResolverPlugin: () => FederationRuntimePlugin = () => ({ ScriptManager.shared.addResolver( async (scriptId, caller, referenceUrl) => { + // entry container if (scriptId === remoteInfo.entryGlobalName) { - return { url: remoteInfo.entry }; + const locator = await createScriptLocator(remoteInfo.entry, config); + return locator; } - + // entry chunks if (referenceUrl && caller === remoteInfo.entryGlobalName) { const publicPath = remoteInfo.entry.split('/').slice(0, -1).join('/'); const bundlePath = scriptId + referenceUrl.split(scriptId)[1]; - return { url: publicPath + '/' + bundlePath }; + const url = publicPath + '/' + bundlePath; + + const locator = await createScriptLocator(url, config); + return locator; } }, { key: remoteInfo.entryGlobalName } @@ -25,5 +50,3 @@ const repackResolverPlugin: () => FederationRuntimePlugin = () => ({ return args; }, }); - -export default repackResolverPlugin; From 802f2e8788726ade7ed3866c80b2521a576d7116 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 17:33:34 -0500 Subject: [PATCH 06/15] chore: use named exports --- packages/repack/mf/core-runtime-plugin.js | 4 ++-- packages/repack/mf/resolver-runtime-plugin.js | 4 ++-- .../repack/src/modules/FederationRuntimePlugins/CorePlugin.ts | 4 +--- .../src/modules/FederationRuntimePlugins/ResolverPlugin.ts | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/repack/mf/core-runtime-plugin.js b/packages/repack/mf/core-runtime-plugin.js index 2ff260776..30ffb32fd 100644 --- a/packages/repack/mf/core-runtime-plugin.js +++ b/packages/repack/mf/core-runtime-plugin.js @@ -1,2 +1,2 @@ -import CoreRuntimePlugin from '../dist/modules/FederationRuntimePlugins/CorePlugin'; -export default CoreRuntimePlugin; +import { RepackCorePlugin } from '../dist/modules/FederationRuntimePlugins/CorePlugin'; +export { RepackCorePlugin }; diff --git a/packages/repack/mf/resolver-runtime-plugin.js b/packages/repack/mf/resolver-runtime-plugin.js index 2a5efbecd..9367b0f6e 100644 --- a/packages/repack/mf/resolver-runtime-plugin.js +++ b/packages/repack/mf/resolver-runtime-plugin.js @@ -1,2 +1,2 @@ -import ResolverRuntimePlugin from '../dist/modules/FederationRuntimePlugins/ResolverPlugin'; -export default ResolverRuntimePlugin; +import { RepackResolverPlugin } from '../dist/modules/FederationRuntimePlugins/ResolverPlugin'; +export { RepackResolverPlugin }; diff --git a/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts b/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts index 23a293d87..b41cabd30 100644 --- a/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts +++ b/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts @@ -1,7 +1,7 @@ import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; import type * as RepackClient from '../ScriptManager'; -const repackCorePlugin: () => FederationRuntimePlugin = () => ({ +export const RepackCorePlugin: () => FederationRuntimePlugin = () => ({ name: 'repack-core-plugin', loadEntry: async ({ remoteInfo }) => { const client = require('./ScriptManager') as typeof RepackClient; @@ -28,5 +28,3 @@ const repackCorePlugin: () => FederationRuntimePlugin = () => ({ } }, }); - -export default repackCorePlugin; diff --git a/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts b/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts index 3644c920f..d8dea1213 100644 --- a/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts +++ b/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts @@ -19,7 +19,7 @@ const createScriptLocator = async ( return { url: entryUrl }; }; -export const repackResolverPlugin: ( +export const RepackResolverPlugin: ( config?: RepackResolverPluginConfiguration ) => FederationRuntimePlugin = (config) => ({ name: 'repack-resolver-plugin', From da366bfef10eed984989f8973ed2fc096ea2a5f9 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 17:51:51 -0500 Subject: [PATCH 07/15] test: update tests --- .../ModuleFederationPluginV2.test.ts | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/repack/src/plugins/__tests__/ModuleFederationPluginV2.test.ts b/packages/repack/src/plugins/__tests__/ModuleFederationPluginV2.test.ts index 815f2fa27..fda6eeed5 100644 --- a/packages/repack/src/plugins/__tests__/ModuleFederationPluginV2.test.ts +++ b/packages/repack/src/plugins/__tests__/ModuleFederationPluginV2.test.ts @@ -16,8 +16,11 @@ const mockPlugin = MFPluginRspack as unknown as jest.Mock< typeof MFPluginRspack >; -const runtimePluginPath = require.resolve( - '../../modules/FederationRuntimePlugin' +const corePluginPath = require.resolve( + '../../modules/FederationRuntimePlugins/CorePlugin' +); +const resolverPluginPath = require.resolve( + '../../modules/FederationRuntimePlugins/ResolverPlugin' ); describe('ModuleFederationPlugin', () => { @@ -136,22 +139,24 @@ describe('ModuleFederationPlugin', () => { expect(config.shared['@react-native/'].eager).toBe(false); }); - it('should add FederationRuntimePlugin to runtime plugins', () => { + it('should add CorePlugin & ResolverPlugin to runtime plugins by default', () => { new ModuleFederationPluginV2({ name: 'test' }).apply(mockCompiler); const config = mockPlugin.mock.calls[0][0]; - expect(config.runtimePlugins).toContain(runtimePluginPath); + expect(config.runtimePlugins).toContain(corePluginPath); + expect(config.runtimePlugins).toContain(resolverPluginPath); }); - it('should not add FederationRuntimePlugin to runtime plugins when already present', () => { + it('should not add duplicate default runtime plugins when already present', () => { new ModuleFederationPluginV2({ name: 'test', - runtimePlugins: [runtimePluginPath], + runtimePlugins: [corePluginPath, resolverPluginPath], }).apply(mockCompiler); const config = mockPlugin.mock.calls[0][0]; - expect(config.runtimePlugins).toContain(runtimePluginPath); - expect(config.runtimePlugins).toHaveLength(1); + expect(config.runtimePlugins).toContain(corePluginPath); + expect(config.runtimePlugins).toContain(resolverPluginPath); + expect(config.runtimePlugins).toHaveLength(2); }); it('should use loaded-first as default shareStrategy', () => { From 7d59df91f2c56435a0bd42b4a48ea737785f688b Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 17:54:36 -0500 Subject: [PATCH 08/15] fix: plugin import naming --- packages/repack/mf/{core-runtime-plugin.js => core-plugin.js} | 0 .../repack/mf/{resolver-runtime-plugin.js => resolver-plugin.js} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename packages/repack/mf/{core-runtime-plugin.js => core-plugin.js} (100%) rename packages/repack/mf/{resolver-runtime-plugin.js => resolver-plugin.js} (100%) diff --git a/packages/repack/mf/core-runtime-plugin.js b/packages/repack/mf/core-plugin.js similarity index 100% rename from packages/repack/mf/core-runtime-plugin.js rename to packages/repack/mf/core-plugin.js diff --git a/packages/repack/mf/resolver-runtime-plugin.js b/packages/repack/mf/resolver-plugin.js similarity index 100% rename from packages/repack/mf/resolver-runtime-plugin.js rename to packages/repack/mf/resolver-plugin.js From 3a5a1192c292e54399a213487e8b6724a5b47aa7 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 18:21:07 -0500 Subject: [PATCH 09/15] chore: add changeset --- .changeset/wet-countries-sin.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wet-countries-sin.md diff --git a/.changeset/wet-countries-sin.md b/.changeset/wet-countries-sin.md new file mode 100644 index 000000000..cd1137552 --- /dev/null +++ b/.changeset/wet-countries-sin.md @@ -0,0 +1,5 @@ +--- +"@callstack/repack": minor +--- + +Refactor FederationRuntimePlugin into two separate plugins for more granular control over the MF2 runtime behaviour (CorePlugin & ResolverPlugin) From d79d046601ee815abd702c11e1a8c57d37b77088 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 19:21:15 -0500 Subject: [PATCH 10/15] fix: reconfigure package exports --- packages/repack/client.d.ts | 1 - packages/repack/client.js | 1 - packages/repack/client/index.js | 1 + packages/repack/package.json | 45 +++++++-------------------------- packages/repack/tsconfig.json | 11 +++----- 5 files changed, 13 insertions(+), 46 deletions(-) delete mode 100644 packages/repack/client.d.ts delete mode 100644 packages/repack/client.js create mode 100644 packages/repack/client/index.js diff --git a/packages/repack/client.d.ts b/packages/repack/client.d.ts deleted file mode 100644 index 6ca051f49..000000000 --- a/packages/repack/client.d.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './dist/modules/ScriptManager'; diff --git a/packages/repack/client.js b/packages/repack/client.js deleted file mode 100644 index 6ca051f49..000000000 --- a/packages/repack/client.js +++ /dev/null @@ -1 +0,0 @@ -export * from './dist/modules/ScriptManager'; diff --git a/packages/repack/client/index.js b/packages/repack/client/index.js new file mode 100644 index 000000000..c32d6c64a --- /dev/null +++ b/packages/repack/client/index.js @@ -0,0 +1 @@ +export * from '../dist/modules/ScriptManager'; diff --git a/packages/repack/package.json b/packages/repack/package.json index 337853171..9962a848c 100644 --- a/packages/repack/package.json +++ b/packages/repack/package.json @@ -6,38 +6,13 @@ "types": "./dist/index.d.ts", "react-native": "", "exports": { - ".": { - "types": "./dist/index.d.ts", - "default": "./dist/index.js" - }, - "./client": { - "types": "./dist/modules/ScriptManager/index.d.ts", - "default": "./dist/modules/ScriptManager/index.js" - }, - "./mf/core-plugin": { - "types": "./dist/modules/FederationRuntimePlugins/CorePlugin.d.ts", - "default": "./dist/modules/FederationRuntimePlugins/CorePlugin.js" - }, - "./mf/resolver-plugin": { - "types": "./dist/modules/FederationRuntimePlugins/ResolverPlugin.d.ts", - "default": "./dist/modules/FederationRuntimePlugins/ResolverPlugin.js" - }, - "./commands/webpack": { - "types": "./dist/commands/webpack/index.d.ts", - "default": "./dist/commands/webpack/index.js" - }, - "./commands/rspack": { - "types": "./dist/commands/rspack/index.d.ts", - "default": "./dist/commands/rspack/index.js" - }, - "./assets-loader": { - "types": "./dist/loaders/assetsLoader/index.d.ts", - "default": "./dist/loaders/assetsLoader/index.js" - }, - "./flow-loader": { - "types": "./dist/loaders/flowLoader/index.d.ts", - "default": "./dist/loaders/flowLoader/index.js" - }, + ".": "./dist/index.js", + "./client": "./client/index.js", + "./commands/rspack": "./dist/commands/rspack/index.js", + "./commands/webpack": "./dist/commands/webpack/index.js", + "./assets-loader": "./dist/loaders/assetsLoader/index.js", + "./flow-loader": "./dist/loaders/flowLoader/index.js", + "./mf/*": "./mf/*.js", "./package.json": "./package.json" }, "files": [ @@ -46,10 +21,8 @@ "!android/build", "ios", "!ios/build", - "client.js", - "client.d.ts", - "federation-runtime-plugin.js", - "federation-runtime-plugin.d.ts", + "client", + "mf", "callstack-repack.podspec", "src/modules/ScriptManager/NativeScriptManager.ts" ], diff --git a/packages/repack/tsconfig.json b/packages/repack/tsconfig.json index f62f95513..a033b264e 100644 --- a/packages/repack/tsconfig.json +++ b/packages/repack/tsconfig.json @@ -1,11 +1,11 @@ { "compilerOptions": { "target": "ES2020", - "module": "commonjs", "lib": ["ES2020", "ES2021"], "declaration": true, "strict": true, - "moduleResolution": "node", + "module": "preserve", + "moduleResolution": "bundler", "allowSyntheticDefaultImports": true, "esModuleInterop": true, "skipLibCheck": true, @@ -20,10 +20,5 @@ ] } }, - "include": [ - "./client.d.ts", - "./commands.d.ts", - "src/**/*", - "../dev-server/src" - ] + "include": ["./commands.d.ts", "src/**/*", "../dev-server/src"] } From 21bad2b6a285fb3197bda6cbd1a4b05c3501e739 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Mon, 25 Nov 2024 19:22:08 -0500 Subject: [PATCH 11/15] chore: remove empty RN condition --- packages/repack/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/repack/package.json b/packages/repack/package.json index 9962a848c..1753ade54 100644 --- a/packages/repack/package.json +++ b/packages/repack/package.json @@ -4,7 +4,6 @@ "description": "A toolkit to build your React Native application with Rspack or Webpack.", "main": "./dist/index.js", "types": "./dist/index.d.ts", - "react-native": "", "exports": { ".": "./dist/index.js", "./client": "./client/index.js", From 6d9da6f1e2969ccd269f57cca598b8107fc89ec3 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Tue, 26 Nov 2024 17:02:10 -0500 Subject: [PATCH 12/15] fix: add d.ts files with typings --- packages/repack/client/index.d.ts | 1 + packages/repack/mf/core-plugin.d.ts | 1 + packages/repack/mf/resolver-plugin.d.ts | 1 + 3 files changed, 3 insertions(+) create mode 100644 packages/repack/client/index.d.ts create mode 100644 packages/repack/mf/core-plugin.d.ts create mode 100644 packages/repack/mf/resolver-plugin.d.ts diff --git a/packages/repack/client/index.d.ts b/packages/repack/client/index.d.ts new file mode 100644 index 000000000..c32d6c64a --- /dev/null +++ b/packages/repack/client/index.d.ts @@ -0,0 +1 @@ +export * from '../dist/modules/ScriptManager'; diff --git a/packages/repack/mf/core-plugin.d.ts b/packages/repack/mf/core-plugin.d.ts new file mode 100644 index 000000000..f4a7d71bb --- /dev/null +++ b/packages/repack/mf/core-plugin.d.ts @@ -0,0 +1 @@ +export { RepackCorePlugin } from '../dist/modules/FederationRuntimePlugins/CorePlugin'; diff --git a/packages/repack/mf/resolver-plugin.d.ts b/packages/repack/mf/resolver-plugin.d.ts new file mode 100644 index 000000000..95302d99f --- /dev/null +++ b/packages/repack/mf/resolver-plugin.d.ts @@ -0,0 +1 @@ +export { RepackResolverPlugin } from '../dist/modules/FederationRuntimePlugins/ResolverPlugin'; From 86d236a029586677a09af496990280cf37ffba5b Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Tue, 26 Nov 2024 17:24:18 -0500 Subject: [PATCH 13/15] chore: update tester apps configs --- apps/tester-federation-v2/ios/Podfile.lock | 4 ++-- apps/tester-federation-v2/rspack.config.host-app.mjs | 5 ----- apps/tester-federation-v2/webpack.config.host-app.mjs | 5 ----- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/apps/tester-federation-v2/ios/Podfile.lock b/apps/tester-federation-v2/ios/Podfile.lock index e73868fb4..9ea5abb19 100644 --- a/apps/tester-federation-v2/ios/Podfile.lock +++ b/apps/tester-federation-v2/ios/Podfile.lock @@ -1,6 +1,6 @@ PODS: - boost (1.84.0) - - callstack-repack (5.0.0-rc.0): + - callstack-repack (5.0.0-rc.2): - DoubleConversion - glog - hermes-engine @@ -1895,7 +1895,7 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: boost: 1dca942403ed9342f98334bf4c3621f011aa7946 - callstack-repack: 75464b0e26467fc4a7236373399bc0fc2281f495 + callstack-repack: 3106db24c24f7a76a380230ff7794d225cecb760 DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385 FBLazyVector: 7075bb12898bc3998fd60f4b7ca422496cc2cdf7 fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be diff --git a/apps/tester-federation-v2/rspack.config.host-app.mjs b/apps/tester-federation-v2/rspack.config.host-app.mjs index 6adb34a29..03d3069a0 100644 --- a/apps/tester-federation-v2/rspack.config.host-app.mjs +++ b/apps/tester-federation-v2/rspack.config.host-app.mjs @@ -1,11 +1,9 @@ // @ts-check -import { createRequire } from 'node:module'; import path from 'node:path'; import * as Repack from '@callstack/repack'; import rspack from '@rspack/core'; const dirname = Repack.getDirname(import.meta.url); -const { resolve } = createRequire(import.meta.url); /** @type {(env: import('@callstack/repack').EnvOptions) => import('@rspack/core').Configuration} */ export default (env) => { @@ -121,9 +119,6 @@ export default (env) => { MiniApp: `MiniApp@http://localhost:8082/${platform}/mf-manifest.json`, }, dts: false, - runtimePlugins: [ - resolve('@callstack/repack/federation-runtime-plugin'), - ], shared: { react: { singleton: true, diff --git a/apps/tester-federation-v2/webpack.config.host-app.mjs b/apps/tester-federation-v2/webpack.config.host-app.mjs index 770c95e5d..c9a4f4cb9 100644 --- a/apps/tester-federation-v2/webpack.config.host-app.mjs +++ b/apps/tester-federation-v2/webpack.config.host-app.mjs @@ -1,11 +1,9 @@ // @ts-check -import { createRequire } from 'node:module'; import path from 'node:path'; import * as Repack from '@callstack/repack'; import webpack from 'webpack'; const dirname = Repack.getDirname(import.meta.url); -const { resolve } = createRequire(import.meta.url); /** @type {(env: import('@callstack/repack').EnvOptions) => import('webpack').Configuration} */ export default (env) => { @@ -105,9 +103,6 @@ export default (env) => { remotes: { MiniApp: `MiniApp@http://localhost:8082/${platform}/mf-manifest.json`, }, - runtimePlugins: [ - resolve('@callstack/repack/federation-runtime-plugin'), - ], shared: { react: { singleton: true, From a59e4d5c614c3551b5238ad68c944b01a6f9a0a2 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Tue, 26 Nov 2024 17:24:41 -0500 Subject: [PATCH 14/15] fix: use default export --- packages/repack/mf/core-plugin.d.ts | 2 +- packages/repack/mf/core-plugin.js | 4 ++-- packages/repack/mf/resolver-plugin.d.ts | 2 +- packages/repack/mf/resolver-plugin.js | 4 ++-- .../src/modules/FederationRuntimePlugins/CorePlugin.ts | 6 ++++-- .../src/modules/FederationRuntimePlugins/ResolverPlugin.ts | 7 +++++-- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/repack/mf/core-plugin.d.ts b/packages/repack/mf/core-plugin.d.ts index f4a7d71bb..c968a3b65 100644 --- a/packages/repack/mf/core-plugin.d.ts +++ b/packages/repack/mf/core-plugin.d.ts @@ -1 +1 @@ -export { RepackCorePlugin } from '../dist/modules/FederationRuntimePlugins/CorePlugin'; +export { default } from '../dist/modules/FederationRuntimePlugins/CorePlugin'; diff --git a/packages/repack/mf/core-plugin.js b/packages/repack/mf/core-plugin.js index 30ffb32fd..fb650e84b 100644 --- a/packages/repack/mf/core-plugin.js +++ b/packages/repack/mf/core-plugin.js @@ -1,2 +1,2 @@ -import { RepackCorePlugin } from '../dist/modules/FederationRuntimePlugins/CorePlugin'; -export { RepackCorePlugin }; +import RepackCorePlugin from '../dist/modules/FederationRuntimePlugins/CorePlugin'; +export default RepackCorePlugin; diff --git a/packages/repack/mf/resolver-plugin.d.ts b/packages/repack/mf/resolver-plugin.d.ts index 95302d99f..a0afc981b 100644 --- a/packages/repack/mf/resolver-plugin.d.ts +++ b/packages/repack/mf/resolver-plugin.d.ts @@ -1 +1 @@ -export { RepackResolverPlugin } from '../dist/modules/FederationRuntimePlugins/ResolverPlugin'; +export { default } from '../dist/modules/FederationRuntimePlugins/ResolverPlugin'; diff --git a/packages/repack/mf/resolver-plugin.js b/packages/repack/mf/resolver-plugin.js index 9367b0f6e..77f215723 100644 --- a/packages/repack/mf/resolver-plugin.js +++ b/packages/repack/mf/resolver-plugin.js @@ -1,2 +1,2 @@ -import { RepackResolverPlugin } from '../dist/modules/FederationRuntimePlugins/ResolverPlugin'; -export { RepackResolverPlugin }; +import RepackResolverPlugin from '../dist/modules/FederationRuntimePlugins/ResolverPlugin'; +export default RepackResolverPlugin; diff --git a/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts b/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts index b41cabd30..d4fbd237e 100644 --- a/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts +++ b/packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts @@ -1,10 +1,10 @@ import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; import type * as RepackClient from '../ScriptManager'; -export const RepackCorePlugin: () => FederationRuntimePlugin = () => ({ +const RepackCorePlugin: () => FederationRuntimePlugin = () => ({ name: 'repack-core-plugin', loadEntry: async ({ remoteInfo }) => { - const client = require('./ScriptManager') as typeof RepackClient; + const client = require('../ScriptManager') as typeof RepackClient; const { ScriptManager, getWebpackContext } = client; const { entry, entryGlobalName } = remoteInfo; @@ -28,3 +28,5 @@ export const RepackCorePlugin: () => FederationRuntimePlugin = () => ({ } }, }); + +export default RepackCorePlugin; diff --git a/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts b/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts index d8dea1213..2560c3772 100644 --- a/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts +++ b/packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts @@ -19,12 +19,13 @@ const createScriptLocator = async ( return { url: entryUrl }; }; -export const RepackResolverPlugin: ( +const RepackResolverPlugin: ( config?: RepackResolverPluginConfiguration ) => FederationRuntimePlugin = (config) => ({ name: 'repack-resolver-plugin', afterResolve(args) { - const { ScriptManager } = require('./ScriptManager') as typeof RepackClient; + const { ScriptManager } = + require('../ScriptManager') as typeof RepackClient; const { remoteInfo } = args; ScriptManager.shared.addResolver( @@ -50,3 +51,5 @@ export const RepackResolverPlugin: ( return args; }, }); + +export default RepackResolverPlugin; From 188915557b6fef362ea8870aa1f7bea4b7f01a73 Mon Sep 17 00:00:00 2001 From: Jakub Romanczyk Date: Sun, 1 Dec 2024 22:45:49 -0500 Subject: [PATCH 15/15] refactor: change to list of default runtime plugins --- .../src/plugins/ModuleFederationPluginV2.ts | 36 ++++++++++--------- .../ModuleFederationPluginV2.test.ts | 6 ++-- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/repack/src/plugins/ModuleFederationPluginV2.ts b/packages/repack/src/plugins/ModuleFederationPluginV2.ts index d5c5d0843..6fca61c0d 100644 --- a/packages/repack/src/plugins/ModuleFederationPluginV2.ts +++ b/packages/repack/src/plugins/ModuleFederationPluginV2.ts @@ -11,8 +11,15 @@ import { isRspackCompiler } from './utils/isRspackCompiler'; */ export interface ModuleFederationPluginV2Config extends MF.ModuleFederationPluginOptions { - /** Whether to disable adding default runtime plugins to the configuration. */ - disableDefaultRuntimePlugins?: boolean; + /** + * List of default runtime plugins for Federation Runtime. + * Useful if you want to modify or disable behaviour of runtime plugins. + * + * Defaults to an array containing: + * - '@callstack/repack/mf/core-plugin + * - '@callstack/repack/mf/resolver-plugin + */ + defaultRuntimePlugins?: string[]; /** Enable or disable adding React Native deep imports to shared dependencies. Defaults to true */ reactNativeDeepImports?: boolean; } @@ -85,14 +92,17 @@ export interface ModuleFederationPluginV2Config export class ModuleFederationPluginV2 implements RspackPluginInstance { public config: MF.ModuleFederationPluginOptions; private deepImports: boolean; - private disableDefaultRuntimePlugins: boolean; + private defaultRuntimePlugins: string[]; constructor(pluginConfig: ModuleFederationPluginV2Config) { - const { disableDefaultRuntimePlugins, reactNativeDeepImports, ...config } = + const { defaultRuntimePlugins, reactNativeDeepImports, ...config } = pluginConfig; this.config = config; this.deepImports = reactNativeDeepImports ?? true; - this.disableDefaultRuntimePlugins = disableDefaultRuntimePlugins ?? false; + this.defaultRuntimePlugins = defaultRuntimePlugins ?? [ + '@callstack/repack/mf/core-plugin', + '@callstack/repack/mf/resolver-plugin', + ]; } private ensureModuleFederationPackageInstalled(context: string) { @@ -110,15 +120,6 @@ export class ModuleFederationPluginV2 implements RspackPluginInstance { context: string, runtimePlugins: string[] | undefined = [] ) { - if (this.disableDefaultRuntimePlugins) { - return runtimePlugins; - } - - const defaultRuntimePlugins = [ - require.resolve('../modules/FederationRuntimePlugins/CorePlugin'), - require.resolve('../modules/FederationRuntimePlugins/ResolverPlugin'), - ]; - const plugins = runtimePlugins .map((pluginPath) => { try { @@ -131,9 +132,10 @@ export class ModuleFederationPluginV2 implements RspackPluginInstance { }) .filter((pluginPath) => !!pluginPath) as string[]; - for (const plugin of defaultRuntimePlugins) { - if (!plugins.includes(plugin)) { - plugins.unshift(plugin); + for (const plugin of this.defaultRuntimePlugins) { + const pluginPath = require.resolve(plugin); + if (!plugins.includes(pluginPath)) { + plugins.unshift(pluginPath); } } diff --git a/packages/repack/src/plugins/__tests__/ModuleFederationPluginV2.test.ts b/packages/repack/src/plugins/__tests__/ModuleFederationPluginV2.test.ts index fda6eeed5..380603b66 100644 --- a/packages/repack/src/plugins/__tests__/ModuleFederationPluginV2.test.ts +++ b/packages/repack/src/plugins/__tests__/ModuleFederationPluginV2.test.ts @@ -16,11 +16,9 @@ const mockPlugin = MFPluginRspack as unknown as jest.Mock< typeof MFPluginRspack >; -const corePluginPath = require.resolve( - '../../modules/FederationRuntimePlugins/CorePlugin' -); +const corePluginPath = require.resolve('@callstack/repack/mf/core-plugin'); const resolverPluginPath = require.resolve( - '../../modules/FederationRuntimePlugins/ResolverPlugin' + '@callstack/repack/mf/resolver-plugin' ); describe('ModuleFederationPlugin', () => {