mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-16 21:58:19 +00:00
feat: support importing from #imports
(#2168)
This commit is contained in:
parent
f5d1d9a208
commit
b7a429ca61
@ -4,7 +4,7 @@ import { isAbsolute, join, relative, resolve, normalize } from 'pathe'
|
|||||||
import { TransformPlugin } from './transform'
|
import { TransformPlugin } from './transform'
|
||||||
import { Nuxt3AutoImports } from './imports'
|
import { Nuxt3AutoImports } from './imports'
|
||||||
import { scanForComposables } from './composables'
|
import { scanForComposables } from './composables'
|
||||||
import { toImports } from './utils'
|
import { toExports, toImports } from './utils'
|
||||||
import { AutoImportContext, createAutoImportContext, updateAutoImportContext } from './context'
|
import { AutoImportContext, createAutoImportContext, updateAutoImportContext } from './context'
|
||||||
|
|
||||||
export default defineNuxtModule<AutoImportsOptions>({
|
export default defineNuxtModule<AutoImportsOptions>({
|
||||||
@ -44,6 +44,13 @@ export default defineNuxtModule<AutoImportsOptions>({
|
|||||||
await nuxt.callHook('autoImports:dirs', composablesDirs)
|
await nuxt.callHook('autoImports:dirs', composablesDirs)
|
||||||
composablesDirs = composablesDirs.map(dir => normalize(dir))
|
composablesDirs = composablesDirs.map(dir => normalize(dir))
|
||||||
|
|
||||||
|
// Support for importing from '#imports'
|
||||||
|
addTemplate({
|
||||||
|
filename: 'imports.mjs',
|
||||||
|
getContents: () => toExports(ctx.autoImports)
|
||||||
|
})
|
||||||
|
nuxt.options.alias['#imports'] = join(nuxt.options.buildDir, 'imports')
|
||||||
|
|
||||||
// Transpile and injection
|
// Transpile and injection
|
||||||
// @ts-ignore temporary disabled due to #746
|
// @ts-ignore temporary disabled due to #746
|
||||||
if (nuxt.options.dev && options.global) {
|
if (nuxt.options.dev && options.global) {
|
||||||
@ -108,6 +115,12 @@ function generateDts (ctx: AutoImportContext) {
|
|||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addTemplate({
|
||||||
|
filename: 'imports.d.ts',
|
||||||
|
write: true,
|
||||||
|
getContents: () => toExports(ctx.autoImports)
|
||||||
|
})
|
||||||
|
|
||||||
addTemplate({
|
addTemplate({
|
||||||
filename: 'auto-imports.d.ts',
|
filename: 'auto-imports.d.ts',
|
||||||
write: true,
|
write: true,
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import type { AutoImport } from '@nuxt/schema'
|
import type { AutoImport } from '@nuxt/schema'
|
||||||
|
|
||||||
export function toImports (autoImports: AutoImport[], isCJS = false) {
|
export function toImportModuleMap (autoImports: AutoImport[], isCJS = false) {
|
||||||
const aliasKeyword = isCJS ? ' : ' : ' as '
|
const aliasKeyword = isCJS ? ' : ' : ' as '
|
||||||
|
|
||||||
const map: Record<string, Set<string>> = {}
|
const map: Record<string, Set<string>> = {}
|
||||||
for (const autoImport of autoImports) {
|
for (const autoImport of autoImports) {
|
||||||
if (!map[autoImport.from]) {
|
if (!map[autoImport.from]) {
|
||||||
@ -14,7 +13,11 @@ export function toImports (autoImports: AutoImport[], isCJS = false) {
|
|||||||
: autoImport.name + aliasKeyword + autoImport.as
|
: autoImport.name + aliasKeyword + autoImport.as
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
return map
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toImports (autoImports: AutoImport[], isCJS = false) {
|
||||||
|
const map = toImportModuleMap(autoImports, isCJS)
|
||||||
if (isCJS) {
|
if (isCJS) {
|
||||||
return Object.entries(map)
|
return Object.entries(map)
|
||||||
.map(([name, imports]) => `const { ${Array.from(imports).join(', ')} } = require('${name}');`)
|
.map(([name, imports]) => `const { ${Array.from(imports).join(', ')} } = require('${name}');`)
|
||||||
@ -26,6 +29,13 @@ export function toImports (autoImports: AutoImport[], isCJS = false) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function toExports (autoImports: AutoImport[]) {
|
||||||
|
const map = toImportModuleMap(autoImports, false)
|
||||||
|
return Object.entries(map)
|
||||||
|
.map(([name, imports]) => `export { ${Array.from(imports).join(', ')} } from '${name}';`)
|
||||||
|
.join('\n')
|
||||||
|
}
|
||||||
|
|
||||||
export function filterInPlace<T> (arr: T[], predicate: (v: T) => any) {
|
export function filterInPlace<T> (arr: T[], predicate: (v: T) => any) {
|
||||||
let i = arr.length
|
let i = arr.length
|
||||||
while (i--) {
|
while (i--) {
|
||||||
|
Loading…
Reference in New Issue
Block a user