feat(kit): add addImportsSources utility (#7270)

This commit is contained in:
Daniel Roe 2022-09-06 11:17:41 +01:00 committed by GitHub
parent 396d55edb8
commit a931667111
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import { Import } from 'unimport' import type { Import } from 'unimport'
import type { ImportPresetWithDeprecation } from '@nuxt/schema'
import { useNuxt } from './context' import { useNuxt } from './context'
import { assertNuxtCompatibility } from './compatibility' import { assertNuxtCompatibility } from './compatibility'
@ -31,3 +32,14 @@ export function addImportsDir (dirs: string | string[]) {
* @deprecated Please use `addImportsDir` instead with nuxt>=3.0.0-rc.9 * @deprecated Please use `addImportsDir` instead with nuxt>=3.0.0-rc.9
*/ */
export const addAutoImportDir = addImportsDir export const addAutoImportDir = addImportsDir
export function addImportsSources (presets: ImportPresetWithDeprecation | ImportPresetWithDeprecation[]) {
assertNuxtCompatibility({ bridge: true })
// TODO: Use imports:* when widely adopted
useNuxt().hook('autoImports:sources', (_presets: ImportPresetWithDeprecation[]) => {
for (const preset of (Array.isArray(presets) ? presets : [presets])) {
_presets.push(preset)
}
}, { allowDeprecated: true })
}

View File

@ -152,7 +152,7 @@ async function initNuxt (nuxt: Nuxt) {
nuxt.hooks.deprecateHooks({ nuxt.hooks.deprecateHooks({
'autoImports:sources': { 'autoImports:sources': {
to: 'imports:sources', to: 'imports:sources',
message: '`autoImports:sources` hook is deprecated. Use `imports:sources` with `nuxt>=3.0.0-rc.9`.' message: '`autoImports:sources` hook is deprecated. Use `addImportsSources()` from `@nuxt/kit` or `imports:dirs` with `nuxt>=3.0.0-rc.10`.'
}, },
'autoImports:dirs': { 'autoImports:dirs': {
to: 'imports:dirs', to: 'imports:dirs',