Skip to content

Commit

Permalink
fix: Ensure @module-federation/runtime singleton (#31)
Browse files Browse the repository at this point in the history
* fix: Ensure @module-federation/runtime singleton

* fix: build preview

---------

Co-authored-by: 张洪恩 <[email protected]>
  • Loading branch information
zhangHongEn and 张洪恩 authored Aug 3, 2024
1 parent 34a2a7e commit 694a27d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export default defineConfig({
## roadmap

- fix: vitePluginAddEntry.ts This plugin may need to support the case where base in vite.config.js is not configured as "/"
- fix: Ensure @module-federation/runtime is a singleton, split it into a separate chunk
- fix: remoteEntry and hostInit file names support hash generation
- feat: generate mf-manifest.json
- feat: support chrome plugin
Expand Down
1 change: 1 addition & 0 deletions examples/vite/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineConfig({
server: {
open: true,
},
base: 'http://localhost:5173',
plugins: [
react(),
federation({
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
return [
aliasToArrayPlugin,
normalizeOptimizeDepsPlugin,
normalizeBuildPlugin(shared),
normalizeBuildPlugin([...Object.keys(shared), "@module-federation/runtime"]),
addEntry({
entryName: 'remoteEntry',
entryPath: emptyPath + '?__mf__wrapRemoteEntry__',
Expand Down Expand Up @@ -272,3 +272,4 @@ function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
}

export { federation };
export default federation

This comment has been minimized.

Copy link
@husayt

husayt Aug 6, 2024

Contributor

this is not really needed and a bad practice in general to have both names and default exports of the same module

This comment has been minimized.

Copy link
@gioboa

gioboa Aug 6, 2024

Collaborator

Would you like to create a PR for that?

This comment has been minimized.

Copy link
@husayt

husayt Aug 6, 2024

Contributor

Created PR as suggested. Thank you

12 changes: 4 additions & 8 deletions src/utils/normalizeBuild.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import { UserConfig } from 'vite';

interface Shared {
[key: string]: any;
}

interface Output {
manualChunks?: {
[key: string]: any;
};
}

export default (shared: Shared) => ({
export default (singleChunkModules: string[]) => ({
name: 'normalizeBuild',
config: (config: UserConfig, { command }: { command: string }) => {
if (!config.build) config.build = {};
if (!config.build.rollupOptions) config.build.rollupOptions = {};
let { rollupOptions } = config.build;
if (!rollupOptions.output) rollupOptions.output = {};
normalizeManualChunks(rollupOptions.output as any, shared);
normalizeManualChunks(rollupOptions.output as any, singleChunkModules);
},
});

function normalizeManualChunks(output: Output, shared: Shared = {}): void {
const pattern = new RegExp(`node_modules/(${Object.keys(shared).join('|')})/`);
function normalizeManualChunks(output: Output, singleChunkModules: string[]): void {
const pattern = new RegExp(`node_modules/(${singleChunkModules.join('|')})/`);
if (!output.manualChunks) output.manualChunks = {};
const wrapManualChunks =
(original: any) =>
Expand Down
30 changes: 15 additions & 15 deletions src/utils/normalizeModuleFederationOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function normalizeShared(
string,
| string
| {
name: string;
name?: string;
version?: string;
shareScope?: string;
singleton?: boolean;
Expand All @@ -186,7 +186,7 @@ function normalizeShared(
}
if (typeof shared === 'object') {
Object.keys(shared).forEach((key) => {
result[key] = normalizeShareItem(key, shared[key]);
result[key] = normalizeShareItem(key, shared[key] as any);
});
}

Expand All @@ -199,21 +199,21 @@ function normalizeLibrary(library: any): any {
}

export type ModuleFederationOptions = {
exposes: Record<string, string | { import: string }> | undefined;
exposes?: Record<string, string | { import: string }> | undefined;
filename?: string;
library: any;
library?: any;
name: string;
remoteType: string;
remotes:
// remoteType?: string;
remotes?:
| Record<
string,
| string
| { type: string; name: string; entry: string; entryGlobalName: string; shareScope: string }
>
| undefined;
runtime: any;
runtime?: any;
shareScope?: string;
shared:
shared?:
| string[]
| Record<
string,
Expand All @@ -229,19 +229,19 @@ export type ModuleFederationOptions = {
>
| undefined;
runtimePlugins?: string[];
getPublicPath: any;
implementation: any;
manifest: any;
dev: any;
dts: any;
getPublicPath?: any;
implementation?: any;
manifest?: any;
dev?: any;
dts?: any;
};

export interface NormalizedModuleFederationOptions {
exposes: Record<string, ExposesItem>;
filename: string;
library: any;
name: string;
remoteType: string;
// remoteType: string;
remotes: Record<
string,
{ type: string; name: string; entry: string; entryGlobalName: string; shareScope: string }
Expand All @@ -265,7 +265,7 @@ export function normalizeModuleFederationOptions(
filename: options.filename || 'remoteEntry.js',
library: normalizeLibrary(options.library),
name: options.name,
remoteType: options.remoteType,
// remoteType: options.remoteType,
remotes: normalizeRemotes(options.remotes),
runtime: options.runtime,
shareScope: options.shareScope || 'default',
Expand Down

0 comments on commit 694a27d

Please sign in to comment.