1
- import Debug from 'debug'
2
1
import type {
3
2
CallExpression , ObjectProperty , File , VariableDeclaration , FunctionExpression , BlockStatement ,
4
3
} from '@babel/types'
5
4
import type MagicString from 'magic-string'
6
5
import { parse , ParseResult } from '@babel/parser'
7
6
import 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'
14
8
15
9
/**
16
10
* get Vue 2 render function position
@@ -20,16 +14,16 @@ const debug = Debug('unplugin-vue-components:transform:directive')
20
14
const getRenderFnStart = ( ast : ParseResult < File > ) : number => {
21
15
const renderFn = ast . program . body . find ( ( node ) : node is VariableDeclaration =>
22
16
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' ,
25
19
)
26
20
const start = ( ( ( renderFn ?. declarations [ 0 ] . init as FunctionExpression ) . body ) as BlockStatement ) . start
27
21
if ( start === null )
28
22
throw new Error ( '[unplugin-vue-components:directive] Cannot find render function position.' )
29
23
return start + 1
30
24
}
31
25
32
- const resolveVue2 = ( code : string , s : MagicString ) : ResolveResult [ ] => {
26
+ export const resolve = ( code : string , s : MagicString ) : ResolveResult [ ] => {
33
27
const ast = parse ( code , {
34
28
sourceType : 'module' ,
35
29
} )
@@ -51,8 +45,8 @@ const resolveVue2 = (code: string, s: MagicString): ResolveResult[] => {
51
45
const directives = args [ 1 ] . properties . find (
52
46
( property ) : property is ObjectProperty =>
53
47
property . type === 'ObjectProperty'
54
- && property . key . type === 'Identifier'
55
- && property . key . name === 'directives' ,
48
+ && property . key . type === 'Identifier'
49
+ && property . key . name === 'directives' ,
56
50
) ?. value
57
51
if ( ! directives || directives . type !== 'ArrayExpression' )
58
52
continue
@@ -65,8 +59,8 @@ const resolveVue2 = (code: string, s: MagicString): ResolveResult[] => {
65
59
const nameNode = directive . properties . find (
66
60
( p ) : p is ObjectProperty =>
67
61
p . type === 'ObjectProperty'
68
- && p . key . type === 'Identifier'
69
- && p . key . name === 'name' ,
62
+ && p . key . type === 'Identifier'
63
+ && p . key . name === 'name' ,
70
64
) ?. value
71
65
if ( nameNode ?. type !== 'StringLiteral' ) continue
72
66
const name = nameNode . value
@@ -82,42 +76,3 @@ const resolveVue2 = (code: string, s: MagicString): ResolveResult[] => {
82
76
83
77
return results
84
78
}
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