Skip to content

Commit

Permalink
fix: issue with multiple init calls in remote (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangHongEn authored Oct 29, 2024
1 parent b7d638c commit bbd0e5d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
...addEntry({
entryName: 'hostInit',
entryPath: getHostAutoInitPath(),
inject: 'html',
}),
...addEntry({
entryName: 'virtualExposes',
Expand Down
34 changes: 31 additions & 3 deletions src/plugins/pluginAddEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ interface AddEntryOptions {
entryName: string;
entryPath: string;
fileName?: string;
inject?: 'entry' | 'html';
}

const addEntry = ({ entryName, entryPath, fileName }: AddEntryOptions): Plugin[] => {
const addEntry = ({
entryName,
entryPath,
fileName,
inject = 'entry',
}: AddEntryOptions): Plugin[] => {
const devEntryPath = entryPath.startsWith('virtual:mf') ? '/@id/' + entryPath : entryPath;
let entryFiles: string[] = [];
let htmlFilePath: string;
let _command: string;
let emitFileId: string;
let viteConfig: any;

return [
{
Expand Down Expand Up @@ -50,6 +58,7 @@ const addEntry = ({ entryName, entryPath, fileName }: AddEntryOptions): Plugin[]
name: 'add-entry',
enforce: 'post',
configResolved(config) {
viteConfig = config;
const inputOptions = config.build.rollupOptions.input;

if (!inputOptions) {
Expand Down Expand Up @@ -77,7 +86,7 @@ const addEntry = ({ entryName, entryPath, fileName }: AddEntryOptions): Plugin[]
if (!hasHash) {
emitFileOptions.fileName = fileName;
}
this.emitFile(emitFileOptions);
emitFileId = this.emitFile(emitFileOptions);
if (htmlFilePath) {
const htmlContent = fs.readFileSync(htmlFilePath, 'utf-8');
const scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
Expand All @@ -88,8 +97,27 @@ const addEntry = ({ entryName, entryPath, fileName }: AddEntryOptions): Plugin[]
}
}
},
generateBundle(options, bundle) {
if (inject !== 'html') return;
const file = this.getFileName(emitFileId);
const scriptContent = `
<script type="module" src="${viteConfig.base + file}"></script>
`;

for (const fileName in bundle) {
if (fileName.endsWith('.html')) {
let htmlAsset = bundle[fileName];
if (htmlAsset.type === 'chunk') return;
let htmlContent = htmlAsset.source.toString() || '';

htmlContent = htmlContent.replace('<head>', `<head>${scriptContent}`);

htmlAsset.source = htmlContent;
}
}
},
transform(code, id) {
if (entryFiles.some((file) => id.endsWith(file))) {
if (inject === 'entry' && entryFiles.some((file) => id.endsWith(file))) {
const injection = `
import ${JSON.stringify(entryPath)};
`;
Expand Down
3 changes: 2 additions & 1 deletion src/virtualModules/virtualRemoteEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ export function generateRemoteEntry(options: NormalizedModuleFederationOptions):
* Inject entry file, automatically init when used as host,
* and will not inject remoteEntry
*/
const hostAutoInitModule = new VirtualModule('hostAutoInit');
export const HOST_AUTO_INIT_TAG = '__H_A_I__';
const hostAutoInitModule = new VirtualModule('hostAutoInit', HOST_AUTO_INIT_TAG);
export function writeHostAutoInit() {
hostAutoInitModule.writeSync(`
import {init} from "${REMOTE_ENTRY_ID}"
Expand Down

0 comments on commit bbd0e5d

Please sign in to comment.