Skip to content

Commit

Permalink
refactor!: rename autoImports to imports (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Sep 14, 2022
1 parent b03ca40 commit 74c3f4f
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export default defineNuxtConfig({
// transpile: false,

// Disable composables auto importing
// autoImports: false,
// imports: false,

// Do not warn about module incompatibilities
// constraints: false
Expand Down
File renamed without changes.
46 changes: 31 additions & 15 deletions src/auto-imports/module.ts → src/imports/module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { addVitePlugin, addWebpackPlugin, defineNuxtModule, addTemplate, resolveAlias, useNuxt, addPluginTemplate, logger } from '@nuxt/kit'
import { isAbsolute, join, relative, resolve, normalize } from 'pathe'
import { createUnimport, Import, toImports, Unimport } from 'unimport'
import { AutoImportsOptions, ImportPresetWithDeprecation } from '@nuxt/schema'
import { ImportsOptions, ImportPresetWithDeprecation } from '@nuxt/schema'
import { TransformPlugin } from './transform'
import { defaultPresets } from './presets'
import { scanForComposables } from './composables'

export default defineNuxtModule<Partial<AutoImportsOptions>>({
export default defineNuxtModule<Partial<ImportsOptions>>({
meta: {
name: 'auto-imports',
configKey: 'autoImports'
name: 'imports',
configKey: 'imports'
},
defaults: {
presets: defaultPresets,
Expand All @@ -21,13 +21,29 @@ export default defineNuxtModule<Partial<AutoImportsOptions>>({
}
},
async setup (options, nuxt) {
// Deprecate hooks
nuxt.hooks.deprecateHooks({
'autoImports:sources': {
to: 'imports:sources',
message: '`autoImports:sources` hook is deprecated. Use `addImportsSources()` from `@nuxt/kit` or `imports:dirs` with latest Nuxt Bridge.'
},
'autoImports:dirs': {
to: 'imports:dirs',
message: '`autoImports:dirs` hook is deprecated. Use `addImportsDir()` from `@nuxt/kit` or `imports:dirs` with latest Nuxt Bridge.'
},
'autoImports:extend': {
to: 'imports:extend',
message: '`autoImports:extend` hook is deprecated. Use `addImports()` from `@nuxt/kit` or `imports:extend` with latest Nuxt Bridge.'
}
})

// Allow modules extending sources
await nuxt.callHook('autoImports:sources', options.presets as ImportPresetWithDeprecation[])
await nuxt.callHook('imports:sources', options.presets as ImportPresetWithDeprecation[])

options.presets.forEach((i: ImportPresetWithDeprecation) => {
if (typeof i !== 'string' && i.names && !i.imports) {
i.imports = i.names
logger.warn('auto-imports: presets.names is deprecated, use presets.imports instead')
logger.warn('imports: presets.names is deprecated, use presets.imports instead')
}
})

Expand All @@ -44,12 +60,12 @@ export default defineNuxtModule<Partial<AutoImportsOptions>>({
let composablesDirs = []
for (const layer of nuxt.options._layers) {
composablesDirs.push(resolve(layer.config.srcDir, 'composables'))
for (const dir of (layer.config.autoImports?.dirs ?? [])) {
for (const dir of (layer.config.imports?.dirs ?? layer.config.autoImports?.dirs ?? [])) {
composablesDirs.push(resolve(layer.config.srcDir, dir))
}
}

await nuxt.callHook('autoImports:dirs', composablesDirs)
await nuxt.callHook('imports:dirs', composablesDirs)
composablesDirs = composablesDirs.map(dir => normalize(dir))

// Support for importing from '#imports'
Expand All @@ -64,7 +80,7 @@ export default defineNuxtModule<Partial<AutoImportsOptions>>({
if (nuxt.options.dev && options.global) {
// Add all imports to globalThis in development mode
addPluginTemplate({
filename: 'auto-imports.mjs',
filename: 'imports.mjs',
getContents: () => {
const imports = ctx.getImports()
const importStatement = toImports(imports)
Expand All @@ -78,23 +94,23 @@ export default defineNuxtModule<Partial<AutoImportsOptions>>({
addWebpackPlugin(TransformPlugin.webpack({ ctx, options }))
}

const regenerateAutoImports = async () => {
const regenerateImports = async () => {
// Scan composables/
await scanForComposables(composablesDirs, ctx)
// Allow modules extending
await ctx.modifyDynamicImports(async (imports) => {
await nuxt.callHook('autoImports:extend', imports)
await nuxt.callHook('imports:extend', imports)
})
}

await regenerateAutoImports()
await regenerateImports()

// Generate types
addDeclarationTemplates(ctx)

// Add generated types to `nuxt.d.ts`
nuxt.hook('prepare:types', ({ references }) => {
references.push({ path: resolve(nuxt.options.buildDir, 'types/auto-imports.d.ts') })
references.push({ path: resolve(nuxt.options.buildDir, 'types/imports.d.ts') })
references.push({ path: resolve(nuxt.options.buildDir, 'imports.d.ts') })
})

Expand All @@ -107,7 +123,7 @@ export default defineNuxtModule<Partial<AutoImportsOptions>>({
})

nuxt.hook('builder:generateApp', async () => {
await regenerateAutoImports()
await regenerateImports()
})
}
})
Expand Down Expand Up @@ -139,7 +155,7 @@ function addDeclarationTemplates (ctx: Unimport) {
})

addTemplate({
filename: 'types/auto-imports.d.ts',
filename: 'types/imports.d.ts',
getContents: () => '// Generated by auto imports\n' + ctx.generateTypeDeclarations({ resolvePath: r })
})
}
File renamed without changes.
6 changes: 3 additions & 3 deletions src/auto-imports/transform.ts → src/imports/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { pathToFileURL } from 'url'
import { createUnplugin } from 'unplugin'
import { parseQuery, parseURL } from 'ufo'
import { Unimport } from 'unimport'
import { AutoImportsOptions } from '@nuxt/schema'
import { ImportsOptions } from '@nuxt/schema'

export const TransformPlugin = createUnplugin(({ ctx, options }: {ctx: Unimport, options: Partial<AutoImportsOptions> }) => {
export const TransformPlugin = createUnplugin(({ ctx, options }: { ctx: Unimport, options: Partial<ImportsOptions> }) => {
return {
name: 'nuxt:auto-imports-transform',
name: 'nuxt:imports-transform',
enforce: 'post',
transformInclude (id) {
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
Expand Down
8 changes: 4 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { setupNitroBridge } from './nitro'
import { setupAppBridge } from './app'
import { setupCAPIBridge } from './capi'
import { setupBetterResolve } from './resolve'
import autoImports from './auto-imports/module'
import importsModule from './imports/module'
import { setupTypescript } from './typescript'
import { setupMeta } from './meta'
import { setupTranspile } from './transpile'
Expand All @@ -25,7 +25,7 @@ export default defineNuxtModule({
app: {},
capi: {},
transpile: true,
autoImports: true,
imports: true,
compatibility: true,
meta: null,
// TODO: Remove from 2.16
Expand Down Expand Up @@ -55,8 +55,8 @@ export default defineNuxtModule({
}
await setupCAPIBridge(opts.capi === true ? {} : opts.capi)
}
if (opts.autoImports) {
nuxt.hook('modules:done', () => installModule(autoImports))
if (opts.imports ?? opts.autoImports) {
nuxt.hook('modules:done', () => installModule(importsModule))
}
if (opts.vite) {
const viteModule = await import('./vite/module').then(r => r.default || r) as NuxtModule
Expand Down
2 changes: 1 addition & 1 deletion test/auto-imports.test.ts → test/imports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, describe, it } from 'vitest'
import * as VueFunctions from 'vue'
import { defaultPresets } from '../src/auto-imports/presets'
import { defaultPresets } from '../src/imports/presets'

const excludedVueHelpers = [
// Already globally registered
Expand Down
6 changes: 4 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export interface BridgeConfig {
capi: boolean | {
legacy?: boolean
}
autoImports: boolean
imports: boolean
/** @deprecated */
autoImports?: boolean
transpile: boolean
compatibility: boolean
postcss8: boolean
Expand All @@ -29,4 +31,4 @@ declare module '@nuxt/schema' {
}
}

export declare function defineNuxtConfig(config: NuxtConfig): NuxtConfig
export declare function defineNuxtConfig (config: NuxtConfig): NuxtConfig

0 comments on commit 74c3f4f

Please sign in to comment.