-
-
Notifications
You must be signed in to change notification settings - Fork 271
feat: show replaceable dependencies #1468
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
Merged
Changes from all commits
Commits
Show all changes
2 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
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,63 @@ | ||
| import type { ModuleReplacement } from 'module-replacements' | ||
|
|
||
| async function fetchReplacements( | ||
| deps: Record<string, string>, | ||
| ): Promise<Record<string, ModuleReplacement>> { | ||
| const names = Object.keys(deps) | ||
|
|
||
| const results = await Promise.all( | ||
| names.map(async name => { | ||
| try { | ||
| const replacement = await $fetch<ModuleReplacement | null>(`/api/replacements/${name}`) | ||
| return { name, replacement } | ||
| } catch { | ||
| return { name, replacement: null } | ||
| } | ||
| }), | ||
| ) | ||
|
|
||
| const map: Record<string, ModuleReplacement> = {} | ||
| for (const { name, replacement } of results) { | ||
| if (replacement) { | ||
| map[name] = replacement | ||
| } | ||
| } | ||
| return map | ||
| } | ||
|
|
||
| /** | ||
| * Fetch module replacement suggestions for a set of dependencies. | ||
| * Returns a reactive map of dependency name to ModuleReplacement. | ||
| */ | ||
| export function useReplacementDependencies( | ||
| dependencies: MaybeRefOrGetter<Record<string, string> | undefined>, | ||
| ) { | ||
| const replacements = shallowRef<Record<string, ModuleReplacement>>({}) | ||
| let generation = 0 | ||
|
|
||
| if (import.meta.client) { | ||
| watch( | ||
| () => toValue(dependencies), | ||
| async deps => { | ||
| const currentGeneration = ++generation | ||
|
|
||
| if (!deps || Object.keys(deps).length === 0) { | ||
| replacements.value = {} | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| const result = await fetchReplacements(deps) | ||
| if (currentGeneration === generation) { | ||
| replacements.value = result | ||
| } | ||
| } catch { | ||
| // catastrophic failure, just keep whatever we have | ||
| } | ||
| }, | ||
| { immediate: true }, | ||
| ) | ||
| } | ||
|
|
||
| return replacements | ||
| } | ||
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
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
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
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
113 changes: 113 additions & 0 deletions
113
test/nuxt/composables/use-replacement-dependencies.spec.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,113 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime' | ||
| import type { ModuleReplacement } from 'module-replacements' | ||
|
|
||
| const SIMPLE_REPLACEMENT: ModuleReplacement = { | ||
| type: 'simple', | ||
| moduleName: 'is-even', | ||
| replacement: 'Use (n % 2) === 0', | ||
| category: 'micro-utilities', | ||
| } | ||
|
|
||
| const NATIVE_REPLACEMENT: ModuleReplacement = { | ||
| type: 'native', | ||
| moduleName: 'array-includes', | ||
| nodeVersion: '6.0.0', | ||
| replacement: 'Array.prototype.includes', | ||
| mdnPath: 'Global_Objects/Array/includes', | ||
| category: 'native', | ||
| } | ||
|
|
||
| async function mountWithDeps(deps: Record<string, string> | undefined) { | ||
| const captured = ref<Record<string, ModuleReplacement>>({}) | ||
|
|
||
| const WrapperComponent = defineComponent({ | ||
| setup() { | ||
| const replacements = useReplacementDependencies(() => deps) | ||
|
|
||
| watchEffect(() => { | ||
| captured.value = { ...replacements.value } | ||
| }) | ||
|
|
||
| return () => h('div') | ||
| }, | ||
| }) | ||
|
|
||
| await mountSuspended(WrapperComponent) | ||
|
|
||
| return captured | ||
| } | ||
|
|
||
| describe('useReplacementDependencies', () => { | ||
| it('returns replacements for dependencies that have them', async () => { | ||
| registerEndpoint('/api/replacements/is-even', () => SIMPLE_REPLACEMENT) | ||
| registerEndpoint('/api/replacements/picoquery', () => null) | ||
|
|
||
| const replacements = await mountWithDeps({ | ||
| 'is-even': '^1.0.0', | ||
| 'picoquery': '^1.0.0', | ||
| }) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(replacements.value['is-even']).toBeDefined() | ||
| }) | ||
|
|
||
| expect(replacements.value['is-even']?.type).toBe('simple') | ||
| expect(replacements.value['picoquery']).toBeUndefined() | ||
| }) | ||
|
|
||
| it('returns empty object for undefined dependencies', async () => { | ||
| const replacements = await mountWithDeps(undefined) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(replacements.value).toEqual({}) | ||
| }) | ||
| }) | ||
|
|
||
| it('returns empty object for empty dependencies', async () => { | ||
| const replacements = await mountWithDeps({}) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(replacements.value).toEqual({}) | ||
| }) | ||
| }) | ||
|
|
||
| it('handles multiple dependencies with replacements', async () => { | ||
| registerEndpoint('/api/replacements/is-even', () => SIMPLE_REPLACEMENT) | ||
| registerEndpoint('/api/replacements/array-includes', () => NATIVE_REPLACEMENT) | ||
| registerEndpoint('/api/replacements/picoquery', () => null) | ||
|
|
||
| const replacements = await mountWithDeps({ | ||
| 'is-even': '^1.0.0', | ||
| 'array-includes': '^3.0.0', | ||
| 'picoquery': '^1.0.0', | ||
| }) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(Object.keys(replacements.value)).toHaveLength(2) | ||
| }) | ||
|
|
||
| expect(replacements.value['is-even']?.type).toBe('simple') | ||
| expect(replacements.value['array-includes']?.type).toBe('native') | ||
| expect(replacements.value['picoquery']).toBeUndefined() | ||
| }) | ||
|
|
||
| it('handles fetch errors gracefully', async () => { | ||
| registerEndpoint('/api/replacements/failing-package', () => { | ||
| throw new Error('Network error') | ||
| }) | ||
| registerEndpoint('/api/replacements/is-even', () => SIMPLE_REPLACEMENT) | ||
|
|
||
| const replacements = await mountWithDeps({ | ||
| 'failing-package': '^1.0.0', | ||
| 'is-even': '^1.0.0', | ||
| }) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(replacements.value['is-even']).toBeDefined() | ||
| }) | ||
|
|
||
| expect(replacements.value['failing-package']).toBeUndefined() | ||
| expect(replacements.value['is-even']?.type).toBe('simple') | ||
| }) | ||
| }) |
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.