-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split federation runtime plugin into separate plugins (#803)
* refactor: split federation runtinme plugin into separate plugins: * chore: cjs exports * chore: package exports * feat: add option to disable injecting runtime plugins * feat: make resolver plugin configurable * chore: use named exports * test: update tests * fix: plugin import naming * chore: add changeset * fix: reconfigure package exports * chore: remove empty RN condition * fix: add d.ts files with typings * chore: update tester apps configs * fix: use default export * refactor: change to list of default runtime plugins
- Loading branch information
Showing
21 changed files
with
147 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@callstack/repack": minor | ||
--- | ||
|
||
Refactor FederationRuntimePlugin into two separate plugins for more granular control over the MF2 runtime behaviour (CorePlugin & ResolverPlugin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../dist/modules/ScriptManager'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../dist/modules/ScriptManager'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '../dist/modules/FederationRuntimePlugins/CorePlugin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import RepackCorePlugin from '../dist/modules/FederationRuntimePlugins/CorePlugin'; | ||
export default RepackCorePlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '../dist/modules/FederationRuntimePlugins/ResolverPlugin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import RepackResolverPlugin from '../dist/modules/FederationRuntimePlugins/ResolverPlugin'; | ||
export default RepackResolverPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
packages/repack/src/modules/FederationRuntimePlugins/CorePlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
55 changes: 55 additions & 0 deletions
55
packages/repack/src/modules/FederationRuntimePlugins/ResolverPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; | ||
import type * as RepackClient from '../ScriptManager'; | ||
|
||
export type RepackResolverPluginConfiguration = | ||
| Omit<RepackClient.ScriptLocator, 'url'> | ||
| ((url: string) => Promise<RepackClient.ScriptLocator>); | ||
|
||
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 }; | ||
}; | ||
|
||
const RepackResolverPlugin: ( | ||
config?: RepackResolverPluginConfiguration | ||
) => FederationRuntimePlugin = (config) => ({ | ||
name: 'repack-resolver-plugin', | ||
afterResolve(args) { | ||
const { ScriptManager } = | ||
require('../ScriptManager') as typeof RepackClient; | ||
const { remoteInfo } = args; | ||
|
||
ScriptManager.shared.addResolver( | ||
async (scriptId, caller, referenceUrl) => { | ||
// entry container | ||
if (scriptId === remoteInfo.entryGlobalName) { | ||
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]; | ||
const url = publicPath + '/' + bundlePath; | ||
|
||
const locator = await createScriptLocator(url, config); | ||
return locator; | ||
} | ||
}, | ||
{ key: remoteInfo.entryGlobalName } | ||
); | ||
|
||
return args; | ||
}, | ||
}); | ||
|
||
export default RepackResolverPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.