Skip to content

Commit

Permalink
feat(dts-plugin,sdk): added option to use a base path when resolving …
Browse files Browse the repository at this point in the history
…types from relative remote urls
  • Loading branch information
sbvice committed Sep 18, 2024
1 parent 13975e1 commit 9b00854
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/bright-brooms-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@module-federation/dts-plugin': minor
'@module-federation/sdk': minor
---

Adding `remoteBasePath` to `DtsHostOptions` enabling types to be resolved from a relative remote path.
51 changes: 51 additions & 0 deletions packages/dts-plugin/src/core/configurations/hostPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('hostPlugin', () => {
abortOnError: true,
consumeAPITypes: false,
runtimePkgs: [],
remoteBasePath: 'file:',
});

expect(mapRemotesToDownload).toStrictEqual({
Expand All @@ -66,6 +67,7 @@ describe('hostPlugin', () => {
abortOnError: true,
consumeAPITypes: false,
runtimePkgs: [],
remoteBasePath: 'file:',
};

const { hostOptions, mapRemotesToDownload } =
Expand Down Expand Up @@ -143,5 +145,54 @@ describe('hostPlugin', () => {
},
});
});

it('uses the provided remote base path for relative remote urls', () => {
const subpathModuleFederationConfig = {
...moduleFederationConfig,
remotes: {
moduleFederationTypescript: '/subpatha/mf-manifest.json',
},
};

const { mapRemotesToDownload } = retrieveHostConfig({
moduleFederationConfig: subpathModuleFederationConfig,
remoteBasePath: 'http://localhost:3000',
});

expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTypescript: {
alias: 'moduleFederationTypescript',
apiTypeUrl: 'http://localhost:3000/subpatha/@mf-types.d.ts',
name: '/subpatha/mf-manifest.json',
url: 'http://localhost:3000/subpatha/mf-manifest.json',
zipUrl: 'http://localhost:3000/subpatha/@mf-types.zip',
},
});
});

it('ignores the provided remote base path for absolute remote urls', () => {
const subpathModuleFederationConfig = {
...moduleFederationConfig,
remotes: {
moduleFederationTypescript:
'http://localhost:3333/subpatha/mf-manifest.json',
},
};

const { mapRemotesToDownload } = retrieveHostConfig({
moduleFederationConfig: subpathModuleFederationConfig,
remoteBasePath: 'http://localhost:3000',
});

expect(mapRemotesToDownload).toStrictEqual({
moduleFederationTypescript: {
alias: 'moduleFederationTypescript',
apiTypeUrl: 'http://localhost:3333/subpatha/@mf-types.d.ts',
name: 'http://localhost:3333/subpatha/mf-manifest.json',
url: 'http://localhost:3333/subpatha/mf-manifest.json',
zipUrl: 'http://localhost:3333/subpatha/@mf-types.zip',
},
});
});
});
});
35 changes: 25 additions & 10 deletions packages/dts-plugin/src/core/configurations/hostPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { utils } from '@module-federation/managers';
import { HostOptions, RemoteInfo } from '../interfaces/HostOptions';
import { validateOptions } from '../lib/utils';

const fileBase = 'file:';

const defaultOptions = {
typesFolder: '@mf-types',
remoteTypesFolder: '@mf-types',
Expand All @@ -18,18 +20,31 @@ const defaultOptions = {
abortOnError: true,
consumeAPITypes: false,
runtimePkgs: [],
remoteBasePath: fileBase,
} satisfies Partial<HostOptions>;

const buildZipUrl = (hostOptions: Required<HostOptions>, url: string) => {
const remoteUrl = new URL(url, 'file:');

const pathnameWithoutEntry = remoteUrl.pathname
.split('/')
.slice(0, -1)
.join('/');
remoteUrl.pathname = `${pathnameWithoutEntry}/${hostOptions.remoteTypesFolder}.zip`;
const resolveRelativeUrl = (
hostOptions: Required<HostOptions>,
entryUrl: string,
relativeFile?: string,
) => {
let remoteUrl = new URL(entryUrl, hostOptions.remoteBasePath);
if (relativeFile) {
const pathnameWithoutEntry = remoteUrl.pathname
.split('/')
.slice(0, -1)
.join('/');
remoteUrl.pathname = `${pathnameWithoutEntry}/${relativeFile}`;
}
return remoteUrl.protocol === fileBase ? remoteUrl.pathname : remoteUrl.href;
};

return remoteUrl.protocol === 'file:' ? remoteUrl.pathname : remoteUrl.href;
const buildZipUrl = (hostOptions: Required<HostOptions>, url: string) => {
return resolveRelativeUrl(
hostOptions,
url,
`${hostOptions.remoteTypesFolder}.zip`,
);
};

const buildApiTypeUrl = (zipUrl?: string) => {
Expand Down Expand Up @@ -63,7 +78,7 @@ export const retrieveRemoteInfo = (options: {

return {
name: parsedInfo.name || remoteAlias,
url: url,
url: resolveRelativeUrl(hostOptions, url),
zipUrl,
apiTypeUrl: buildApiTypeUrl(zipUrl),
alias: remoteAlias,
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/src/types/plugins/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export interface DtsHostOptions {
maxRetries?: number;
consumeAPITypes?: boolean;
runtimePkgs?: string[];
/**
* Used as the default base path for relative remote URLs when locating types
*/
remoteBasePath?: string;
}

export interface DtsRemoteOptions {
Expand Down

0 comments on commit 9b00854

Please sign in to comment.