|
| 1 | +import { init } from '@module-federation/enhanced/runtime'; |
| 2 | +import { FederationHost, UserOptions } from '@module-federation/runtime-core'; |
| 3 | + |
| 4 | +export type ManifestFile<T extends RemoteConfig = RemoteConfig> = { |
| 5 | + [key: string]: string | T; |
| 6 | +}; |
| 7 | + |
| 8 | +export type Manifest<T extends RemoteConfig = RemoteConfig> = { |
| 9 | + [key: string]: T; |
| 10 | +}; |
| 11 | + |
| 12 | +// |
| 13 | +// remoteEntry is the original used by the orignal |
| 14 | +// webpack-based plugin; entry is used by the new |
| 15 | +// Module Federation Runtime. We support both to |
| 16 | +// avoid confusion. |
| 17 | +// |
| 18 | +export type RemoteConfig = |
| 19 | + | { |
| 20 | + name: string; |
| 21 | + type: 'module' | 'script'; |
| 22 | + remoteEntry: string; |
| 23 | + [key: string]: unknown; |
| 24 | + } |
| 25 | + | { |
| 26 | + name: string; |
| 27 | + type: 'module' | 'script'; |
| 28 | + entry: string; |
| 29 | + [key: string]: unknown; |
| 30 | + }; |
| 31 | + |
| 32 | +export type InitFederationOptions = { |
| 33 | + runtimeOptions?: UserOptions; |
| 34 | +}; |
| 35 | + |
| 36 | +let config: Manifest = {}; |
| 37 | + |
| 38 | +export async function initFederation( |
| 39 | + manifest: string | ManifestFile, |
| 40 | + options?: InitFederationOptions |
| 41 | +): Promise<FederationHost> { |
| 42 | + if (typeof manifest === 'string') { |
| 43 | + config = await loadManifest(manifest); |
| 44 | + } else { |
| 45 | + config = parseConfig(manifest); |
| 46 | + } |
| 47 | + |
| 48 | + const runtimeConfig = toRuntimeConfig(config, options); |
| 49 | + return init(runtimeConfig); |
| 50 | +} |
| 51 | + |
| 52 | +// TODO: Consider making internal |
| 53 | +export function toRuntimeConfig( |
| 54 | + config: Manifest<RemoteConfig>, |
| 55 | + options?: InitFederationOptions |
| 56 | +): UserOptions { |
| 57 | + return { |
| 58 | + name: 'shell', |
| 59 | + ...options?.runtimeOptions, |
| 60 | + remotes: [ |
| 61 | + ...(options?.runtimeOptions?.remotes ?? []), |
| 62 | + ...toRemotes(config), |
| 63 | + ], |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +function toRemotes(config: Manifest) { |
| 68 | + return Object.values(config).map((c) => ({ |
| 69 | + name: c.name, |
| 70 | + entry: (c.entry ?? c.remoteEntry ?? '') as string, |
| 71 | + type: c.type, |
| 72 | + })); |
| 73 | +} |
| 74 | + |
| 75 | +export function getManifest<T extends Manifest>(): T { |
| 76 | + return config as T; |
| 77 | +} |
| 78 | + |
| 79 | +// Just needed to align with original webpack-based plugin |
| 80 | +export async function setManifest(manifest: ManifestFile) { |
| 81 | + config = parseConfig(manifest); |
| 82 | +} |
| 83 | + |
| 84 | +export async function loadManifest<T extends Manifest = Manifest>( |
| 85 | + configFile: string |
| 86 | +): Promise<T> { |
| 87 | + const result = await fetch(configFile); |
| 88 | + |
| 89 | + if (!result.ok) { |
| 90 | + throw Error('could not load configFile: ' + configFile); |
| 91 | + } |
| 92 | + |
| 93 | + config = parseConfig(await result.json()); |
| 94 | + return config as T; |
| 95 | +} |
| 96 | + |
| 97 | +// TODO: Consider making internal |
| 98 | +export function parseConfig(config: ManifestFile): Manifest { |
| 99 | + const result: Manifest = {}; |
| 100 | + for (const key in config) { |
| 101 | + const value = config[key]; |
| 102 | + |
| 103 | + let entry: RemoteConfig; |
| 104 | + if (typeof value === 'string') { |
| 105 | + entry = { |
| 106 | + name: key, |
| 107 | + remoteEntry: value, |
| 108 | + type: 'module', |
| 109 | + }; |
| 110 | + } else { |
| 111 | + entry = { |
| 112 | + ...value, |
| 113 | + name: key, |
| 114 | + type: value.type || 'module', |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + result[key] = entry; |
| 119 | + } |
| 120 | + return result; |
| 121 | +} |
0 commit comments