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

feat(dts-plugin,sdk,website-new): added option to use a base path when resolving types from relative remote urls #3042

Open
wants to merge 2 commits 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
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 @@ -148,6 +148,7 @@ interface DtsHostOptions {
deleteTypesFolder?: boolean;
maxRetries?: number;
consumeAPITypes?: boolean;
remoteBasePath?: string;
sbvice marked this conversation as resolved.
Show resolved Hide resolved
}
```

Expand Down Expand Up @@ -209,6 +210,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
10 changes: 10 additions & 0 deletions apps/website-new/docs/zh/configure/dts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ interface DtsHostOptions {
deleteTypesFolder?: boolean;
maxRetries?: number;
consumeAPITypes?: boolean;
remoteBasePath?: string;
}
```

Expand Down Expand Up @@ -209,6 +210,15 @@ interface DtsHostOptions {

对应 `remotes` 目录配置的 `typesFolder`

#### remoteBasePath

- 类型:`string`
- 是否必填:否
- 默认值:`''`

在定位类型时,用作相对远程 URL 的默认基础路径


### tsConfigPath

- 类型:`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 @@ -144,6 +144,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