Skip to content

Commit 3082718

Browse files
authored
Fix/relative path (#2610)
1 parent 349c381 commit 3082718

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

.changeset/forty-dolls-sell.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@module-federation/native-federation-typescript': patch
3+
'@module-federation/native-federation-tests': patch
4+
---
5+
6+
ability to use relative paths

packages/native-federation-tests/src/configurations/hostPlugin.test.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('hostPlugin', () => {
77
name: 'moduleFederationHost',
88
filename: 'remoteEntry.js',
99
remotes: {
10-
moduleFederationTypescript: 'http://localhost:3000/remoteEntry.js',
10+
moduleFederationTests: 'http://localhost:3000/remoteEntry.js',
1111
},
1212
shared: {
1313
react: { singleton: true, eager: true },
@@ -37,7 +37,7 @@ describe('hostPlugin', () => {
3737
});
3838

3939
expect(mapRemotesToDownload).toStrictEqual({
40-
moduleFederationTypescript: 'http://localhost:3000/@mf-tests.zip',
40+
moduleFederationTests: 'http://localhost:3000/@mf-tests.zip',
4141
});
4242
});
4343

@@ -56,7 +56,7 @@ describe('hostPlugin', () => {
5656
expect(hostOptions).toStrictEqual(options);
5757

5858
expect(mapRemotesToDownload).toStrictEqual({
59-
moduleFederationTypescript: 'http://localhost:3000/custom-tests.zip',
59+
moduleFederationTests: 'http://localhost:3000/custom-tests.zip',
6060
});
6161
});
6262
});
@@ -65,7 +65,7 @@ describe('hostPlugin', () => {
6565
const subpathModuleFederationConfig = {
6666
...moduleFederationConfig,
6767
remotes: {
68-
moduleFederationTypescript:
68+
moduleFederationTests:
6969
'http://localhost:3000/subpatha/subpathb/remoteEntry.js',
7070
},
7171
};
@@ -75,9 +75,26 @@ describe('hostPlugin', () => {
7575
});
7676

7777
expect(mapRemotesToDownload).toStrictEqual({
78-
moduleFederationTypescript:
78+
moduleFederationTests:
7979
'http://localhost:3000/subpatha/subpathb/@mf-tests.zip',
8080
});
8181
});
82+
83+
it('correctly resolve remotes with relative reference in place of absolute url', () => {
84+
const subpathModuleFederationConfig = {
85+
...moduleFederationConfig,
86+
remotes: {
87+
moduleFederationTests: '/subpatha/remoteEntry.js',
88+
},
89+
};
90+
91+
const { mapRemotesToDownload } = retrieveHostConfig({
92+
moduleFederationConfig: subpathModuleFederationConfig,
93+
});
94+
95+
expect(mapRemotesToDownload).toStrictEqual({
96+
moduleFederationTests: '/subpatha/@mf-tests.zip',
97+
});
98+
});
8299
});
83100
});

packages/native-federation-tests/src/configurations/hostPlugin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ const retrieveRemoteStringUrl = (remote: string) => {
1212
return splittedRemote[splittedRemote.length - 1];
1313
};
1414

15+
const FILE_PROTOCOL = 'file:';
16+
1517
const buildZipUrl = (hostOptions: Required<HostOptions>, remote: string) => {
1618
const remoteStringUrl = retrieveRemoteStringUrl(remote);
17-
const remoteUrl = new URL(remoteStringUrl);
19+
const remoteUrl = new URL(remoteStringUrl, FILE_PROTOCOL);
1820

1921
const pathnameWithoutEntry = remoteUrl.pathname
2022
.split('/')
2123
.slice(0, -1)
2224
.join('/');
2325
remoteUrl.pathname = `${pathnameWithoutEntry}/${hostOptions.testsFolder}.zip`;
2426

25-
return remoteUrl.href;
27+
return remoteUrl.protocol === FILE_PROTOCOL
28+
? remoteUrl.pathname
29+
: remoteUrl.href;
2630
};
2731

2832
const resolveRemotes = (hostOptions: Required<HostOptions>) => {

packages/native-federation-typescript/src/configurations/hostPlugin.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,22 @@ describe('hostPlugin', () => {
7777
'http://localhost:3000/subpatha/subpathb/@mf-types.zip',
7878
});
7979
});
80+
81+
it('correctly resolve remotes with relative reference in place of absolute url', () => {
82+
const subpathModuleFederationConfig = {
83+
...moduleFederationConfig,
84+
remotes: {
85+
moduleFederationTypescript: '/subpatha/remoteEntry.js',
86+
},
87+
};
88+
89+
const { mapRemotesToDownload } = retrieveHostConfig({
90+
moduleFederationConfig: subpathModuleFederationConfig,
91+
});
92+
93+
expect(mapRemotesToDownload).toStrictEqual({
94+
moduleFederationTypescript: '/subpatha/@mf-types.zip',
95+
});
96+
});
8097
});
8198
});

packages/native-federation-typescript/src/configurations/hostPlugin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ const retrieveRemoteStringUrl = (remote: string) => {
1111
return splittedRemote[splittedRemote.length - 1];
1212
};
1313

14+
const FILE_PROTOCOL = 'file:';
15+
1416
const buildZipUrl = (hostOptions: Required<HostOptions>, remote: string) => {
1517
const remoteStringUrl = retrieveRemoteStringUrl(remote);
16-
const remoteUrl = new URL(remoteStringUrl);
18+
const remoteUrl = new URL(remoteStringUrl, FILE_PROTOCOL);
1719

1820
const pathnameWithoutEntry = remoteUrl.pathname
1921
.split('/')
2022
.slice(0, -1)
2123
.join('/');
2224
remoteUrl.pathname = `${pathnameWithoutEntry}/${hostOptions.typesFolder}.zip`;
2325

24-
return remoteUrl.href;
26+
return remoteUrl.protocol === FILE_PROTOCOL
27+
? remoteUrl.pathname
28+
: remoteUrl.href;
2529
};
2630

2731
const resolveRemotes = (hostOptions: Required<HostOptions>) => {

0 commit comments

Comments
 (0)