fix(nuxt): enable injectAtEnd to reduce circular auto-imports (#28822)

This commit is contained in:
Anthony Fu 2024-09-09 17:30:10 +02:00 committed by GitHub
parent 69d7448980
commit ada1e49f5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 9 additions and 3 deletions

View File

@ -22,6 +22,7 @@ export function createTransformPlugin (nuxt: Nuxt, getComponents: getComponentsT
}, },
], ],
virtualImports: ['#components'], virtualImports: ['#components'],
injectAtEnd: true,
}) })
function getComponentsImports (): Import[] { function getComponentsImports (): Import[] {

View File

@ -40,6 +40,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
// Create a context to share state between module internals // Create a context to share state between module internals
const ctx = createUnimport({ const ctx = createUnimport({
injectAtEnd: true,
...options, ...options,
addons: { addons: {
vueTemplate: options.autoImport, vueTemplate: options.autoImport,

View File

@ -18,6 +18,7 @@ describe('imports:transform', () => {
] ]
const ctx = createUnimport({ const ctx = createUnimport({
injectAtEnd: true,
imports, imports,
}) })

View File

@ -15,7 +15,8 @@ describe('components:transform', () => {
const code = await transform('import { Foo, Bar } from \'#components\'', '/app.vue') const code = await transform('import { Foo, Bar } from \'#components\'', '/app.vue')
expect(code).toMatchInlineSnapshot(` expect(code).toMatchInlineSnapshot(`
"import Foo from '/Foo.vue'; "
import Foo from '/Foo.vue';
import { Bar } from '/Bar.vue'; import { Bar } from '/Bar.vue';
" "
`) `)
@ -28,7 +29,8 @@ describe('components:transform', () => {
const code = await transform('import { Foo, LazyFoo } from \'#components\'', '/app.vue') const code = await transform('import { Foo, LazyFoo } from \'#components\'', '/app.vue')
expect(code).toMatchInlineSnapshot(` expect(code).toMatchInlineSnapshot(`
"import Foo from '/Foo.vue?nuxt_component=server&nuxt_component_name=Foo&nuxt_component_export=default'; "
import Foo from '/Foo.vue?nuxt_component=server&nuxt_component_name=Foo&nuxt_component_export=default';
import LazyFoo from '/Foo.vue?nuxt_component=server,async&nuxt_component_name=Foo&nuxt_component_export=default'; import LazyFoo from '/Foo.vue?nuxt_component=server,async&nuxt_component_name=Foo&nuxt_component_export=default';
" "
`) `)
@ -54,7 +56,8 @@ describe('components:transform', () => {
const code = await transform('import { Foo, LazyFoo } from \'#components\'', '/app.vue') const code = await transform('import { Foo, LazyFoo } from \'#components\'', '/app.vue')
expect(code).toMatchInlineSnapshot(` expect(code).toMatchInlineSnapshot(`
"import Foo from '/Foo.vue?nuxt_component=client&nuxt_component_name=Foo&nuxt_component_export=default'; "
import Foo from '/Foo.vue?nuxt_component=client&nuxt_component_name=Foo&nuxt_component_export=default';
import LazyFoo from '/Foo.vue?nuxt_component=client,async&nuxt_component_name=Foo&nuxt_component_export=default'; import LazyFoo from '/Foo.vue?nuxt_component=client,async&nuxt_component_name=Foo&nuxt_component_export=default';
" "
`) `)