1
1
import { capitalize } from '@vue/shared'
2
2
import type { Node , ObjectExpression , Statement } from '@babel/types'
3
- import { partition } from '@antfu/utils'
3
+ import { notNullish , partition , uniq } from '@antfu/utils'
4
4
import type { ParsedSFC , ScriptSetupTransformOptions } from '../types'
5
5
import { applyMacros } from './macros'
6
6
import { getIdentifierDeclarations } from './identifiers'
7
7
import { generate , t } from './babel'
8
- import { isNotNil , pascalize } from './utils'
8
+ import { pascalize } from './utils'
9
9
10
- function isAsyncImport ( node : any ) {
11
- if ( node . type === 'VariableDeclaration' ) {
10
+ function isAsyncImport ( node : Statement ) {
11
+ if ( t . isVariableDeclaration ( node ) ) {
12
12
const declaration = node . declarations [ 0 ]
13
13
14
- return declaration ?. init ?. callee ?. name === 'defineAsyncComponent'
14
+ return (
15
+ declaration !== undefined
16
+ && t . isCallExpression ( declaration . init )
17
+ && t . isIdentifier ( declaration . init . callee )
18
+ && declaration . init . callee . name === 'defineAsyncComponent'
19
+ )
15
20
}
16
21
17
22
return false
18
23
}
19
24
20
- export function transformScriptSetup ( sfc : ParsedSFC , options ?: ScriptSetupTransformOptions ) {
25
+ export function transformScriptSetup (
26
+ sfc : ParsedSFC ,
27
+ options ?: ScriptSetupTransformOptions ,
28
+ ) {
21
29
const { scriptSetup, script, template } = sfc
22
30
23
31
const { nodes : body , props, expose } = applyMacros ( scriptSetup . ast . body )
@@ -26,16 +34,17 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
26
34
body ,
27
35
n =>
28
36
isAsyncImport ( n )
29
- || n . type === 'ImportDeclaration'
30
- || n . type === 'ExportNamedDeclaration'
31
- || n . type . startsWith ( 'TS' ) ,
37
+ || t . isImportDeclaration ( n )
38
+ || t . isExportNamedDeclaration ( n )
39
+ || n . type . startsWith ( 'TS' ) ,
32
40
)
33
41
34
42
// get all identifiers in `<script setup>`
35
- const declarations = new Set < string > ( )
36
- getIdentifierDeclarations ( hoisted , declarations )
37
- getIdentifierDeclarations ( setupBody , declarations )
38
- const declarationArray = Array . from ( declarations ) . filter ( isNotNil )
43
+ const declarations = [
44
+ ...getIdentifierDeclarations ( hoisted ) ,
45
+ ...getIdentifierDeclarations ( setupBody ) ,
46
+ ]
47
+ const declarationArray = uniq ( declarations ) . filter ( notNullish )
39
48
40
49
// filter out identifiers that are used in `<template>`
41
50
const returns : ObjectExpression [ 'properties' ] = declarationArray
@@ -45,19 +54,24 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
45
54
return t . objectProperty ( id , id , false , true )
46
55
} )
47
56
48
- const components = Array . from ( template . components ) . map ( component =>
49
- declarationArray . find ( declare => declare === component )
50
- ?? declarationArray . find ( declare => pascalize ( declare ) === component ) ,
51
- ) . filter ( isNotNil )
57
+ const components = Array . from ( template . components )
58
+ . map (
59
+ component =>
60
+ declarationArray . find ( declare => declare === component )
61
+ ?? declarationArray . find ( declare => pascalize ( declare ) === component ) ,
62
+ )
63
+ . filter ( notNullish )
52
64
53
- const directiveDeclaration = Array . from ( template . directives ) . map ( ( directive ) => {
54
- const identifier = declarationArray . find ( declaration => declaration === `v${ capitalize ( directive ) } ` )
55
- if ( identifier === undefined )
56
- return undefined
65
+ const directiveDeclaration = Array . from ( template . directives )
66
+ . map ( ( directive ) => {
67
+ const identifier = declarationArray . find (
68
+ declaration => declaration === `v${ capitalize ( directive ) } ` ,
69
+ )
70
+ if ( identifier === undefined ) return undefined
57
71
58
- return { identifier, directive }
59
- } ,
60
- ) . filter ( isNotNil )
72
+ return { identifier, directive }
73
+ } )
74
+ . filter ( notNullish )
61
75
62
76
// append `<script setup>` imports to `<script>`
63
77
@@ -71,10 +85,7 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
71
85
if ( node . type === 'ExportDefaultDeclaration' ) {
72
86
hasBody = true
73
87
return t . variableDeclaration ( 'const' , [
74
- t . variableDeclarator (
75
- __sfc ,
76
- node . declaration as any ,
77
- ) ,
88
+ t . variableDeclarator ( __sfc , node . declaration as any ) ,
78
89
] )
79
90
}
80
91
return node
@@ -90,10 +101,7 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
90
101
if ( ! hasBody ) {
91
102
ast . body . push (
92
103
t . variableDeclaration ( 'const' , [
93
- t . variableDeclarator (
94
- __sfc ,
95
- t . objectExpression ( [ ] ) ,
96
- ) ,
104
+ t . variableDeclarator ( __sfc , t . objectExpression ( [ ] ) ) ,
97
105
] ) ,
98
106
)
99
107
}
@@ -104,7 +112,8 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
104
112
hasBody = true
105
113
ast . body . push (
106
114
t . expressionStatement (
107
- t . assignmentExpression ( '=' ,
115
+ t . assignmentExpression (
116
+ '=' ,
108
117
t . memberExpression ( __sfc , t . identifier ( 'props' ) ) ,
109
118
props as any ,
110
119
) ,
@@ -126,15 +135,13 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
126
135
127
136
ast . body . push (
128
137
t . expressionStatement (
129
- t . assignmentExpression ( '=' ,
138
+ t . assignmentExpression (
139
+ '=' ,
130
140
t . memberExpression ( __sfc , t . identifier ( 'setup' ) ) ,
131
- t . arrowFunctionExpression ( [
132
- t . identifier ( '__props' ) ,
133
- t . identifier ( '__ctx' ) ,
134
- ] , t . blockStatement ( [
135
- ...setupBody ,
136
- returnStatement as any ,
137
- ] ) ) ,
141
+ t . arrowFunctionExpression (
142
+ [ t . identifier ( '__props' ) , t . identifier ( '__ctx' ) ] ,
143
+ t . blockStatement ( [ ...setupBody , returnStatement as any ] ) ,
144
+ ) ,
138
145
) ,
139
146
) as any ,
140
147
)
@@ -153,7 +160,8 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
153
160
154
161
ast . body . push (
155
162
t . expressionStatement (
156
- t . assignmentExpression ( '=' ,
163
+ t . assignmentExpression (
164
+ '=' ,
157
165
t . memberExpression ( __sfc , t . identifier ( 'components' ) ) ,
158
166
t . callExpression (
159
167
t . memberExpression ( t . identifier ( 'Object' ) , t . identifier ( 'assign' ) ) ,
@@ -172,17 +180,20 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
172
180
if ( directiveDeclaration . length ) {
173
181
hasBody = true
174
182
const directivesObject = t . objectExpression (
175
- directiveDeclaration . map ( ( { directive, identifier } ) => ( t . objectProperty (
176
- t . identifier ( directive ) ,
177
- t . identifier ( identifier ) ,
178
- false ,
179
- false ,
180
- ) ) ) ,
183
+ directiveDeclaration . map ( ( { directive, identifier } ) =>
184
+ t . objectProperty (
185
+ t . identifier ( directive ) ,
186
+ t . identifier ( identifier ) ,
187
+ false ,
188
+ false ,
189
+ ) ,
190
+ ) ,
181
191
)
182
192
183
193
ast . body . push (
184
194
t . expressionStatement (
185
- t . assignmentExpression ( '=' ,
195
+ t . assignmentExpression (
196
+ '=' ,
186
197
t . memberExpression ( __sfc , t . identifier ( 'directives' ) ) ,
187
198
t . callExpression (
188
199
t . memberExpression ( t . identifier ( 'Object' ) , t . identifier ( 'assign' ) ) ,
@@ -205,9 +216,7 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
205
216
206
217
// re-export
207
218
// `export default __sfc_main`
208
- ast . body . push (
209
- t . exportDefaultDeclaration ( __sfc ) as any ,
210
- )
219
+ ast . body . push ( t . exportDefaultDeclaration ( __sfc ) as any )
211
220
212
221
ast = options ?. astTransforms ?. post ?.( ast , sfc ) || ast
213
222
0 commit comments