|
| 1 | +import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime'; |
| 2 | +import type * as RepackClient from '../ScriptManager/index.js'; |
| 3 | + |
| 4 | +interface PrefetchAsset { |
| 5 | + name: string; |
| 6 | + remoteName: string; |
| 7 | + url: string; |
| 8 | +} |
| 9 | + |
| 10 | +function getAssetName(asset: string): string { |
| 11 | + // remove the extension from the asset name |
| 12 | + return asset.split('.')[0]; |
| 13 | +} |
| 14 | + |
| 15 | +function getAssetUrl(asset: string) { |
| 16 | + // create placeholder reference url for the asset |
| 17 | + return 'prefetch:///' + asset; |
| 18 | +} |
| 19 | + |
| 20 | +function prefetchAsset(asset: PrefetchAsset) { |
| 21 | + const client = require('../ScriptManager/index.js') as typeof RepackClient; |
| 22 | + const { ScriptManager, getWebpackContext } = client; |
| 23 | + |
| 24 | + // caller should be undefined when fetching/loading the remote entry container |
| 25 | + const caller = asset.name === asset.remoteName ? undefined : asset.remoteName; |
| 26 | + |
| 27 | + return ScriptManager.shared.prefetchScript( |
| 28 | + asset.name, |
| 29 | + caller, |
| 30 | + getWebpackContext(), |
| 31 | + asset.url |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +const RepackPrefetchPlugin: () => FederationRuntimePlugin = () => ({ |
| 36 | + name: 'repack-prefetch-plugin', |
| 37 | + generatePreloadAssets: async (args) => { |
| 38 | + const preloadConfig = args.preloadOptions.preloadConfig; |
| 39 | + const remoteName = preloadConfig.nameOrAlias; |
| 40 | + const remoteSnapshot = args.remoteSnapshot; |
| 41 | + |
| 42 | + if (preloadConfig.depsRemote !== false) { |
| 43 | + console.warn( |
| 44 | + '[RepackPrefetchPlugin] ' + |
| 45 | + 'The depsRemote configuration option is not implemented yet. ' + |
| 46 | + 'This setting will be ignored and will have no effect. ' + |
| 47 | + 'You can hide this warning by setting depsRemote explicitly to false.' |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + function handleAssets(assets: string[]): PrefetchAsset[] { |
| 52 | + return assets.map((asset) => ({ |
| 53 | + name: getAssetName(asset), |
| 54 | + remoteName, |
| 55 | + url: getAssetUrl(asset), |
| 56 | + })); |
| 57 | + } |
| 58 | + |
| 59 | + let assets: PrefetchAsset[] = []; |
| 60 | + |
| 61 | + if ('modules' in remoteSnapshot) { |
| 62 | + for (const exposedModule of remoteSnapshot.modules) { |
| 63 | + if (preloadConfig.exposes) { |
| 64 | + if (!preloadConfig.exposes.includes(exposedModule.moduleName)) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (preloadConfig.resourceCategory === 'all') { |
| 70 | + assets.push(...handleAssets(exposedModule.assets.js.async)); |
| 71 | + assets.push(...handleAssets(exposedModule.assets.js.sync)); |
| 72 | + } else if (preloadConfig.resourceCategory === 'sync') { |
| 73 | + assets.push(...handleAssets(exposedModule.assets.js.sync)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (preloadConfig.filter) { |
| 78 | + assets = assets.filter((asset) => preloadConfig.filter!(asset.name)); |
| 79 | + } |
| 80 | + |
| 81 | + assets.unshift({ |
| 82 | + name: remoteSnapshot.globalName, |
| 83 | + remoteName: remoteSnapshot.globalName, |
| 84 | + url: getAssetUrl(remoteSnapshot.remoteEntry), |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + await Promise.all(assets.map(prefetchAsset)); |
| 89 | + |
| 90 | + // noop for compatibility |
| 91 | + return Promise.resolve({ |
| 92 | + cssAssets: [], |
| 93 | + jsAssetsWithoutEntry: [], |
| 94 | + entryAssets: [], |
| 95 | + }); |
| 96 | + }, |
| 97 | +}); |
| 98 | + |
| 99 | +export default RepackPrefetchPlugin; |
0 commit comments