fix(nuxt): respect ignorelist when scanning auto-imports (#22782)

This commit is contained in:
Daniel Roe 2023-08-24 13:42:15 +01:00 committed by GitHub
parent 23723305da
commit b5ce892864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, resolveAlias, tryResolveModule, updateTemplates, useNuxt } from '@nuxt/kit'
import { addTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, isIgnored, resolveAlias, tryResolveModule, updateTemplates, useNuxt } from '@nuxt/kit'
import { isAbsolute, join, normalize, relative, resolve } from 'pathe'
import type { Import, Unimport } from 'unimport'
import { createUnimport, scanDirExports } from 'unimport'
@ -92,7 +92,9 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
// Clear old imports
imports.length = 0
// Scan `composables/`
const composableImports = await scanDirExports(composablesDirs)
const composableImports = await scanDirExports(composablesDirs, {
fileFilter: file => !isIgnored(file)
})
for (const i of composableImports) {
i.priority = i.priority || priorities.find(([dir]) => i.from.startsWith(dir))?.[1]
}

View File

@ -889,6 +889,13 @@ describe('composable tree shaking', () => {
})
})
describe('ignore list', () => {
it('should ignore composable files in .nuxtignore', async () => {
const html = await $fetch('/ignore/composables')
expect(html).toContain('was import ignored: true')
})
})
describe('server tree shaking', () => {
it('should work', async () => {
const html = await $fetch('/client')

1
test/fixtures/basic/.nuxtignore vendored Normal file
View File

@ -0,0 +1 @@
composables/ignored.*

View File

@ -0,0 +1,3 @@
export function useIgnoredImport () {
}

View File

@ -0,0 +1,9 @@
<script setup lang="ts">
const wasImportIgnored = typeof useIgnoredImport === 'undefined'
</script>
<template>
<div>
was import ignored: {{ wasImportIgnored }}
</div>
</template>