feat(nuxt): support auto imports within <template> (#4904)

This commit is contained in:
Anthony Fu 2022-06-09 04:09:31 +08:00 committed by GitHub
parent 4e2dc037aa
commit a802b87ac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 2 deletions

View File

@ -36,7 +36,10 @@ export default defineNuxtModule<Partial<AutoImportsOptions>>({
// Create a context to share state between module internals // Create a context to share state between module internals
const ctx = createUnimport({ const ctx = createUnimport({
presets: options.presets, presets: options.presets,
imports: options.imports imports: options.imports,
addons: {
vueTemplate: true
}
}) })
// composables/ dirs from all layers // composables/ dirs from all layers

View File

@ -39,7 +39,7 @@ export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: {ctx
} }
}, },
async transform (_code, id) { async transform (_code, id) {
const { code, s } = await ctx.injectImports(_code) const { code, s } = await ctx.injectImports(_code, id)
if (code === _code) { if (code === _code) {
return return
} }

View File

@ -41,6 +41,7 @@ describe('pages', () => {
// composables auto import // composables auto import
expect(html).toContain('Composable | foo: auto imported from ~/components/foo.ts') expect(html).toContain('Composable | foo: auto imported from ~/components/foo.ts')
expect(html).toContain('Composable | bar: auto imported from ~/components/useBar.ts') expect(html).toContain('Composable | bar: auto imported from ~/components/useBar.ts')
expect(html).toContain('Composable | template: auto imported from ~/components/template.ts')
// should import components // should import components
expect(html).toContain('This is a custom component with a named export.') expect(html).toContain('This is a custom component with a named export.')

View File

@ -0,0 +1 @@
export const templateAutoImport = 'auto imported from ~/components/template.ts'

View File

@ -7,6 +7,7 @@
<div>RuntimeConfig | testConfig: {{ config.testConfig }}</div> <div>RuntimeConfig | testConfig: {{ config.testConfig }}</div>
<div>Composable | foo: {{ foo }}</div> <div>Composable | foo: {{ foo }}</div>
<div>Composable | bar: {{ bar }}</div> <div>Composable | bar: {{ bar }}</div>
<div>Composable | template: {{ templateAutoImport }}</div>
<div>Path: {{ $route.fullPath }}</div> <div>Path: {{ $route.fullPath }}</div>
<NuxtLink to="/"> <NuxtLink to="/">
Link Link