Skip to content

Commit

Permalink
feat(dts-plugin,sdk,website-new): added option to use a base path whe…
Browse files Browse the repository at this point in the history
…n resolving types from relative remote urls
  • Loading branch information
sbvice authored and Owen Hancock committed Oct 3, 2024
1 parent 13975e1 commit 4d44ce2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/bright-brooms-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@module-federation/dts-plugin': minor
'@module-federation/sdk': minor
'@module-federation/website-new': minor
---

Adding `remoteBasePath` to `DtsHostOptions` enabling types to be resolved from a relative remote path.
9 changes: 9 additions & 0 deletions apps/website-new/docs/en/configure/dts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ interface DtsHostOptions {
deleteTypesFolder?: boolean;
maxRetries?: number;
consumeAPITypes?: boolean;
remoteBasePath?: string;
}
```

Expand Down Expand Up @@ -208,6 +209,14 @@ Before loading type files, whether to delete the previously loaded `typesFolder`

`typesFolder` corresponding to `remotes` directory configuration

#### remoteBasePath

- Type: `string`
- Required: No
- Default value: `''`

Used as the default base path for relative remote URLs when locating types

### tsConfigPath

- Type: `string`
Expand Down
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 4d44ce2

Please sign in to comment.