mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +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 { Nuxt3AutoImports } from './imports'
|
||||
import { scanForComposables } from './composables'
|
||||
import { toImports } from './utils'
|
||||
import { toExports, toImports } from './utils'
|
||||
import { AutoImportContext, createAutoImportContext, updateAutoImportContext } from './context'
|
||||
|
||||
export default defineNuxtModule<AutoImportsOptions>({
|
||||
@ -44,6 +44,13 @@ export default defineNuxtModule<AutoImportsOptions>({
|
||||
await nuxt.callHook('autoImports:dirs', composablesDirs)
|
||||
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
|
||||
// @ts-ignore temporary disabled due to #746
|
||||
if (nuxt.options.dev && options.global) {
|
||||
@ -108,6 +115,12 @@ function generateDts (ctx: AutoImportContext) {
|
||||
return path
|
||||
}
|
||||
|
||||
addTemplate({
|
||||
filename: 'imports.d.ts',
|
||||
write: true,
|
||||
getContents: () => toExports(ctx.autoImports)
|
||||
})
|
||||
|
||||
addTemplate({
|
||||
filename: 'auto-imports.d.ts',
|
||||
write: true,
|
||||
|
@ -1,8 +1,7 @@
|
||||
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 map: Record<string, Set<string>> = {}
|
||||
for (const autoImport of autoImports) {
|
||||
if (!map[autoImport.from]) {
|
||||
@ -14,7 +13,11 @@ export function toImports (autoImports: AutoImport[], isCJS = false) {
|
||||
: autoImport.name + aliasKeyword + autoImport.as
|
||||
)
|
||||
}
|
||||
return map
|
||||
}
|
||||
|
||||
export function toImports (autoImports: AutoImport[], isCJS = false) {
|
||||
const map = toImportModuleMap(autoImports, isCJS)
|
||||
if (isCJS) {
|
||||
return Object.entries(map)
|
||||
.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) {
|
||||
let i = arr.length
|
||||
while (i--) {
|
||||
|
Loading…
Reference in New Issue
Block a user