Skip to content

Fix/relative path #2610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/forty-dolls-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@module-federation/native-federation-typescript': patch
'@module-federation/native-federation-tests': patch
---

ability to use relative paths
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('hostPlugin', () => {
name: 'moduleFederationHost',
filename: 'remoteEntry.js',
remotes: {
moduleFederationTypescript: 'http://localhost:3000/remoteEntry.js',
moduleFederationTests: 'http://localhost:3000/remoteEntry.js',
},
shared: {
react: { singleton: true, eager: true },
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('hostPlugin', () => {
});

expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTypescript: 'http://localhost:3000/@mf-tests.zip',
moduleFederationTests: 'http://localhost:3000/@mf-tests.zip',
});
});

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

expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTypescript: 'http://localhost:3000/custom-tests.zip',
moduleFederationTests: 'http://localhost:3000/custom-tests.zip',
});
});
});
Expand All @@ -65,7 +65,7 @@ describe('hostPlugin', () => {
const subpathModuleFederationConfig = {
...moduleFederationConfig,
remotes: {
moduleFederationTypescript:
moduleFederationTests:
'http://localhost:3000/subpatha/subpathb/remoteEntry.js',
},
};
Expand All @@ -75,9 +75,26 @@ describe('hostPlugin', () => {
});

expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTypescript:
moduleFederationTests:
'http://localhost:3000/subpatha/subpathb/@mf-tests.zip',
});
});

it('correctly resolve remotes with relative reference in place of absolute url', () => {
const subpathModuleFederationConfig = {
...moduleFederationConfig,
remotes: {
moduleFederationTests: '/subpatha/remoteEntry.js',
},
};

const { mapRemotesToDownload } = retrieveHostConfig({
moduleFederationConfig: subpathModuleFederationConfig,
});

expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTests: '/subpatha/@mf-tests.zip',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ const retrieveRemoteStringUrl = (remote: string) => {
return splittedRemote[splittedRemote.length - 1];
};

const FILE_PROTOCOL = 'file:';

const buildZipUrl = (hostOptions: Required<HostOptions>, remote: string) => {
const remoteStringUrl = retrieveRemoteStringUrl(remote);
const remoteUrl = new URL(remoteStringUrl);
const remoteUrl = new URL(remoteStringUrl, FILE_PROTOCOL);

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

return remoteUrl.href;
return remoteUrl.protocol === FILE_PROTOCOL
? remoteUrl.pathname
: remoteUrl.href;
};

const resolveRemotes = (hostOptions: Required<HostOptions>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,22 @@ describe('hostPlugin', () => {
'http://localhost:3000/subpatha/subpathb/@mf-types.zip',
});
});

it('correctly resolve remotes with relative reference in place of absolute url', () => {
const subpathModuleFederationConfig = {
...moduleFederationConfig,
remotes: {
moduleFederationTypescript: '/subpatha/remoteEntry.js',
},
};

const { mapRemotesToDownload } = retrieveHostConfig({
moduleFederationConfig: subpathModuleFederationConfig,
});

expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTypescript: '/subpatha/@mf-types.zip',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ const retrieveRemoteStringUrl = (remote: string) => {
return splittedRemote[splittedRemote.length - 1];
};

const FILE_PROTOCOL = 'file:';

const buildZipUrl = (hostOptions: Required<HostOptions>, remote: string) => {
const remoteStringUrl = retrieveRemoteStringUrl(remote);
const remoteUrl = new URL(remoteStringUrl);
const remoteUrl = new URL(remoteStringUrl, FILE_PROTOCOL);

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

return remoteUrl.href;
return remoteUrl.protocol === FILE_PROTOCOL
? remoteUrl.pathname
: remoteUrl.href;
};

const resolveRemotes = (hostOptions: Required<HostOptions>) => {
Expand Down
Loading