Skip to content

Commit 0106c80

Browse files
committed
feat(macros): support vue sfc
1 parent 1800b44 commit 0106c80

File tree

10 files changed

+382
-292
lines changed

10 files changed

+382
-292
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353
},
5454
"pnpm": {
5555
"overrides": {
56-
"estree-walker": "2.0.2",
57-
"ts-macro": "catalog:"
56+
"estree-walker": "2.0.2"
5857
}
5958
}
6059
}

packages/macros/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
"@vue-macros/common": "catalog:",
139139
"@vue/compiler-sfc": "catalog:",
140140
"hash-sum": "catalog:",
141-
"ts-macro": "catalog:",
141+
"ts-macro": "^0.2.7",
142142
"unplugin": "catalog:"
143143
},
144144
"devDependencies": {

packages/macros/src/volar.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { createFilter } from '@vue-macros/common'
1+
import { createFilter, REGEX_VUE_SFC } from '@vue-macros/common'
22
import { createPlugin, type PluginReturn } from 'ts-macro'
33
import { resolveOptions, type Options } from './options'
44
import { getGlobalTypes, getRootMap, transformJsxMacros } from './volar/index'
55

66
const plugin: PluginReturn<Options | undefined> = createPlugin(
77
({ ts }, userOptions = {}) => {
88
const resolvedOptions = resolveOptions(userOptions!)
9+
;(resolvedOptions.include as any[]).push(REGEX_VUE_SFC)
910
const filter = createFilter(resolvedOptions)
1011

1112
return {
@@ -29,3 +30,4 @@ const plugin: PluginReturn<Options | undefined> = createPlugin(
2930
},
3031
)
3132
export default plugin
33+
export { plugin as 'module.exports' }

packages/macros/src/volar/define-component.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { allCodeFeatures, replaceRange } from 'ts-macro'
21
import type { TransformOptions } from '.'
32

43
export function transformDefineComponent(
@@ -8,23 +7,18 @@ export function transformDefineComponent(
87
): void {
98
const { codes, ast, ts } = options
109

11-
replaceRange(codes, node.arguments[0].end, node.end - 1)
10+
codes.replaceRange(node.arguments[0].end, node.end - 1)
1211

1312
const componentOptions = node.arguments[1]
14-
replaceRange(
15-
codes,
13+
codes.replaceRange(
1614
node.getStart(ast),
1715
node.expression.end + 1,
1816
ts.isExpressionStatement(parent) ? ';' : '',
1917
'(',
20-
[node.expression.getText(ast), node.getStart(ast), allCodeFeatures],
21-
'(() => ({}) as any, ',
18+
[node.expression.getText(ast), node.getStart(ast)],
19+
'(() => ({}) as any,',
2220
componentOptions
23-
? [
24-
componentOptions.getText(ast),
25-
componentOptions.getStart(ast),
26-
allCodeFeatures,
27-
]
21+
? [componentOptions.getText(ast), componentOptions.getStart(ast)]
2822
: '',
2923
'), ',
3024
)

packages/macros/src/volar/define-style.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { allCodeFeatures, replaceRange, type Code } from 'ts-macro'
1+
import { allCodeFeatures, type Code } from 'ts-macro'
22
import type { DefineStyle, TransformOptions } from '.'
33

44
export function transformDefineStyle(
@@ -13,8 +13,7 @@ export function transformDefineStyle(
1313
!expression.typeArguments &&
1414
ts.isTemplateLiteral(expression.arguments[0])
1515
) {
16-
replaceRange(
17-
codes,
16+
codes.replaceRange(
1817
expression.arguments.pos - 1,
1918
expression.arguments.pos - 1,
2019
`<{`,

packages/macros/src/volar/index.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HELPER_PREFIX, type Overwrite } from '@vue-macros/common'
2-
import { replaceRange, type TsmVirtualCode } from 'ts-macro'
2+
import { getText, type TsmVirtualCode } from 'ts-macro'
33
import type { OptionsResolved } from '../options'
44
import { transformDefineComponent } from './define-component'
55

@@ -40,7 +40,7 @@ function getMacro(
4040
if (!node) return
4141

4242
if (ts.isVariableStatement(node)) {
43-
return ts.forEachChild(node.declarationList, (decl) => getExpression(decl))
43+
return node.declarationList.forEachChild((decl) => getExpression(decl))
4444
} else {
4545
return getExpression(node)
4646
}
@@ -164,16 +164,14 @@ export function getRootMap(options: TransformOptions): RootMap {
164164
}
165165

166166
if (!hasRequired && isRequired) {
167-
replaceRange(
168-
codes,
167+
codes.replaceRange(
169168
modelOptions.end - 1,
170169
modelOptions.end - 1,
171170
`${!modelOptions.properties.hasTrailingComma && modelOptions.properties.length ? ',' : ''} required: true`,
172171
)
173172
}
174173
} else if (isRequired) {
175-
replaceRange(
176-
codes,
174+
codes.replaceRange(
177175
expression.arguments.end,
178176
expression.arguments.end,
179177
`${!expression.arguments.hasTrailingComma && expression.arguments.length ? ',' : ''} { required: true }`,
@@ -189,28 +187,25 @@ export function getRootMap(options: TransformOptions): RootMap {
189187
)
190188
if (expression.typeArguments?.[1]) {
191189
defineModel.push(
192-
`${modelName}Modifiers?: Partial<Record<${expression.typeArguments[1].getText(ast)}, boolean>>`,
190+
`${modelName}Modifiers?: Partial<Record<${getText(expression.typeArguments[1], ast, ts)}, boolean>>`,
193191
)
194192
}
195193
if (ts.isVariableStatement(node))
196-
replaceRange(
197-
codes,
194+
codes.replaceRange(
198195
initializer.getStart(ast),
199196
initializer.getStart(ast),
200197
`// @ts-ignore\n${id};\nlet ${id} = `,
201198
)
202199
} else if (options.defineSlots.alias.includes(macroName)) {
203-
replaceRange(
204-
codes,
200+
codes.replaceRange(
205201
expression.getStart(ast),
206202
expression.getStart(ast),
207203
`// @ts-ignore\n${HELPER_PREFIX}slots;\nconst ${HELPER_PREFIX}slots = `,
208204
)
209205
rootMap.get(root)!.defineSlots =
210206
`Partial<typeof ${HELPER_PREFIX}slots>`
211207
} else if (options.defineExpose.alias.includes(macroName)) {
212-
replaceRange(
213-
codes,
208+
codes.replaceRange(
214209
expression.getStart(ast),
215210
expression.getStart(ast),
216211
`// @ts-ignore\n${HELPER_PREFIX}exposed;\nconst ${HELPER_PREFIX}exposed = `,
@@ -220,14 +215,14 @@ export function getRootMap(options: TransformOptions): RootMap {
220215
}
221216
}
222217

223-
ts.forEachChild(node, (child) => {
218+
node.forEachChild((child) => {
224219
parents.unshift(node)
225220
walk(child, parents)
226221
parents.shift()
227222
})
228223
}
229224

230-
ts.forEachChild(ast, (node) => walk(node, []))
225+
ast.forEachChild((node) => walk(node, []))
231226
return rootMap
232227
}
233228

packages/macros/src/volar/transform.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { HELPER_PREFIX } from '@vue-macros/common'
2-
import { replaceRange } from 'ts-macro'
32
import { transformDefineStyle } from './define-style'
43
import type { RootMap, TransformOptions } from '.'
54

@@ -21,14 +20,13 @@ export function transformJsxMacros(
2120
(modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword,
2221
)
2322
if (asyncModifier && macros.defineComponent)
24-
replaceRange(codes, asyncModifier.pos, asyncModifier.end)
23+
codes.replaceRange(asyncModifier.pos, asyncModifier.end)
2524
const result = `({}) as __VLS_PickNotAny<typeof ${HELPER_PREFIX}ctx.render, {}> & { __ctx: typeof ${HELPER_PREFIX}ctx }`
2625

2726
const propsType = root.parameters[0]?.type
28-
? String(root.parameters[0].type.getText(ast))
27+
? root.parameters[0].type.getText(ast)
2928
: '{}'
30-
replaceRange(
31-
codes,
29+
codes.replaceRange(
3230
root.parameters.pos,
3331
root.parameters.pos,
3432
ts.isArrowFunction(root) && root.parameters.pos === root.pos ? '(' : '',
@@ -40,24 +38,18 @@ export function transformJsxMacros(
4038
`${HELPER_PREFIX}setup = (${asyncModifier ? 'async' : ''}(`,
4139
)
4240
if (ts.isArrowFunction(root)) {
43-
replaceRange(
44-
codes,
41+
codes.replaceRange(
4542
root.end,
4643
root.end,
4744
`))${root.pos === root.parameters.pos ? ')' : ''} => `,
4845
result,
4946
)
5047
} else {
51-
replaceRange(
52-
codes,
53-
root.body.getStart(ast),
54-
root.body.getStart(ast),
55-
'=>',
56-
)
57-
replaceRange(codes, root.end, root.end, `)){ return `, result, '}')
48+
codes.replaceRange(root.body.getStart(ast), root.body.getStart(ast), '=>')
49+
codes.replaceRange(root.end, root.end, `)){ return `, result, '}')
5850
}
5951

60-
ts.forEachChild(root.body, (node) => {
52+
root.body.forEachChild((node) => {
6153
if (ts.isReturnStatement(node) && node.expression) {
6254
const props = [...(macros.defineModel ?? [])]
6355
const elements =
@@ -68,12 +60,11 @@ export function transformJsxMacros(
6860
: []
6961
for (const element of elements) {
7062
if (ts.isIdentifier(element.name)) {
71-
const isRequired = ts.forEachChild(
72-
element,
63+
const isRequired = element.forEachChild(
7364
function isNonNullExpression(node): boolean {
7465
return (
7566
ts.isNonNullExpression(node) ||
76-
!!ts.forEachChild(node, isNonNullExpression)
67+
!!node.forEachChild(isNonNullExpression)
7768
)
7869
},
7970
)
@@ -89,15 +80,13 @@ export function transformJsxMacros(
8980
(ts.isArrowFunction(node.expression) ||
9081
ts.isFunctionExpression(node.expression)) &&
9182
macros.defineComponent
92-
replaceRange(
93-
codes,
83+
codes.replaceRange(
9484
node.getStart(ast),
9585
node.expression.getStart(ast),
9686
`const ${HELPER_PREFIX}render = `,
9787
shouldWrapByCall ? '(' : '',
9888
)
99-
replaceRange(
100-
codes,
89+
codes.replaceRange(
10190
node.expression.end,
10291
node.expression.end,
10392
shouldWrapByCall ? ')()' : '',

packages/vue-jsx-vapor/src/volar.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createPlugin, type PluginReturn } from 'ts-macro'
55
import type { Options } from './options'
66

77
const plugin: PluginReturn<Options | undefined, true> = createPlugin(
8-
(ctx, options) => {
8+
(ctx, options = ctx.vueCompilerOptions['vue-jsx-vapor']) => {
99
return [
1010
jsxDirective()(ctx),
1111
options?.ref === false
@@ -21,3 +21,4 @@ const plugin: PluginReturn<Options | undefined, true> = createPlugin(
2121
)
2222

2323
export default plugin
24+
export { plugin as 'module.exports' }

0 commit comments

Comments
 (0)