Skip to content

Commit a960c88

Browse files
authored
fix(runtime): add define constant to help downgrade non-esm project (#3261)
1 parent e26d107 commit a960c88

File tree

7 files changed

+49
-10
lines changed

7 files changed

+49
-10
lines changed

.changeset/dirty-bikes-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/runtime': patch
3+
---
4+
5+
fix(runtime): add define constant to help downgrade non-esm project

.changeset/tasty-guests-shop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/modern-js': patch
3+
---
4+
5+
fix(modern-js-plugin): only export esm mfRuntimePlugin

packages/modernjs/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@
3535
"import": "./dist/esm/cli/ssrPlugin.js",
3636
"require": "./dist/cjs/cli/ssrPlugin.js",
3737
"types": "./dist/types/cli/ssrPlugin.d.ts"
38+
},
39+
"./shared-strategy": {
40+
"import": "./dist/esm/cli/mfRuntimePlugins/shared-strategy.js",
41+
"require": "./dist/esm/cli/mfRuntimePlugins/shared-strategy.js",
42+
"types": "./dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts"
43+
},
44+
"./resolve-entry-ipv4": {
45+
"import": "./dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js",
46+
"require": "./dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js",
47+
"types": "./dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts"
48+
},
49+
"./inject-node-fetch": {
50+
"import": "./dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js",
51+
"require": "./dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js",
52+
"types": "./dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts"
3853
}
3954
},
4055
"typesVersions": {
@@ -50,6 +65,15 @@
5065
],
5166
"ssr-plugin": [
5267
"./dist/types/cli/ssrPlugin.d.ts"
68+
],
69+
"shared-strategy": [
70+
"./dist/types/cli/mfRuntimePlugins/shared-strategy.d.ts"
71+
],
72+
"resolve-entry-ipv4": [
73+
"./dist/types/cli/mfRuntimePlugins/resolve-entry-ipv4.d.ts"
74+
],
75+
"inject-node-fetch": [
76+
"./dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts"
5377
]
5478
}
5579
},

packages/modernjs/src/cli/utils.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ describe('patchMFConfig', async () => {
3939
},
4040
remoteType: 'script',
4141
runtimePlugins: [
42-
path.resolve(__dirname, './mfRuntimePlugins/shared-strategy.js'),
42+
require.resolve('@module-federation/modern-js/shared-strategy'),
4343
require.resolve('@module-federation/node/runtimePlugin'),
44-
path.resolve(__dirname, './mfRuntimePlugins/inject-node-fetch.js'),
44+
require.resolve('@module-federation/modern-js/inject-node-fetch'),
4545
],
4646
shared: {
4747
react: {
@@ -69,7 +69,7 @@ describe('patchMFConfig', async () => {
6969
},
7070
remoteType: 'script',
7171
runtimePlugins: [
72-
path.resolve(__dirname, './mfRuntimePlugins/shared-strategy.js'),
72+
require.resolve('@module-federation/modern-js/shared-strategy'),
7373
],
7474
shared: {
7575
react: {

packages/modernjs/src/cli/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ export const patchMFConfig = (
146146
patchDTSConfig(mfConfig, isServer);
147147

148148
injectRuntimePlugins(
149-
path.resolve(__dirname, './mfRuntimePlugins/shared-strategy.js'),
149+
require.resolve('@module-federation/modern-js/shared-strategy'),
150150
runtimePlugins,
151151
);
152152

153153
if (isDev) {
154154
injectRuntimePlugins(
155-
path.resolve(__dirname, './mfRuntimePlugins/resolve-entry-ipv4.js'),
155+
require.resolve('@module-federation/modern-js/resolve-entry-ipv4'),
156156
runtimePlugins,
157157
);
158158
}
@@ -172,7 +172,7 @@ export const patchMFConfig = (
172172
}
173173

174174
injectRuntimePlugins(
175-
path.resolve(__dirname, './mfRuntimePlugins/inject-node-fetch.js'),
175+
require.resolve('@module-federation/modern-js/inject-node-fetch'),
176176
runtimePlugins,
177177
);
178178

packages/runtime/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ declare const __VERSION__: string;
22
declare const FEDERATION_DEBUG: string;
33
declare const FEDERATION_BUILD_IDENTIFIER: string | undefined;
44
declare const __RELEASE_NUMBER__: number;
5+
declare const FEDERATION_ALLOW_NEW_FUNCTION: string | undefined;

packages/runtime/src/utils/load.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ async function loadEsmEntry({
2525
return new Promise<RemoteEntryExports>((resolve, reject) => {
2626
try {
2727
if (!remoteEntryExports) {
28-
import(/* webpackIgnore: true */ /* @vite-ignore */ entry)
29-
.then(resolve)
30-
.catch(reject);
28+
if (typeof FEDERATION_ALLOW_NEW_FUNCTION !== 'undefined') {
29+
new Function(
30+
'callbacks',
31+
`import("${entry}").then(callbacks[0]).catch(callbacks[1])`,
32+
)([resolve, reject]);
33+
} else {
34+
import(/* webpackIgnore: true */ /* @vite-ignore */ entry).then(resolve).catch(reject);
35+
}
3136
} else {
3237
resolve(remoteEntryExports);
3338
}
@@ -223,7 +228,6 @@ export async function getRemoteEntry({
223228

224229
if (!globalLoading[uniqueKey]) {
225230
const loadEntryHook = origin.remoteHandler.hooks.lifecycle.loadEntry;
226-
const createScriptHook = origin.loaderHook.lifecycle.createScript;
227231
const loaderHook = origin.loaderHook;
228232

229233
globalLoading[uniqueKey] = loadEntryHook

0 commit comments

Comments
 (0)