@@ -4,12 +4,15 @@ import {
4
4
parseSFC ,
5
5
MagicString ,
6
6
checkInvalidScopeReference ,
7
+ babelParse ,
8
+ getLang ,
7
9
} from '@vue-macros/common'
8
10
import type { Thenable , TransformResult } from 'unplugin'
9
11
import type {
10
12
CallExpression ,
11
13
Node ,
12
14
ObjectProperty ,
15
+ Program ,
13
16
Statement ,
14
17
StringLiteral ,
15
18
} from '@babel/types'
@@ -25,6 +28,39 @@ function isStringLiteral(node: Node | null | undefined): node is StringLiteral {
25
28
return node ?. type === 'StringLiteral'
26
29
}
27
30
31
+ /**
32
+ * Generate the ast from a code string and an id. Works with SFC and non-SFC files.
33
+ */
34
+ function getCodeAst ( code : string , id : string ) {
35
+ let offset = 0
36
+ let ast : Program | undefined
37
+ const lang = getLang ( id . split ( MACRO_DEFINE_PAGE_QUERY ) [ 0 ] ! )
38
+ if ( lang === 'vue' ) {
39
+ const sfc = parseSFC ( code , id )
40
+ if ( sfc . scriptSetup ) {
41
+ ast = sfc . getSetupAst ( )
42
+ offset = sfc . scriptSetup . loc . start . offset
43
+ } else if ( sfc . script ) {
44
+ ast = sfc . getScriptAst ( )
45
+ offset = sfc . script . loc . start . offset
46
+ }
47
+ } else if ( / [ j t ] s x ? $ / . test ( lang ) ) {
48
+ ast = babelParse ( code , lang )
49
+ }
50
+
51
+ const definePageNodes : CallExpression [ ] = ( ast ?. body || [ ] )
52
+ . map ( ( node ) => {
53
+ const definePageCallNode =
54
+ node . type === 'ExpressionStatement' ? node . expression : node
55
+ return isCallOf ( definePageCallNode , MACRO_DEFINE_PAGE )
56
+ ? definePageCallNode
57
+ : null
58
+ } )
59
+ . filter ( ( node ) => ! ! node )
60
+
61
+ return { ast, offset, definePageNodes }
62
+ }
63
+
28
64
export function definePageTransform ( {
29
65
code,
30
66
id,
@@ -41,20 +77,8 @@ export function definePageTransform({
41
77
return isExtractingDefinePage ? 'export default {}' : undefined
42
78
}
43
79
44
- // TODO: handle also non SFC
45
-
46
- const sfc = parseSFC ( code , id )
47
- if ( ! sfc . scriptSetup ) return
48
-
49
- const { scriptSetup, getSetupAst } = sfc
50
- const setupAst = getSetupAst ( )
51
-
52
- const definePageNodes = ( setupAst ?. body || ( [ ] as Node [ ] ) )
53
- . map ( ( node ) => {
54
- if ( node . type === 'ExpressionStatement' ) node = node . expression
55
- return isCallOf ( node , MACRO_DEFINE_PAGE ) ? node : null
56
- } )
57
- . filter ( ( node ) : node is CallExpression => ! ! node )
80
+ const { ast, offset, definePageNodes } = getCodeAst ( code , id )
81
+ if ( ! ast ) return
58
82
59
83
if ( ! definePageNodes . length ) {
60
84
return isExtractingDefinePage
@@ -67,7 +91,6 @@ export function definePageTransform({
67
91
}
68
92
69
93
const definePageNode = definePageNodes [ 0 ] !
70
- const setupOffset = scriptSetup . loc . start . offset
71
94
72
95
// we only want the page info
73
96
if ( isExtractingDefinePage ) {
@@ -82,13 +105,13 @@ export function definePageTransform({
82
105
)
83
106
}
84
107
85
- const scriptBindings = setupAst ? .body ? getIdentifiers ( setupAst . body ) : [ ]
108
+ const scriptBindings = ast . body ? getIdentifiers ( ast . body ) : [ ]
86
109
87
110
// this will throw if a property from the script setup is used in definePage
88
111
checkInvalidScopeReference ( routeRecord , MACRO_DEFINE_PAGE , scriptBindings )
89
112
90
- s . remove ( setupOffset + routeRecord . end ! , code . length )
91
- s . remove ( 0 , setupOffset + routeRecord . start ! )
113
+ s . remove ( offset + routeRecord . end ! , code . length )
114
+ s . remove ( 0 , offset + routeRecord . start ! )
92
115
s . prepend ( `export default ` )
93
116
94
117
// find all static imports and filter out the ones that are not used
@@ -156,11 +179,8 @@ export function definePageTransform({
156
179
157
180
const s = new MagicString ( code )
158
181
159
- // s.removeNode(definePageNode, { offset: setupOffset })
160
- s . remove (
161
- setupOffset + definePageNode . start ! ,
162
- setupOffset + definePageNode . end !
163
- )
182
+ // s.removeNode(definePageNode, { offset })
183
+ s . remove ( offset + definePageNode . start ! , offset + definePageNode . end ! )
164
184
165
185
return generateTransform ( s , id )
166
186
}
@@ -172,19 +192,8 @@ export function extractDefinePageNameAndPath(
172
192
) : { name ?: string ; path ?: string } | null | undefined {
173
193
if ( ! sfcCode . includes ( MACRO_DEFINE_PAGE ) ) return
174
194
175
- const sfc = parseSFC ( sfcCode , id )
176
-
177
- if ( ! sfc . scriptSetup ) return
178
-
179
- const { getSetupAst } = sfc
180
- const setupAst = getSetupAst ( )
181
-
182
- const definePageNodes = ( setupAst ?. body ?? ( [ ] as Node [ ] ) )
183
- . map ( ( node ) => {
184
- if ( node . type === 'ExpressionStatement' ) node = node . expression
185
- return isCallOf ( node , MACRO_DEFINE_PAGE ) ? node : null
186
- } )
187
- . filter ( ( node ) : node is CallExpression => ! ! node )
195
+ const { ast, definePageNodes } = getCodeAst ( sfcCode , id )
196
+ if ( ! ast ) return
188
197
189
198
if ( ! definePageNodes . length ) {
190
199
return
0 commit comments