From e70ff83e72dc8ae5892bdc20d47f47bc007ca62a Mon Sep 17 00:00:00 2001 From: anhao <117057608+ah-dc@users.noreply.github.com> Date: Wed, 5 Jul 2023 17:35:45 +0800 Subject: [PATCH] fix(vite): fix issue detecting shadowed keyed composables (#21891) --- packages/vite/src/plugins/composable-keys.ts | 4 +-- packages/vite/test/composable-keys.test.ts | 27 +++++++++++++++++++ test/fixtures/basic/nuxt.config.ts | 2 +- .../custom-keyed-composable.ts | 3 +++ .../basic/pages/keyed-composables/index.vue | 3 ++- .../basic/pages/keyed-composables/local.vue | 7 +++-- 6 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 packages/vite/test/composable-keys.test.ts create mode 100644 test/fixtures/basic/other-composables-folder/custom-keyed-composable.ts 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 @@