1- import Debug from 'debug'
21import type {
32 CallExpression , ObjectProperty , File , VariableDeclaration , FunctionExpression , BlockStatement ,
43} from '@babel/types'
54import type MagicString from 'magic-string'
65import { parse , ParseResult } from '@babel/parser'
76import traverse from '@babel/traverse'
8- import { pascalCase , stringifyComponentImport } from '../utils'
9- import type { Context } from '../context'
10- import { ResolveResult } from '../transformer'
11- import { SupportedTransformer } from '../..'
12-
13- const debug = Debug ( 'unplugin-vue-components:transform:directive' )
7+ import { ResolveResult } from '../../transformer'
148
159/**
1610 * get Vue 2 render function position
@@ -20,16 +14,16 @@ const debug = Debug('unplugin-vue-components:transform:directive')
2014const getRenderFnStart = ( ast : ParseResult < File > ) : number => {
2115 const renderFn = ast . program . body . find ( ( node ) : node is VariableDeclaration =>
2216 node . type === 'VariableDeclaration'
23- && node . declarations [ 0 ] . id . type === 'Identifier'
24- && node . declarations [ 0 ] . id . name === 'render' ,
17+ && node . declarations [ 0 ] . id . type === 'Identifier'
18+ && node . declarations [ 0 ] . id . name === 'render' ,
2519 )
2620 const start = ( ( ( renderFn ?. declarations [ 0 ] . init as FunctionExpression ) . body ) as BlockStatement ) . start
2721 if ( start === null )
2822 throw new Error ( '[unplugin-vue-components:directive] Cannot find render function position.' )
2923 return start + 1
3024}
3125
32- const resolveVue2 = ( code : string , s : MagicString ) : ResolveResult [ ] => {
26+ export const resolve = ( code : string , s : MagicString ) : ResolveResult [ ] => {
3327 const ast = parse ( code , {
3428 sourceType : 'module' ,
3529 } )
@@ -51,8 +45,8 @@ const resolveVue2 = (code: string, s: MagicString): ResolveResult[] => {
5145 const directives = args [ 1 ] . properties . find (
5246 ( property ) : property is ObjectProperty =>
5347 property . type === 'ObjectProperty'
54- && property . key . type === 'Identifier'
55- && property . key . name === 'directives' ,
48+ && property . key . type === 'Identifier'
49+ && property . key . name === 'directives' ,
5650 ) ?. value
5751 if ( ! directives || directives . type !== 'ArrayExpression' )
5852 continue
@@ -65,8 +59,8 @@ const resolveVue2 = (code: string, s: MagicString): ResolveResult[] => {
6559 const nameNode = directive . properties . find (
6660 ( p ) : p is ObjectProperty =>
6761 p . type === 'ObjectProperty'
68- && p . key . type === 'Identifier'
69- && p . key . name === 'name' ,
62+ && p . key . type === 'Identifier'
63+ && p . key . name === 'name' ,
7064 ) ?. value
7165 if ( nameNode ?. type !== 'StringLiteral' ) continue
7266 const name = nameNode . value
@@ -82,42 +76,3 @@ const resolveVue2 = (code: string, s: MagicString): ResolveResult[] => {
8276
8377 return results
8478}
85-
86- const resolveVue3 = ( code : string , s : MagicString ) : ResolveResult [ ] => {
87- const results : ResolveResult [ ] = [ ]
88-
89- for ( const match of code . matchAll ( / _ r e s o l v e D i r e c t i v e \( " ( .+ ?) " \) / g) ) {
90- const matchedName = match [ 1 ]
91- if ( match . index != null && matchedName && ! matchedName . startsWith ( '_' ) ) {
92- const start = match . index
93- const end = start + match [ 0 ] . length
94- results . push ( {
95- rawName : matchedName ,
96- replace : resolved => s . overwrite ( start , end , resolved ) ,
97- } )
98- }
99- }
100-
101- return results
102- }
103-
104- export default async ( code : string , transformer : SupportedTransformer , s : MagicString , ctx : Context , sfcPath : string ) => {
105- let no = 0
106-
107- const results = transformer === 'vue2' ? resolveVue2 ( code , s ) : resolveVue3 ( code , s )
108- for ( const { rawName, replace } of results ) {
109- debug ( `| ${ rawName } ` )
110- const name = pascalCase ( rawName )
111- ctx . updateUsageMap ( sfcPath , [ name ] )
112-
113- const directive = await ctx . findComponent ( name , 'directive' , [ sfcPath ] )
114- if ( ! directive ) continue
115-
116- const varName = `__unplugin_directives_${ no } `
117- s . prepend ( `${ stringifyComponentImport ( { ...directive , name : varName } , ctx ) } ;\n` )
118- no += 1
119- replace ( varName )
120- }
121-
122- debug ( `^ (${ no } )` )
123- }
0 commit comments