diff --git a/packages/vite/src/plugins/composable-keys.ts b/packages/vite/src/plugins/composable-keys.ts index 86df9a2659..43cd18b353 100644 --- a/packages/vite/src/plugins/composable-keys.ts +++ b/packages/vite/src/plugins/composable-keys.ts @@ -224,7 +224,7 @@ class ScopedVarsCollector { const NUXT_IMPORT_RE = /nuxt|#app|#imports/ -function detectImportNames (code: string, composableMeta: Record) { +export function detectImportNames (code: string, composableMeta: Record) { const imports = findStaticImports(code) const names = new Set() for (const i of imports) { @@ -235,7 +235,7 @@ function detectImportNames (code: string, composableMeta: Record { + 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", + ] + `) + }) +}) diff --git a/test/fixtures/basic/nuxt.config.ts b/test/fixtures/basic/nuxt.config.ts index 19f8dfb8b7..5a6d38d138 100644 --- a/test/fixtures/basic/nuxt.config.ts +++ b/test/fixtures/basic/nuxt.config.ts @@ -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 } ] diff --git a/test/fixtures/basic/other-composables-folder/custom-keyed-composable.ts b/test/fixtures/basic/other-composables-folder/custom-keyed-composable.ts new file mode 100644 index 0000000000..f149fd3585 --- /dev/null +++ b/test/fixtures/basic/other-composables-folder/custom-keyed-composable.ts @@ -0,0 +1,3 @@ +export function useCustomKeyedComposable (arg?: string) { + return arg +} diff --git a/test/fixtures/basic/pages/keyed-composables/index.vue b/test/fixtures/basic/pages/keyed-composables/index.vue index e08d87b814..97428b2814 100644 --- a/test/fixtures/basic/pages/keyed-composables/index.vue +++ b/test/fixtures/basic/pages/keyed-composables/index.vue @@ -1,4 +1,6 @@