mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-14 10:04:05 +00:00
feat(nuxt): imports.autoImport
option to disable auto-imports (#6768)
This commit is contained in:
parent
5254b037b8
commit
856c2a6fbd
@ -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)
|
const double = computed(() => count.value * 2)
|
||||||
</script>
|
</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).
|
||||||
|
@ -12,6 +12,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
|
|||||||
configKey: 'imports'
|
configKey: 'imports'
|
||||||
},
|
},
|
||||||
defaults: {
|
defaults: {
|
||||||
|
autoImport: true,
|
||||||
presets: defaultPresets,
|
presets: defaultPresets,
|
||||||
global: false,
|
global: false,
|
||||||
imports: [],
|
imports: [],
|
||||||
@ -55,7 +56,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
|
|||||||
imports: options.imports,
|
imports: options.imports,
|
||||||
virtualImports: ['#imports'],
|
virtualImports: ['#imports'],
|
||||||
addons: {
|
addons: {
|
||||||
vueTemplate: true
|
vueTemplate: options.autoImport
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -113,7 +114,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
|
|||||||
await regenerateImports()
|
await regenerateImports()
|
||||||
|
|
||||||
// Generate types
|
// Generate types
|
||||||
addDeclarationTemplates(ctx)
|
addDeclarationTemplates(ctx, options)
|
||||||
|
|
||||||
// Add generated types to `nuxt.d.ts`
|
// Add generated types to `nuxt.d.ts`
|
||||||
nuxt.hook('prepare:types', ({ references }) => {
|
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()
|
const nuxt = useNuxt()
|
||||||
|
|
||||||
// Remove file extension for benefit of TypeScript
|
// Remove file extension for benefit of TypeScript
|
||||||
@ -163,6 +164,10 @@ function addDeclarationTemplates (ctx: Unimport) {
|
|||||||
|
|
||||||
addTemplate({
|
addTemplate({
|
||||||
filename: 'types/imports.d.ts',
|
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.'
|
||||||
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: { ct
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const { s } = await ctx.injectImports(code, id, { autoImport: !isNodeModule })
|
const { s } = await ctx.injectImports(code, id, { autoImport: options.autoImport && !isNodeModule })
|
||||||
if (s.hasChanged()) {
|
if (s.hasChanged()) {
|
||||||
return {
|
return {
|
||||||
code: s.toString(),
|
code: s.toString(),
|
||||||
|
@ -1,7 +1,25 @@
|
|||||||
import { UnimportOptions } from 'unimport'
|
import { UnimportOptions } from 'unimport'
|
||||||
|
|
||||||
export interface ImportsOptions extends UnimportOptions {
|
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[]
|
dirs?: string[]
|
||||||
|
/**
|
||||||
|
* Assign auto imported utilities to `globalThis` instead of using built time transformation.
|
||||||
|
*
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
global?: boolean
|
global?: boolean
|
||||||
transform?: {
|
transform?: {
|
||||||
exclude?: RegExp[]
|
exclude?: RegExp[]
|
||||||
|
Loading…
Reference in New Issue
Block a user