-
Notifications
You must be signed in to change notification settings - Fork 87
chore: warn if shared dependency is resolved to somewhere (#354) #358
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
Merged
gioboa
merged 3 commits into
module-federation:main
from
CaptainWang98:feature_shared_conflict_warning
Nov 19, 2025
+287
−0
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
src/plugins/__tests__/pluginCheckAliasConflicts.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { checkAliasConflicts } from '../pluginCheckAliasConflicts'; | ||
|
|
||
| describe('pluginCheckAliasConflicts', () => { | ||
| let consoleWarnSpy: any; | ||
| let mockLogger: any; | ||
|
|
||
| beforeEach(() => { | ||
| consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| mockLogger = { | ||
| warn: vi.fn(), | ||
| info: vi.fn(), | ||
| error: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| consoleWarnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('should warn when alias conflicts with shared module', () => { | ||
| const plugin = checkAliasConflicts({ | ||
| shared: { | ||
| vue: { | ||
| name: 'vue', | ||
| version: '3.2.45', | ||
| scope: 'default', | ||
| from: 'host', | ||
| shareConfig: { | ||
| requiredVersion: '^3.2.45', | ||
| } as any, | ||
| }, | ||
| pinia: { | ||
| name: 'pinia', | ||
| version: '2.0.28', | ||
| scope: 'default', | ||
| from: 'host', | ||
| shareConfig: { | ||
| requiredVersion: '^2.0.28', | ||
| } as any, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const mockConfig = { | ||
| logger: mockLogger, | ||
| resolve: { | ||
| alias: [ | ||
| { | ||
| find: 'vue', | ||
| replacement: '/path/to/project/node_modules/vue/dist/vue.runtime.esm-bundler.js', | ||
| }, | ||
| { | ||
| find: 'pinia', | ||
| replacement: '/path/to/project/node_modules/pinia/dist/pinia.mjs', | ||
| }, | ||
| { | ||
| find: 'shared', | ||
| replacement: '/path/to/project/shared/shared', | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| plugin.configResolved!(mockConfig as any); | ||
|
|
||
| expect(mockLogger.warn).toHaveBeenCalledTimes(5); | ||
|
Check failure on line 67 in src/plugins/__tests__/pluginCheckAliasConflicts.test.ts
|
||
| expect(mockLogger.warn).toHaveBeenCalledWith( | ||
| '\n[Module Federation] Detected alias conflicts with shared modules:' | ||
| ); | ||
| expect(mockLogger.warn).toHaveBeenCalledWith( | ||
| expect.stringContaining('Shared module "vue" is aliased by "vue"') | ||
| ); | ||
| expect(mockLogger.warn).toHaveBeenCalledWith( | ||
| expect.stringContaining('Shared module "pinia" is aliased by "pinia"') | ||
| ); | ||
| }); | ||
|
|
||
| it('should not warn when no alias conflicts exist', () => { | ||
| const plugin = checkAliasConflicts({ | ||
| shared: { | ||
| vue: { | ||
| name: 'vue', | ||
| version: '3.2.45', | ||
| scope: 'default', | ||
| from: 'host', | ||
| shareConfig: { | ||
| requiredVersion: '^3.2.45', | ||
| } as any, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const mockConfig = { | ||
| logger: mockLogger, | ||
| resolve: { | ||
| alias: [ | ||
| { | ||
| find: 'shared', | ||
| replacement: '/path/to/project/shared/shared', | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| plugin.configResolved!(mockConfig as any); | ||
|
|
||
| expect(mockLogger.warn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not warn when shared is empty', () => { | ||
| const plugin = checkAliasConflicts({ | ||
| shared: {}, | ||
| }); | ||
|
|
||
| const mockConfig = { | ||
| logger: mockLogger, | ||
| resolve: { | ||
| alias: [ | ||
| { | ||
| find: 'vue', | ||
| replacement: '/path/to/project/node_modules/vue/dist/vue.runtime.esm-bundler.js', | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| plugin.configResolved!(mockConfig as any); | ||
|
|
||
| expect(mockLogger.warn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should handle regex alias patterns', () => { | ||
| const plugin = checkAliasConflicts({ | ||
| shared: { | ||
| react: { | ||
| name: 'react', | ||
| version: '18.0.0', | ||
| scope: 'default', | ||
| from: 'host', | ||
| shareConfig: { | ||
| requiredVersion: '^18.0.0', | ||
| } as any, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const mockConfig = { | ||
| logger: mockLogger, | ||
| resolve: { | ||
| alias: [ | ||
| { | ||
| find: /^react$/, | ||
| replacement: '/path/to/project/node_modules/react/index.js', | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| plugin.configResolved!(mockConfig as any); | ||
|
|
||
| expect(mockLogger.warn).toHaveBeenCalled(); | ||
| expect(mockLogger.warn).toHaveBeenCalledWith( | ||
| expect.stringContaining('Shared module "react" is aliased') | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle shared modules with trailing slash', () => { | ||
| const plugin = checkAliasConflicts({ | ||
| shared: { | ||
| 'lodash/': { | ||
| name: 'lodash/', | ||
| version: '4.17.21', | ||
| scope: 'default', | ||
| from: 'host', | ||
| shareConfig: { | ||
| requiredVersion: '^4.17.21', | ||
| } as any, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const mockConfig = { | ||
| logger: mockLogger, | ||
| resolve: { | ||
| alias: [ | ||
| { | ||
| find: 'lodash', | ||
| replacement: '/path/to/project/node_modules/lodash', | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| plugin.configResolved!(mockConfig as any); | ||
|
|
||
| expect(mockLogger.warn).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import type { Alias, Plugin } from 'vite'; | ||
| import { NormalizedShared } from '../utils/normalizeModuleFederationOptions'; | ||
|
|
||
| /** | ||
| * Check if user-defined alias conflicts with shared modules | ||
| * This should run after aliasToArrayPlugin to ensure alias is an array | ||
| */ | ||
| export function checkAliasConflicts(options: { shared?: NormalizedShared }): Plugin { | ||
| const { shared = {} } = options; | ||
| const sharedKeys = Object.keys(shared); | ||
|
|
||
| return { | ||
| name: 'check-alias-conflicts', | ||
| configResolved(config: any) { | ||
| if (sharedKeys.length === 0) return; | ||
|
|
||
| const userAliases: Alias[] = config.resolve?.alias || []; | ||
| const conflicts: Array<{ sharedModule: string; alias: string; target: string }> = []; | ||
|
|
||
| for (const sharedKey of sharedKeys) { | ||
| for (const aliasEntry of userAliases) { | ||
| const findPattern = aliasEntry.find; | ||
| const replacement = aliasEntry.replacement; | ||
|
|
||
| // Skip if replacement is not a string (e.g., customResolver) | ||
| if (typeof replacement !== 'string') continue; | ||
|
|
||
| // Check if alias pattern matches the shared module | ||
| let isMatch = false; | ||
| if (typeof findPattern === 'string') { | ||
| isMatch = findPattern === sharedKey || sharedKey.startsWith(findPattern + '/'); | ||
| } else if (findPattern instanceof RegExp) { | ||
| isMatch = findPattern.test(sharedKey); | ||
| } | ||
|
|
||
| if (isMatch) { | ||
| conflicts.push({ | ||
| sharedModule: sharedKey, | ||
| alias: String(findPattern), | ||
| target: replacement, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (conflicts.length > 0) { | ||
| config.logger.warn('\n[Module Federation] Detected alias conflicts with shared modules:'); | ||
| conflicts.forEach(({ sharedModule, alias, target }) => { | ||
| config.logger.warn( | ||
| ` - Shared module "${sharedModule}" is aliased by "${alias}" to "${target}"` | ||
| ); | ||
| }); | ||
| config.logger.warn( | ||
| " This may cause runtime errors as the shared module will bypass Module Federation's sharing mechanism." | ||
| ); | ||
| } | ||
| }, | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.