Skip to content

Commit 5ad9b5a

Browse files
authored
fix: improve error handling for missing shared modules (#336)
- Add `assertModuleFound` utility to provide clearer error messages when shared modules are not found - Replace direct `VirtualModule.findModule` calls with `assertModuleFound` in proxy shared module plugin - Include unit test for new error handling behavior
1 parent b24e5e7 commit 5ad9b5a

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

src/plugins/pluginProxySharedModule_preBuild.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Plugin, UserConfig } from 'vite';
22
import { NormalizedShared } from '../utils/normalizeModuleFederationOptions';
33
import { PromiseStore } from '../utils/PromiseStore';
4-
import VirtualModule from '../utils/VirtualModule';
54
import {
65
addUsedShares,
76
generateLocalSharedImportMap,
@@ -13,6 +12,8 @@ import {
1312
writePreBuildLibPath,
1413
} from '../virtualModules';
1514
import { parsePromise } from './pluginModuleParseEnd';
15+
import VirtualModule, { assertModuleFound } from '../utils/VirtualModule';
16+
1617
export function proxySharedModule(options: {
1718
shared?: NormalizedShared;
1819
include?: string | string[];
@@ -71,18 +72,17 @@ export function proxySharedModule(options: {
7172
? {
7273
find: new RegExp(`(.*${PREBUILD_TAG}.*)`),
7374
replacement: function ($1: string) {
74-
const pkgName = (VirtualModule.findModule(PREBUILD_TAG, $1) as VirtualModule)
75-
.name;
75+
const module = assertModuleFound(PREBUILD_TAG, $1) as VirtualModule;
76+
const pkgName = module.name;
7677
return pkgName;
7778
},
7879
}
7980
: {
8081
find: new RegExp(`(.*${PREBUILD_TAG}.*)`),
8182
replacement: '$1',
8283
async customResolver(source: string, importer: string) {
83-
const pkgName = (
84-
VirtualModule.findModule(PREBUILD_TAG, source) as VirtualModule
85-
).name;
84+
const module = assertModuleFound(PREBUILD_TAG, source) as VirtualModule;
85+
const pkgName = module.name;
8686
const result = await (this as any)
8787
.resolve(pkgName, importer)
8888
.then((item: any) => item.id);

src/utils/VirtualModule.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ const cacheMap: {
5454
/**
5555
* Physically generate files as virtual modules under node_modules/__mf__virtual/*
5656
*/
57+
export function assertModuleFound(tag: string, str: string = ''): VirtualModule {
58+
const module = VirtualModule.findModule(tag, str);
59+
if (!module) {
60+
throw new Error(
61+
`Module Federation shared module '${str}' not found. Please ensure it's installed as a dependency in your package.json.`
62+
);
63+
}
64+
return module;
65+
}
66+
5767
export default class VirtualModule {
5868
name: string;
5969
tag: string;

src/utils/__tests__/VirtualModule.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getSuffix } from '../VirtualModule';
1+
import { getSuffix, assertModuleFound } from '../VirtualModule';
22

33
describe('getSuffix', () => {
44
it('returns .js for simple package without extension', () => {
@@ -37,3 +37,16 @@ describe('getSuffix', () => {
3737
expect(getSuffix('@scope.with.dots/pkg/deep/util.spec.ts')).toBe('.ts');
3838
});
3939
});
40+
41+
describe('assertModuleFound', () => {
42+
it('throws an error when module is not found', () => {
43+
const tag = '__test_tag__';
44+
const str = 'non-existent-module';
45+
46+
expect(() => {
47+
assertModuleFound(tag, str);
48+
}).toThrow(
49+
`Module Federation shared module '${str}' not found. Please ensure it's installed as a dependency in your package.json.`
50+
);
51+
});
52+
});

src/virtualModules/virtualShared_preBuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function getLoadShareModulePath(pkg: string): string {
3838
}
3939
export function writeLoadShareModule(pkg: string, shareItem: ShareItem, command: string) {
4040
loadShareCacheMap[pkg].writeSync(`
41-
41+
4242
;() => import(${JSON.stringify(getPreBuildLibImportId(pkg))}).catch(() => {});
4343
// dev uses dynamic import to separate chunks
4444
${command !== 'build' ? `;() => import(${JSON.stringify(pkg)}).catch(() => {});` : ''}

0 commit comments

Comments
 (0)