fix(vite): fix issue detecting shadowed keyed composables (#21891)

This commit is contained in:
anhao 2023-07-05 17:35:45 +08:00 committed by GitHub
parent 28e91a7aad
commit e70ff83e72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 6 deletions

View File

@ -224,7 +224,7 @@ class ScopedVarsCollector {
const NUXT_IMPORT_RE = /nuxt|#app|#imports/
function detectImportNames (code: string, composableMeta: Record<string, { source?: string | RegExp }>) {
export function detectImportNames (code: string, composableMeta: Record<string, { source?: string | RegExp }>) {
const imports = findStaticImports(code)
const names = new Set<string>()
for (const i of imports) {
@ -235,7 +235,7 @@ function detectImportNames (code: string, composableMeta: Record<string, { sourc
if (source && matchWithStringOrRegex(i.specifier, source)) {
return
}
names.add(namedImports![name])
names.add(name)
}
const { namedImports, defaultImport, namespacedImport } = parseStaticImport(i)

View File

@ -0,0 +1,27 @@
import { describe, expect, it } from 'vitest'
import { detectImportNames } from '../src/plugins/composable-keys'
describe('detectImportNames', () => {
const keyedComposables = {
useFetch: { source: '#app', argumentLength: 2 },
useCustomFetch: { source: 'custom-fetch', argumentLength: 2 }
}
it('should not include imports from nuxt', () => {
expect([...detectImportNames('import { useFetch } from \'#app\'', {})]).toMatchInlineSnapshot('[]')
expect([...detectImportNames('import { useFetch } from \'nuxt/app\'', {})]).toMatchInlineSnapshot('[]')
})
it('should pick up other imports', () => {
expect([...detectImportNames('import { useCustomFetch, someThing as someThingRenamed } from \'custom-fetch\'', {})]).toMatchInlineSnapshot(`
[
"useCustomFetch",
"someThingRenamed",
]
`)
expect([...detectImportNames('import { useCustomFetch, someThing as someThingRenamed } from \'custom-fetch\'', keyedComposables)]).toMatchInlineSnapshot(`
[
"someThingRenamed",
]
`)
})
})

View File

@ -63,7 +63,7 @@ export default defineNuxtConfig({
keyedComposables: [
{
name: 'useCustomKeyedComposable',
source: 'pages/keyed-composables/index.vue',
source: '~/other-composables-folder/custom-keyed-composable',
argumentLength: 1
}
]

View File

@ -0,0 +1,3 @@
export function useCustomKeyedComposable (arg?: string) {
return arg
}

View File

@ -1,4 +1,6 @@
<script setup lang="ts">
import { useCustomKeyedComposable } from '~/other-composables-folder/custom-keyed-composable'
const useLocalState = () => useState(() => {
if (process.client) { console.error('running usestate') }
return { foo: Math.random() }
@ -33,7 +35,6 @@ const useLocalLazyFetch = () => useLazyFetch(() => '/api/counter')
const { data: useLazyFetchTest1 } = await useLocalLazyFetch()
const { data: useLazyFetchTest2 } = await useLocalLazyFetch()
const useCustomKeyedComposable = (arg?: string) => arg
const useLocalCustomKeyedComposable = () => useCustomKeyedComposable()
const useMyAsyncDataTest1 = useLocalCustomKeyedComposable()
const useMyAsyncDataTest2 = useLocalCustomKeyedComposable()

View File

@ -39,9 +39,12 @@ function localScopedComposables () {
})()]
}
return [...basic(), ...hoisting(), ...complex(), ...deeperScope()]
}
function useCustomKeyedComposable (arg?: string) {
return _assert(arg)
}
return [...basic(), ...hoisting(), ...complex(), ...deeperScope(), useCustomKeyedComposable()]
}
const skippedLocalScopedComposables = localScopedComposables().every(res => res === 'was not keyed')
</script>