Skip to content

Commit 9c2f8c8

Browse files
authored
feat: host init inject option parameter (#343)
1 parent 245223f commit 9c2f8c8

File tree

6 files changed

+18
-3
lines changed

6 files changed

+18
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ export default defineConfig({
109109
},
110110
filename: "remoteEntry.js",
111111
shared: ["vue"],
112+
// Optional parameter that controls where the host initialization script is injected.
113+
// By default, it is injected into the index.html file.
114+
// You can set this to "entry" to inject it into the entry script instead.
115+
// Useful if your application does not load from index.html.
116+
hostInitInjectLocation: "html", // or "entry"
112117
}),
113118
],
114119
server: {
@@ -124,6 +129,7 @@ export default defineConfig({
124129
```
125130

126131
The host app configuration specifies its name, the filename of its exposed remote entry remoteEntry.js, and importantly, the configuration of the remote application to load.
132+
You can specify the place the host initialization file is injected with the **hostInitInjectLocation** option, which is described in the example code above.
127133

128134
## Load the Remote App
129135

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { VIRTUAL_EXPOSES } from './virtualModules/virtualExposes';
2525

2626
function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
2727
const options = normalizeModuleFederationOptions(mfUserOptions);
28-
const { name, remotes, shared, filename } = options;
28+
const { name, remotes, shared, filename, hostInitInjectLocation } = options;
2929
if (!name) throw new Error('name is required');
3030

3131
return [
@@ -50,7 +50,7 @@ function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
5050
...addEntry({
5151
entryName: 'hostInit',
5252
entryPath: getHostAutoInitPath(),
53-
inject: 'html',
53+
inject: hostInitInjectLocation,
5454
}),
5555
...addEntry({
5656
entryName: 'virtualExposes',

src/plugins/pluginAddEntry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import * as path from 'pathe';
33
import { Plugin } from 'vite';
44
import { mapCodeToCodeWithSourcemap } from '../utils/mapCodeToCodeWithSourcemap';
55

6+
import { NormalizedModuleFederationOptions } from '../utils/normalizeModuleFederationOptions';
7+
68
interface AddEntryOptions {
79
entryName: string;
810
entryPath: string;
911
fileName?: string;
10-
inject?: 'entry' | 'html';
12+
inject?: NormalizedModuleFederationOptions['hostInitInjectLocation'];
1113
}
1214

1315
function getFirstHtmlEntryFile(entryFiles: string[]): string | undefined {

src/utils/__tests__/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function getDefaultMockOptions(
1717
manifest: false,
1818
shareStrategy: 'loaded-first',
1919
virtualModuleDir: '__mf__virtual',
20+
hostInitInjectLocation: 'html',
2021
...overrides,
2122
};
2223
}

src/utils/__tests__/normalizeModuleFederationOption.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('normalizeModuleFederationOption', () => {
2727
shareStrategy: 'loaded-first',
2828
ignoreOrigin: false,
2929
virtualModuleDir: '__mf__virtual',
30+
hostInitInjectLocation: 'html',
3031
});
3132
});
3233

src/utils/normalizeModuleFederationOptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ export type ModuleFederationOptions = {
306306
shareStrategy?: ShareStrategy;
307307
ignoreOrigin?: boolean;
308308
virtualModuleDir?: string;
309+
hostInitInjectLocation?: HostInitInjectLocationOptions;
309310
};
310311

311312
export interface NormalizedModuleFederationOptions {
@@ -328,8 +329,11 @@ export interface NormalizedModuleFederationOptions {
328329
publicPath?: string;
329330
ignoreOrigin?: boolean;
330331
virtualModuleDir: string;
332+
hostInitInjectLocation: HostInitInjectLocationOptions;
331333
}
332334

335+
type HostInitInjectLocationOptions = 'entry' | 'html';
336+
333337
interface PluginDevOptions {
334338
disableLiveReload?: boolean;
335339
disableHotTypesReload?: boolean;
@@ -416,5 +420,6 @@ export function normalizeModuleFederationOptions(
416420
shareStrategy: options.shareStrategy || 'version-first',
417421
ignoreOrigin: options.ignoreOrigin || false,
418422
virtualModuleDir: options.virtualModuleDir || '__mf__virtual',
423+
hostInitInjectLocation: options.hostInitInjectLocation || 'html',
419424
});
420425
}

0 commit comments

Comments
 (0)