|
1 | | -import type { FederationRuntimePlugin } from '@module-federation/runtime/types'; |
| 1 | +import type { |
| 2 | + FederationRuntimePlugin, |
| 3 | + Shared, |
| 4 | +} from '@module-federation/runtime/types'; |
2 | 5 | import { loadScript } from '@module-federation/sdk'; |
3 | 6 |
|
4 | 7 | import { isObject, getUnpkgUrl } from '../index'; |
@@ -29,53 +32,54 @@ const fastRefreshPlugin = (): FederationRuntimePlugin => { |
29 | 32 | orderResolve = resolve; |
30 | 33 | }); |
31 | 34 | Object.keys(shareInfo).forEach(async (share) => { |
32 | | - let get: () => any; |
33 | | - if (share === 'react') { |
34 | | - get = () => |
35 | | - loadScript( |
36 | | - getUnpkgUrl(share, shareInfo[share].version) as string, |
37 | | - { |
38 | | - attrs: { defer: true, async: false }, |
39 | | - }, |
40 | | - ).then(() => { |
41 | | - orderResolve(); |
42 | | - }); |
43 | | - } |
44 | | - if (share === 'react-dom') { |
45 | | - get = () => |
46 | | - orderPromise.then(() => |
47 | | - loadScript( |
48 | | - getUnpkgUrl(share, shareInfo[share].version) as string, |
49 | | - { |
50 | | - attrs: { defer: true, async: false }, |
51 | | - }, |
52 | | - ), |
53 | | - ); |
54 | | - } |
55 | | - // @ts-expect-error |
56 | | - if (enableFastRefresh && typeof get === 'function') { |
| 35 | + // @ts-expect-error legacy runtime shareInfo[share] is shared , and latest i shard[] |
| 36 | + const sharedArr: Shared[] = Array.isArray(shareInfo[share]) |
| 37 | + ? shareInfo[share] |
| 38 | + : [shareInfo[share]]; |
| 39 | + |
| 40 | + sharedArr.forEach((shared) => { |
| 41 | + let get: () => any; |
57 | 42 | if (share === 'react') { |
58 | | - shareInfo[share].get = async () => { |
59 | | - if (!window.React) { |
60 | | - await get(); |
61 | | - console.warn( |
62 | | - '[Module Federation HMR]: You are using Module Federation Devtools to debug online host, it will cause your project load Dev mode React and ReactDOM. If not in this mode, please disable it in Module Federation Devtools', |
63 | | - ); |
64 | | - } |
65 | | - shareInfo[share].lib = () => window.React; |
66 | | - return () => window.React; |
67 | | - }; |
| 43 | + get = () => |
| 44 | + loadScript(getUnpkgUrl(share, shared.version) as string, { |
| 45 | + attrs: { defer: true, async: false }, |
| 46 | + }).then(() => { |
| 47 | + orderResolve(); |
| 48 | + }); |
68 | 49 | } |
69 | 50 | if (share === 'react-dom') { |
70 | | - shareInfo[share].get = async () => { |
71 | | - if (!window.ReactDOM) { |
72 | | - await get(); |
73 | | - } |
74 | | - shareInfo[share].lib = () => window.ReactDOM; |
75 | | - return () => window.ReactDOM; |
76 | | - }; |
| 51 | + get = () => |
| 52 | + orderPromise.then(() => |
| 53 | + loadScript(getUnpkgUrl(share, shared.version) as string, { |
| 54 | + attrs: { defer: true, async: false }, |
| 55 | + }), |
| 56 | + ); |
| 57 | + } |
| 58 | + // @ts-expect-error |
| 59 | + if (enableFastRefresh && typeof get === 'function') { |
| 60 | + if (share === 'react') { |
| 61 | + shared.get = async () => { |
| 62 | + if (!window.React) { |
| 63 | + await get(); |
| 64 | + console.warn( |
| 65 | + '[Module Federation HMR]: You are using Module Federation Devtools to debug online host, it will cause your project load Dev mode React and ReactDOM. If not in this mode, please disable it in Module Federation Devtools', |
| 66 | + ); |
| 67 | + } |
| 68 | + shared.lib = () => window.React; |
| 69 | + return () => window.React; |
| 70 | + }; |
| 71 | + } |
| 72 | + if (share === 'react-dom') { |
| 73 | + shared.get = async () => { |
| 74 | + if (!window.ReactDOM) { |
| 75 | + await get(); |
| 76 | + } |
| 77 | + shared.lib = () => window.ReactDOM; |
| 78 | + return () => window.ReactDOM; |
| 79 | + }; |
| 80 | + } |
77 | 81 | } |
78 | | - } |
| 82 | + }); |
79 | 83 | }); |
80 | 84 |
|
81 | 85 | return { |
|
0 commit comments