Skip to content

Commit

Permalink
format: Support empty entry JS in build mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangHongEn committed Sep 29, 2024
1 parent a73d9cb commit 816752e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/plugins/pluginModuleParseEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let _resolve: any,
_reject = reject;
});
let parsePromise = promise;
let exposesParseEnd = false
let exposesParseEnd = false;

const parseStartSet = new Set();
const parseEndSet = new Set();
Expand Down Expand Up @@ -48,7 +48,7 @@ export default function (excludeFn: Function): Plugin[] {
}
if (id === VIRTUAL_EXPOSES) {
// When the entry JS file is empty and only contains exposes export code, it’s necessary to wait for the exposes modules to be resolved in order to collect the dependencies being used.
exposesParseEnd = true
exposesParseEnd = true;
}
parseEndSet.add(id);
if (exposesParseEnd && parseStartSet.size === parseEndSet.size) {
Expand Down
13 changes: 9 additions & 4 deletions src/plugins/pluginProxyRemoteEntry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { createFilter } from '@rollup/pluginutils';
import { Plugin } from 'vite';
import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFederationOptions';
import { generateExposes, generateRemoteEntry, REMOTE_ENTRY_ID, VIRTUAL_EXPOSES } from '../virtualModules';
import {
generateExposes,
generateRemoteEntry,
REMOTE_ENTRY_ID,
VIRTUAL_EXPOSES,
} from '../virtualModules';
import { parsePromise } from './pluginModuleParseEnd';

const filter: (id: string) => boolean = createFilter();
Expand All @@ -15,15 +20,15 @@ export default function (): Plugin {
return REMOTE_ENTRY_ID;
}
if (id === VIRTUAL_EXPOSES) {
return VIRTUAL_EXPOSES
return VIRTUAL_EXPOSES;
}
},
load(id: string) {
if (id === REMOTE_ENTRY_ID) {
return parsePromise.then((_) => generateRemoteEntry(getNormalizeModuleFederationOptions()));
}
if (id === VIRTUAL_EXPOSES) {
return generateExposes()
return generateExposes();
}
},
async transform(code: string, id: string) {
Expand All @@ -32,7 +37,7 @@ export default function (): Plugin {
return parsePromise.then((_) => generateRemoteEntry(getNormalizeModuleFederationOptions()));
}
if (id === VIRTUAL_EXPOSES) {
return generateExposes()
return generateExposes();
}
},
};
Expand Down
8 changes: 4 additions & 4 deletions src/virtualModules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export {
getLocalSharedImportMapPath,
getUsedShares,
REMOTE_ENTRY_ID,
writeLocalSharedImportMap
writeLocalSharedImportMap,
} from './virtualRemoteEntry';

export {
addUsedRemote,
generateRemotes,
getRemoteVirtualModule,
getUsedRemotesMap
getUsedRemotesMap,
} from './virtualRemotes';

export {
Expand All @@ -26,10 +26,10 @@ export {
LOAD_SHARE_TAG,
PREBUILD_TAG,
writeLoadShareModule,
writePreBuildLibPath
writePreBuildLibPath,
} from './virtualShared_preBuild';

export { generateExposes, VIRTUAL_EXPOSES } from "./virtualExposes";
export { generateExposes, VIRTUAL_EXPOSES } from './virtualExposes';

export { virtualRuntimeInitStatus } from './virtualRuntimeInitStatus';

Expand Down
4 changes: 2 additions & 2 deletions src/virtualModules/virtualExposes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getNormalizeModuleFederationOptions } from "../utils/normalizeModuleFederationOptions";
import { getNormalizeModuleFederationOptions } from '../utils/normalizeModuleFederationOptions';

export const VIRTUAL_EXPOSES = 'virtual:mf-exposes';
export function generateExposes() {
const options = getNormalizeModuleFederationOptions()
const options = getNormalizeModuleFederationOptions();
return `
export default {
${Object.keys(options.exposes)
Expand Down
28 changes: 14 additions & 14 deletions src/virtualModules/virtualRemoteEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ export function generateLocalSharedImportMap() {
return `
const importMap = {
${Array.from(getUsedShares())
.map(
(pkg) => `
.map(
(pkg) => `
${JSON.stringify(pkg)}: async () => {
let pkg = await import("${getPreBuildLibImportId(pkg)}")
return pkg
}
`
)
.join(',')}
)
.join(',')}
}
const usedShared = {
${Array.from(getUsedShares())
.map((key) => {
const shareItem = getNormalizeShareItem(key);
return `
.map((key) => {
const shareItem = getNormalizeShareItem(key);
return `
${JSON.stringify(key)}: {
name: ${JSON.stringify(key)},
version: ${JSON.stringify(shareItem.version)},
Expand All @@ -81,22 +81,22 @@ export function generateLocalSharedImportMap() {
}
}
`;
})
.join(',')}
})
.join(',')}
}
const usedRemotes = [${Object.keys(getUsedRemotesMap())
.map((key) => {
const remote = options.remotes[key];
return `
.map((key) => {
const remote = options.remotes[key];
return `
{
entryGlobalName: ${JSON.stringify(remote.entryGlobalName)},
name: ${JSON.stringify(remote.name)},
type: ${JSON.stringify(remote.type)},
entry: ${JSON.stringify(remote.entry)},
}
`;
})
.join(',')}
})
.join(',')}
]
export {
usedShared,
Expand Down

0 comments on commit 816752e

Please sign in to comment.