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

fix: issue with multiple init calls in remote #167

Merged
merged 1 commit into from
Oct 29, 2024
Merged
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
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
Loading