Skip to content

Commit 0dce151

Browse files
authored
feat(runtime): support entry with query (module-federation#1833)
1 parent de66cf8 commit 0dce151

File tree

8 files changed

+50
-9
lines changed

8 files changed

+50
-9
lines changed

.changeset/wet-dancers-raise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/runtime': patch
3+
---
4+
5+
chore(runtime): support entry with query

commitlint.config.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
module.exports = {
22
extends: ['@commitlint/config-conventional', '@commitlint/config-nx-scopes'],
3-
rules: {
4-
'scope-enum': [2, 'always', ['release', 'docs', 'workflow']],
5-
},
63
};

packages/runtime/__tests__/load-remote.spec.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ describe('loadRemote', () => {
267267
reset();
268268
});
269269

270+
it('remote entry url with query', async () => {
271+
const FederationInstance = new FederationHost({
272+
name: '@federation-test/compatible',
273+
remotes: [
274+
{
275+
name: '__FEDERATION_@federation-test/app2:custom__',
276+
alias: 'app2',
277+
entry:
278+
'http://localhost:1111/resources/app2/federation-remote-entry.js?kk=2',
279+
},
280+
],
281+
});
282+
const module =
283+
await FederationInstance.loadRemote<() => string>('app2/say');
284+
assert(module, 'module should be a function');
285+
expect(module()).toBe('hello app2');
286+
});
287+
270288
it('different instance with same module', async () => {
271289
const reset = addGlobalSnapshot({
272290
'@module-federation/load-remote-different-instance': {
@@ -399,6 +417,25 @@ describe('loadRemote with manifest.json', () => {
399417
Global.__FEDERATION__.__INSTANCES__ = [];
400418
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = undefined;
401419
});
420+
421+
it('manifest.json with query', async () => {
422+
const FM = new FederationHost({
423+
name: '@demo/host',
424+
remotes: [
425+
{
426+
name: '@demo/main',
427+
entry:
428+
'http://localhost:1111/resources/main/federation-manifest.json?query=2',
429+
},
430+
],
431+
});
432+
433+
const [module, ,] = await Promise.all([
434+
FM.loadRemote<Promise<() => string>>('@demo/main/say'),
435+
]);
436+
assert(module);
437+
expect(module()).toBe('hello world');
438+
});
402439
});
403440

404441
describe('lazy loadRemote add remote into snapshot', () => {
@@ -560,7 +597,7 @@ describe('loadRemote', () => {
560597
const loadedSrcs = [...document.querySelectorAll('script')].map(
561598
(i) => i.fakeSrc,
562599
);
563-
expect(loadedSrcs.includes(`${remotePublicPath}/${jsSyncAssetPath}`));
600+
expect(loadedSrcs.includes(`${remotePublicPath}${jsSyncAssetPath}`));
564601
reset();
565602
});
566603
});

packages/runtime/__tests__/mock/handlers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const handlers = [
3131
);
3232

3333
const content = fs.readFileSync(filepath, 'utf-8');
34-
if (typeof file === 'string' && file.endsWith('json')) {
34+
if (typeof file === 'string' && file.includes('json')) {
3535
return res(ctx.status(200), ctx.json(JSON.parse(content)));
3636
} else {
3737
return res(ctx.status(200), ctx.text(content));

packages/runtime/__tests__/mock/mock-script.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ function injector(current: Function, methodName: string) {
6161
(nEl as any)['fakeSrc'] = (element as any)[key];
6262
}
6363
}
64-
const filePath = element.src.replace(matchInfo.baseUrl, '');
64+
const filePath = element.src
65+
.replace(matchInfo.baseUrl, '')
66+
.replace(/\?.*$/, '');
6567

6668
const execScriptContent = fs.readFileSync(
6769
path.resolve(matchInfo.baseDir, filePath),

packages/runtime/src/utils/tool.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function isRemoteInfoWithEntry(
2828
}
2929

3030
export function isPureRemoteEntry(remote: RemoteWithEntry): boolean {
31-
return remote.entry.endsWith('.js');
31+
return !remote.entry.includes('.json') && remote.entry.includes('.js');
3232
}
3333

3434
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages/sdk/src/generateSnapshotFromManifest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export function isManifestProvider(
167167
): moduleInfo is ManifestProvider {
168168
if (
169169
'remoteEntry' in moduleInfo &&
170-
moduleInfo.remoteEntry.endsWith(MANIFEST_EXT)
170+
moduleInfo.remoteEntry.includes(MANIFEST_EXT)
171171
) {
172172
return true;
173173
} else {

packages/sdk/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const parseEntry = (str: string, devVerOrUrl?: string): RemoteEntryInfo => {
1919
getProcessEnv()['NODE_ENV'] === 'development' && devVerOrUrl;
2020
const defaultVersion = '*';
2121
const isEntry = (s: string) =>
22-
s.startsWith('http') || s.endsWith(MANIFEST_EXT);
22+
s.startsWith('http') || s.includes(MANIFEST_EXT);
2323

2424
// Check if the string starts with a type
2525
if (strSplit.length >= 2) {

0 commit comments

Comments
 (0)