Nuxt/packages/nuxt3/src/auto-imports/context.ts
Anthony Fu 550a9f2e12
feat: auto-import for composables (#1176)
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
2021-10-20 11:47:18 +02:00

43 lines
1.0 KiB
TypeScript

import type { AutoImport } from '@nuxt/kit'
export interface AutoImportContext {
autoImports: AutoImport[]
matchRE: RegExp
map: Map<string, AutoImport>
}
export function createAutoImportContext (): AutoImportContext {
return {
autoImports: [],
map: new Map(),
matchRE: /__never__/
}
}
export function updateAutoImportContext (ctx: AutoImportContext) {
// Detect duplicates
const usedNames = new Set()
for (const autoImport of ctx.autoImports) {
if (usedNames.has(autoImport.as)) {
autoImport.disabled = true
console.warn(`Disabling duplicate auto import '${autoImport.as}' (imported from '${autoImport.from}')`)
} else {
usedNames.add(autoImport.as)
}
}
// Filter out disabled auto imports
ctx.autoImports = ctx.autoImports.filter(i => i.disabled !== true)
// Create regex
ctx.matchRE = new RegExp(`\\b(${ctx.autoImports.map(i => i.as).join('|')})\\b`, 'g')
// Create map
ctx.map.clear()
for (const autoImport of ctx.autoImports) {
ctx.map.set(autoImport.as, autoImport)
}
return ctx
}