Skip to content
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

fix(native-federation-typescript): enable remote string resolution from object #3517

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,78 +21,97 @@ describe('hostPlugin', () => {
const invokeRetrieve = () => retrieveHostConfig({});
expect(invokeRetrieve).toThrowError('moduleFederationConfig is required');
});
});

describe('correctly intersect with default options', () => {
it('only moduleFederationConfig provided', () => {
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig({
moduleFederationConfig,
});

expect(hostOptions).toStrictEqual({
moduleFederationConfig,
typesFolder: '@mf-types',
deleteTypesFolder: true,
maxRetries: 3,
});

expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTypescript: 'http://localhost:3000/@mf-types.zip',
});
describe('correctly intersect with default options', () => {
it('only moduleFederationConfig provided', () => {
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig({
moduleFederationConfig,
});

it('all options provided', () => {
const options = {
moduleFederationConfig,
typesFolder: 'custom-types',
deleteTypesFolder: false,
maxRetries: 1,
};

const { hostOptions, mapRemotesToDownload } =
retrieveHostConfig(options);

expect(hostOptions).toStrictEqual(options);
expect(hostOptions).toStrictEqual({
moduleFederationConfig,
typesFolder: '@mf-types',
deleteTypesFolder: true,
maxRetries: 3,
});

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

it('correctly resolve subpath remotes', () => {
const subpathModuleFederationConfig = {
...moduleFederationConfig,
remotes: {
moduleFederationTypescript:
'http://localhost:3000/subpatha/subpathb/remoteEntry.js',
},
it('all options provided', () => {
const options = {
moduleFederationConfig,
typesFolder: 'custom-types',
deleteTypesFolder: false,
maxRetries: 1,
};

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

expect(hostOptions).toStrictEqual(options);

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

it('correctly resolve subpath remotes', () => {
const subpathModuleFederationConfig = {
...moduleFederationConfig,
remotes: {
moduleFederationTypescript:
'http://localhost:3000/subpatha/subpathb/remoteEntry.js',
},
};

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

it('correctly resolve remotes with relative reference in place of absolute url', () => {
const subpathModuleFederationConfig = {
...moduleFederationConfig,
remotes: {
moduleFederationTypescript: '/subpatha/remoteEntry.js',
expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTypescript:
'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',
});
});

it('correctly resolve remotes with object entry', () => {
const subpathModuleFederationConfig = {
...moduleFederationConfig,
remotes: {
moduleFederationTypescript: {
entry: 'http://localhost:3000/subpatha/subpathb/remoteEntry.js',
},
};
},
};

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

expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTypescript: '/subpatha/@mf-types.zip',
});
expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTypescript:
'http://localhost:3000/subpatha/subpathb/@mf-types.zip',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ const defaultOptions = {
maxRetries: 3,
} satisfies Partial<HostOptions>;

const retrieveRemoteStringUrl = (remote: string) => {
const splittedRemote = remote.split('@');
return splittedRemote[splittedRemote.length - 1];
const retrieveRemoteStringUrl = (remote: string | Record<string, string>) => {
if (typeof remote === 'string') {
const splittedRemote = remote.split('@');
return splittedRemote[splittedRemote.length - 1];
}

if (typeof remote === 'object' && 'entry' in remote) {
const splittedRemote = remote.entry.split('@');
return splittedRemote[splittedRemote.length - 1];
}

throw new Error(`Invalid type of remote: ${typeof remote}`);
};

const FILE_PROTOCOL = 'file:';
Expand Down