/
definePage.ts
207 lines (174 loc) · 5.47 KB
/
definePage.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
getTransformResult,
isCallOf,
parseSFC,
MagicString,
checkInvalidScopeReference,
} from '@vue-macros/common'
import { Thenable, TransformResult } from 'unplugin'
import type {
CallExpression,
Node,
ObjectProperty,
Statement,
StringLiteral,
} from '@babel/types'
import { walkAST } from 'ast-walker-scope'
import { CustomRouteBlock } from './customBlock'
const MACRO_DEFINE_PAGE = 'definePage'
function isStringLiteral(node: Node | null | undefined): node is StringLiteral {
return node?.type === 'StringLiteral'
}
export function definePageTransform({
code,
id,
}: {
code: string
id: string
}): Thenable<TransformResult> {
if (!code.includes(MACRO_DEFINE_PAGE)) return
const sfc = parseSFC(code, id)
if (!sfc.scriptSetup) return
// are we extracting only the definePage object
const isExtractingDefinePage = id.includes(MACRO_DEFINE_PAGE)
const { script, scriptSetup, scriptCompiled } = sfc
const definePageNodes = (scriptCompiled?.scriptSetupAst || ([] as Node[]))
.map((node) => {
if (node.type === 'ExpressionStatement') node = node.expression
return isCallOf(node, MACRO_DEFINE_PAGE) ? node : null
})
.filter((node): node is CallExpression => !!node)
if (!definePageNodes.length) {
return isExtractingDefinePage
? // e.g. index.vue?definePage that contains a commented `definePage()
'export default {}'
: // e.g. index.vue that contains a commented `definePage()
null
} else if (definePageNodes.length > 1) {
throw new SyntaxError(`duplicate definePage() call`)
}
const definePageNode = definePageNodes[0]
const setupOffset = scriptSetup.loc.start.offset
// we only want the page info
if (isExtractingDefinePage) {
const s = new MagicString(code)
// remove everything except the page info
const routeRecord = definePageNode.arguments[0]
const scriptBindings = sfc.scriptCompiled.scriptSetupAst
? getIdentifiers(sfc.scriptCompiled.scriptSetupAst as any)
: []
checkInvalidScopeReference(routeRecord, MACRO_DEFINE_PAGE, scriptBindings)
// NOTE: this doesn't seem to be any faster than using MagicString
// return (
// 'export default ' +
// code.slice(
// setupOffset + routeRecord.start!,
// setupOffset + routeRecord.end!
// )
// )
s.remove(setupOffset + routeRecord.end!, code.length)
s.remove(0, setupOffset + routeRecord.start!)
s.prepend(`export default `)
return getTransformResult(s, id)
} else {
// console.log('!!!', definePageNode)
const s = new MagicString(code)
// s.removeNode(definePageNode, { offset: setupOffset })
s.remove(
setupOffset + definePageNode.start!,
setupOffset + definePageNode.end!
)
return getTransformResult(s, id)
}
}
export function extractDefinePageNameAndPath(
sfcCode: string,
id: string
): { name?: string; path?: string } | null | undefined {
if (!sfcCode.includes(MACRO_DEFINE_PAGE)) return
const sfc = parseSFC(sfcCode, id)
if (!sfc.scriptSetup) return
const { script, scriptSetup, scriptCompiled } = sfc
const definePageNodes = (scriptCompiled?.scriptSetupAst ?? ([] as Node[]))
.map((node) => {
if (node.type === 'ExpressionStatement') node = node.expression
return isCallOf(node, MACRO_DEFINE_PAGE) ? node : null
})
.filter((node): node is CallExpression => !!node)
if (!definePageNodes.length) {
return
} else if (definePageNodes.length > 1) {
throw new SyntaxError(`duplicate definePage() call`)
}
const definePageNode = definePageNodes[0]
const routeRecord = definePageNode.arguments[0]
if (routeRecord.type !== 'ObjectExpression') {
throw new SyntaxError(
`[${id}]: definePage() expects an object expression as its only argument`
)
}
const routeInfo: Pick<CustomRouteBlock, 'name' | 'path'> = {}
for (const prop of routeRecord.properties) {
if (prop.type === 'ObjectProperty' && prop.key.type === 'Identifier') {
if (prop.key.name === 'name') {
if (prop.value.type !== 'StringLiteral') {
console.warn(
`[unplugin-vue-router]: route name must be a string literal. Found in "${id}".`
)
} else {
routeInfo.name = prop.value.value
}
} else if (prop.key.name === 'path') {
if (prop.value.type !== 'StringLiteral') {
console.warn(
`[unplugin-vue-router]: route path must be a string literal. Found in "${id}".`
)
} else {
routeInfo.path = prop.value.value
}
}
}
}
return routeInfo
}
function extractRouteAlias(
aliasValue: ObjectProperty['value'],
id: string
): string[] | undefined {
if (
aliasValue.type !== 'StringLiteral' &&
aliasValue.type !== 'ArrayExpression'
) {
console.warn(
`[unplugin-vue-router]: route alias must be a string literal. Found in "${id}".`
)
} else {
return aliasValue.type === 'StringLiteral'
? [aliasValue.value]
: aliasValue.elements.filter(isStringLiteral).map((el) => el.value)
}
}
const getIdentifiers = (stmts: Statement[]) => {
let ids: string[] = []
walkAST(
{
type: 'Program',
body: stmts,
directives: [],
sourceType: 'module',
sourceFile: '',
},
{
enter(node) {
if (node.type === 'BlockStatement') {
this.skip()
}
},
leave(node) {
if (node.type !== 'Program') return
ids = Object.keys(this.scope)
},
}
)
return ids
}