Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

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

Merged
merged 1 commit into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/content/2.guide/1.concepts/4.auto-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
13 changes: 9 additions & 4 deletions packages/nuxt/src/imports/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
configKey: 'imports'
},
defaults: {
autoImport: true,
presets: defaultPresets,
global: false,
imports: [],
Expand Down Expand Up @@ -55,7 +56,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
imports: options.imports,
virtualImports: ['#imports'],
addons: {
vueTemplate: true
vueTemplate: options.autoImport
}
})

Expand Down Expand Up @@ -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 }) => {
Expand All @@ -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
Expand Down Expand Up @@ -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.'
)
})
}
2 changes: 1 addition & 1 deletion packages/nuxt/src/imports/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
18 changes: 18 additions & 0 deletions packages/schema/src/types/imports.ts
Original file line number Diff line number Diff line change
@@ -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[]
Expand Down