Skip to content

Commit 264c7be

Browse files
committed
feat(volar): introduce jsx-element
1 parent 56f025f commit 264c7be

File tree

12 files changed

+1570
-564
lines changed

12 files changed

+1570
-564
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"scripts": {
2828
"dev": "pnpm run --filter=\"./packages/*\" --parallel dev",
29-
"build": "pnpm run --filter=\"./packages/*\" build",
29+
"build": "pnpm run --filter=\"./packages/*\" --parallel build",
3030
"typecheck": "tsmc --noEmit",
3131
"lint": "eslint .",
3232
"play": "npm -C playground run dev",

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,27 @@ const __setup = `,
2727
? ['\nreturn __setup']
2828
: ([
2929
`
30-
type __Props = Parameters<typeof __setup>[0]
31-
type __Slots = Parameters<typeof __setup>[1] extends { slots?: infer Slots } | undefined ? Slots : {}
32-
type __Exposed = Parameters<typeof __setup>[1] extends { expose?: (exposed: infer Exposed) => any } | undefined ? Exposed : {}`,
30+
type __Setup = typeof __setup
31+
type __Props = Parameters<__Setup>[0]
32+
type __Slots = Parameters<__Setup>[1] extends { slots?: infer Slots } | undefined ? Slots : {}
33+
type __Exposed = Parameters<__Setup>[1] extends { expose?: (exposed: infer Exposed) => any } | undefined ? Exposed : {}`,
3334
'\n const __component = ',
34-
[node.expression.getText(ast), node.expression.getStart(ast)],
35-
`({`,
3635
isVapor
37-
? ''
38-
: `...{} as {
39-
setup: () => __Exposed,
40-
slots: import('vue').SlotsType<__Slots>
36+
? `// @ts-ignore\n(defineVaporComponent,await import('vue-jsx-vapor')).`
37+
: '',
38+
[node.expression.getText(ast), node.expression.getStart(ast)],
39+
`({\n`,
40+
`...{} as {
41+
setup: (props: __Props) => __Exposed,
42+
render: () => ReturnType<__Setup>
43+
slots: ${isVapor ? '__Slots' : `import('vue').SlotsType<__Slots>`}
4144
},`,
4245
...(compOptions
4346
? ['...', [compOptions.getText(ast), compOptions.getStart(ast)]]
4447
: []),
4548
`})
46-
type __Instance = (typeof __component extends new (...args: any) => any ? InstanceType<typeof __component> : typeof __component) & {${isVapor ? '\n/** @deprecated This is only a type when used in Vapor Instances. */' : ''}
47-
$props: __Props
48-
}
49-
return {} as {
50-
__isFragment?: never
51-
__isTeleport?: never
52-
__isSuspense?: never
53-
new (props: __Props): __Instance,
54-
setup: (props: __Props, ctx?: {
55-
attrs?: Record<string, any>
56-
slots?: __Slots,
57-
expose?: (exposed: keyof __Exposed extends never ? __Instance : __Exposed) => any
58-
}) => {},
49+
return {} as Omit<typeof __component, 'constructor'> & {
50+
new (props?: __Props): InstanceType<typeof __component> & {${isVapor ? '\n/** @deprecated This is only a type when used in Vapor Instances. */\n$props: __Props' : ''}},
5951
}`,
6052
] as Code[])
6153
codes.replaceRange(node.end, node.end, ...result, `\n})()`)

packages/macros/src/volar/transform.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,27 @@ export function transformJsxMacros(
8181
node.getStart(ast),
8282
node.expression.getStart(ast),
8383
'const ',
84-
[`__render`, node.getStart(ast), { verification: true }],
84+
[`__render`, node.getStart(ast) - 1, { verification: true }],
8585
macros.defineComponent
8686
? macros.defineComponent?.expression.getText(ast) ===
8787
'defineVaporComponent'
88-
? ': import("vue-jsx-vapor").NodeChild'
89-
: ': () => import("vue").VNodeChild'
88+
? ''
89+
: ': () => JSX.Element'
9090
: '',
91-
` = `,
91+
' = ',
9292
)
9393
codes.replaceRange(
9494
node.expression.end,
9595
node.expression.end,
9696
`
9797
return {} as {
9898
props: {${props.join(', ')}},
99-
context: {
100-
slots?: ${macros.defineSlots ?? '{}'},
101-
expose?: (exposed: import('vue').ShallowUnwrapRef<${macros.defineExpose ?? '{}'}>) => void,
102-
attrs?: Record<string, any>
99+
context: ${
100+
root.parameters[1]?.type ? root.parameters[1].type.getText(ast) : '{}'
101+
} & {
102+
slots: ${macros.defineSlots ?? '{}'},
103+
expose: (exposed: ${macros.defineExpose ?? '{}'}) => void,
104+
attrs: Record<string, any>
103105
},
104106
render: typeof __render
105107
}`,

packages/runtime/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './node'
55
export * from './block'
66
export * from './component'
77
export * from './helpers'
8+
export * from './vue'

0 commit comments

Comments
 (0)