forked from unplugin/unplugin-vue-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
32 lines (26 loc) 路 1.15 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Debug from 'debug'
import type MagicString from 'magic-string'
import { pascalCase, stringifyComponentImport } from '../../utils'
import type { Context } from '../../context'
import type { SupportedTransformer } from '../../..'
import { DIRECTIVE_IMPORT_PREFIX } from '../../constants'
import vue2Resolver from './vue2'
import vue3Resolver from './vue3'
const debug = Debug('unplugin-vue-components:transform:directive')
export default async function transformDirective(code: string, transformer: SupportedTransformer, s: MagicString, ctx: Context, sfcPath: string) {
let no = 0
const results = await (transformer === 'vue2' ? vue2Resolver(code, s) : vue3Resolver(code, s))
for (const { rawName, replace } of results) {
debug(`| ${rawName}`)
const name = `${DIRECTIVE_IMPORT_PREFIX}${pascalCase(rawName)}`
ctx.updateUsageMap(sfcPath, [name])
const directive = await ctx.findComponent(name, 'directive', [sfcPath])
if (!directive)
continue
const varName = `__unplugin_directives_${no}`
s.prepend(`${stringifyComponentImport({ ...directive, as: varName }, ctx)};\n`)
no += 1
replace(varName)
}
debug(`^ (${no})`)
}