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: support remotes renaming #468

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 4 additions & 2 deletions packages/lib/src/dev/remote-development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ const wrapShareScope = ${REMOTE_FROM_PARAMETER} => {
}
}
const initMap = Object.create(null);
async function __federation_method_ensure(remoteId) {
const remote = remotesMap[remoteId];
async function __federation_method_ensure(remoteName) {
const remote = remotesMap[remoteName];
const remoteId = remote?.id

if (!remote.inited) {
if ('var' === remote.format) {
// loading js with script tag
Expand Down
6 changes: 4 additions & 2 deletions packages/lib/src/prod/remote-production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ export function prodRemotePlugin(

const initMap = Object.create(null);

async function __federation_method_ensure(remoteId) {
const remote = remotesMap[remoteId];
async function __federation_method_ensure(remoteName) {
const remote = remotesMap[remoteName];
const remoteId = remote?.id

if (!remote.inited) {
if ('var' === remote.format) {
// loading js with script tag
Expand Down
30 changes: 17 additions & 13 deletions packages/lib/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,21 @@ export function parseRemoteOptions(
): (string | ConfigTypeSet)[] {
return parseOptions(
options.remotes ? options.remotes : {},
(item) => ({
(item, key) => ({
external: Array.isArray(item) ? item : [item],
shareScope: options.shareScope || 'default',
format: 'esm',
from: 'vite',
externalType: 'url'
externalType: 'url',
externalName: key
}),
(item) => ({
(item, key) => ({
external: Array.isArray(item.external) ? item.external : [item.external],
shareScope: item.shareScope || options.shareScope || 'default',
format: item.format || 'esm',
from: item.from ?? 'vite',
externalType: item.externalType || 'url'
externalType: item.externalType || 'url',
externalName: item.externalName ?? key
})
)
}
Expand Down Expand Up @@ -222,16 +224,18 @@ export function createRemotesMap(remotes: Remote[]): string {
return `'${external}'`
}
}

const createRemote = (remote: Remote) =>
`'${remote.id}': {
url: ${createUrl(remote)},
format: '${remote.config.format}',
from: '${remote.config.from}',
id: '${remote.config.externalName}',
}`

return `const remotesMap = {
${remotes
.map(
(remote) =>
`'${remote.id}':{url:${createUrl(remote)},format:'${
remote.config.format
}',from:'${remote.config.from}'}`
)
.join(',\n ')}
};`
${remotes.map(createRemote).join(',\n ')}
};`
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ declare interface RemotesConfig {
*/
external: string

/**
* Name by which the remote is exposed.
*/
externalName?: string

/**
* The format of the specified external
*/
Expand Down