feat(nuxt): imports.autoImport option to disable auto-imports (#6768)

This commit is contained in:
Anthony Fu 2022-08-24 16:44:38 +08:00 committed by GitHub
parent 5254b037b8
commit 856c2a6fbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 5 deletions

View File

@ -55,3 +55,17 @@ Nuxt exposes every auto-import with the `#imports` alias that can be used to mak
const double = computed(() => count.value * 2)
</script>
```
## Disable Auto-imports
In case you want to disable auto-imports, you can set `imports.autoImport` to `false`.
```ts [nuxt.config.ts]
export default defineNuxtConfig({
imports: {
autoImport: false
}
})
```
This will disable implicit auto imports completely but it's still possible to use [Explicit Imports](#explicit-imports).

View File

@ -12,6 +12,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
configKey: 'imports'
},
defaults: {
autoImport: true,
presets: defaultPresets,
global: false,
imports: [],
@ -55,7 +56,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
imports: options.imports,
virtualImports: ['#imports'],
addons: {
vueTemplate: true
vueTemplate: options.autoImport
}
})
@ -113,7 +114,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
await regenerateImports()
// Generate types
addDeclarationTemplates(ctx)
addDeclarationTemplates(ctx, options)
// Add generated types to `nuxt.d.ts`
nuxt.hook('prepare:types', ({ references }) => {
@ -135,7 +136,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
}
})
function addDeclarationTemplates (ctx: Unimport) {
function addDeclarationTemplates (ctx: Unimport, options: Partial<ImportsOptions>) {
const nuxt = useNuxt()
// Remove file extension for benefit of TypeScript
@ -163,6 +164,10 @@ function addDeclarationTemplates (ctx: Unimport) {
addTemplate({
filename: 'types/imports.d.ts',
getContents: () => '// Generated by auto imports\n' + ctx.generateTypeDeclarations({ resolvePath: r })
getContents: () => '// Generated by auto imports\n' + (
options.autoImport
? ctx.generateTypeDeclarations({ resolvePath: r })
: '// Implicit auto importing is disabled, you can use explicitly import from `#imports` instead.'
)
})
}

View File

@ -44,7 +44,7 @@ export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: { ct
return
}
const { s } = await ctx.injectImports(code, id, { autoImport: !isNodeModule })
const { s } = await ctx.injectImports(code, id, { autoImport: options.autoImport && !isNodeModule })
if (s.hasChanged()) {
return {
code: s.toString(),

View File

@ -1,7 +1,25 @@
import { UnimportOptions } from 'unimport'
export interface ImportsOptions extends UnimportOptions {
/**
* Enable implicit auto import from Vue, Nuxt and module contributed utilities.
* Generate global TypeScript definitions.
*
* @default true
*/
autoImport?: boolean
/**
* Directories to scan for auto imports.
*
* @see https://v3.nuxtjs.org/guide/directory-structure/composables/#how-files-are-scanned
* @default ['./composables']
*/
dirs?: string[]
/**
* Assign auto imported utilities to `globalThis` instead of using built time transformation.
*
* @default false
*/
global?: boolean
transform?: {
exclude?: RegExp[]