Skip to content

Commit 8a79bdb

Browse files
committed
fix(macros): correct types for defineComponent and defineVaporComponent
1 parent 1f898c5 commit 8a79bdb

File tree

18 files changed

+244
-302
lines changed

18 files changed

+244
-302
lines changed

docs/features/macros.md

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,46 @@ export default {
4141

4242
:::
4343

44+
::: details Install as a standalone plugin
45+
46+
We have also released a standalone plugin that can be used in virtual DOM projects.
47+
48+
```bash
49+
pnpm add @vue-jsx-vapor/macros -D
50+
```
51+
52+
配置:
53+
54+
```ts
55+
// vite.config.ts
56+
import jsxMacros from '@vue-jsx-vapor/macros/vite'
57+
58+
export default {
59+
plugins: [
60+
jsxMacros()
61+
]
62+
}
63+
```
64+
65+
:::
66+
4467
## defineComponent
4568

69+
### Options
70+
71+
```ts
72+
VueJsxVapor({
73+
defineComponent: {
74+
/**
75+
* @default ['defineComponent','defineVaporComponent']
76+
*
77+
* You can set alias to an empty array to disable defineComponent macro.
78+
*/
79+
alias: []
80+
}
81+
})
82+
```
83+
4684
- Support `await` keyword.
4785
- Automatically collects used props to the defineComponent's props option.
4886

@@ -57,7 +95,7 @@ const Comp = defineComponent(
5795
}) => {
5896
await nextTick()
5997
const attrs = useAttrs()
60-
return (
98+
return () => (
6199
<div>
62100
<span {...attrs}>{props.foo}</span>
63101
</div>
@@ -101,9 +139,9 @@ defineComponent(
101139

102140
```tsx twoslash
103141
// @errors: 2322
104-
import { defineComponent } from 'vue'
142+
import { defineVaporComponent } from 'vue'
105143

106-
const Comp = defineComponent(
144+
const Comp = defineVaporComponent(
107145
<T,>({ foo = undefined as T, bar = ''!, ...attrs }) => {
108146
return (
109147
<div>
@@ -119,9 +157,9 @@ export default () => <Comp<string> foo={1} bar="bar" />
119157
::: details Compiled Code
120158

121159
```tsx
122-
import { defineComponent } from 'vue'
160+
import { defineVaporComponent } from 'vue'
123161
import { createPropsDefaultProxy } from '/vue-macros/jsx-macros/with-defaults'
124-
defineComponent(
162+
defineVaporComponent(
125163
(_props) => {
126164
const props = createPropsDefaultProxy(_props, { bar: '' })
127165
const attrs = useAttrs()

docs/features/use-ref.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Automatically infer type for `useRef`. It's an alias of `shallowRef`.
55
## Basic Usage
66

77
```tsx twoslash
8-
import { defineComponent } from 'vue'
8+
import { defineVaporComponent } from 'vue'
99
import { useRef } from 'vue-jsx-vapor'
1010
// or
1111
// import { shallowRef as useRef } from 'vue'
@@ -18,7 +18,7 @@ export const Comp = () => {
1818
return <div />
1919
}
2020

21-
export default defineComponent(() => {
21+
export default defineVaporComponent(() => {
2222
const comp = useRef()
2323
comp.value?.foo
2424
// ^?

docs/introduction/migration.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ export default () => {
5757
- Usage
5858

5959
```tsx
60-
import { defineComponent, ref } from 'vue'
60+
import { defineVaporComponent, ref } from 'vue'
6161
62-
const Comp = defineComponent(({ foo }) => {
62+
const Comp = defineVaporComponent(({ foo }) => {
6363
return <>{foo}</>
6464
})
6565
// Will be convert to:
66-
const Comp = defineComponent((_props) => {
66+
const Comp = defineVaporComponent((_props) => {
6767
return <>{_props.foo}</>
6868
}, { props: ['foo'] })
6969
@@ -119,9 +119,9 @@ const double = computed(() => foo * 2)
119119
console.log({ double: double.value }, [double.value])
120120
```
121121

122-
### defineComponent
122+
### defineVaporComponent
123123

124-
Use `defineComponent` macro to support destructuring props.
124+
Use `defineVaporComponent` macro to support destructuring props.
125125

126126
```tsx
127127
// before
@@ -130,7 +130,7 @@ const Comp = ({ count = 1 }) => {
130130
}
131131
132132
// after
133-
const Comp = defineComponent(({ count = 1 }) => {
133+
const Comp = defineVaporComponent(({ count = 1 }) => {
134134
return <div>{count}</div>
135135
})
136136
```

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"devDependencies": {
1616
"@shikijs/vitepress-twoslash": "^3.8.1",
17-
"@ts-macro/twoslash": "^0.0.4",
17+
"@ts-macro/twoslash": "^0.0.5",
1818
"vitepress": "2.0.0-alpha.5"
1919
}
2020
}

docs/zh/features/macros.md

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,47 @@ export default {
4141

4242
:::
4343

44+
45+
::: details 作为单独的插件安装
46+
47+
我们也发布了一个单独的插件,可以给 虚拟DOM 的项目使用。
48+
49+
```bash
50+
pnpm add @vue-jsx-vapor/macros -D
51+
```
52+
53+
配置:
54+
55+
```ts
56+
// vite.config.ts
57+
import jsxMacros from '@vue-jsx-vapor/macros/vite'
58+
59+
export default {
60+
plugins: [
61+
jsxMacros()
62+
]
63+
}
64+
```
65+
66+
:::
67+
4468
## defineComponent
4569

70+
### 选项
71+
72+
```ts
73+
VueJsxVapor({
74+
defineComponent: {
75+
/**
76+
* @default ['defineComponent','defineVaporComponent']
77+
*
78+
* 可以通过把 alias 设为空数组来禁用 defineComponent 宏.
79+
*/
80+
alias: []
81+
}
82+
})
83+
```
84+
4685
- 支持 `await` 关键字。
4786
- 自动收集使用到的 props 到 `defineComponent``props` 选项中。
4887

@@ -57,7 +96,7 @@ const Comp = defineComponent(
5796
}) => {
5897
await nextTick()
5998
const attrs = useAttrs()
60-
return (
99+
return () => (
61100
<div>
62101
<span {...attrs}>{props.foo}</span>
63102
</div>
@@ -101,9 +140,9 @@ defineComponent(
101140

102141
```tsx twoslash
103142
// @errors: 2322
104-
import { defineComponent } from 'vue'
143+
import { defineVaporComponent } from 'vue'
105144

106-
const Comp = defineComponent(
145+
const Comp = defineVaporComponent(
107146
<T,>({ foo = undefined as T, bar = ''!, ...attrs }) => {
108147
return (
109148
<div>
@@ -119,9 +158,9 @@ export default () => <Comp<string> foo={1} bar="bar" />
119158
::: details 编译后代码
120159

121160
```tsx
122-
import { defineComponent } from 'vue'
161+
import { defineVaporComponent } from 'vue'
123162
import { createPropsDefaultProxy } from '/vue-macros/jsx-macros/with-defaults'
124-
defineComponent(
163+
defineVaporComponent(
125164
(_props) => {
126165
const props = createPropsDefaultProxy(_props, { bar: '' })
127166
const attrs = useAttrs()

docs/zh/features/use-ref.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## 基本用法
66

77
```tsx twoslash
8-
import { defineComponent } from 'vue'
8+
import { defineVaporComponent } from 'vue'
99
import { useRef } from 'vue-jsx-vapor'
1010
// 或者
1111
// import { shallowRef as useRef } from 'vue'
@@ -18,7 +18,7 @@ export const Comp = () => {
1818
return <div />
1919
}
2020

21-
export default defineComponent(() => {
21+
export default defineVaporComponent(() => {
2222
const comp = useRef()
2323
comp.value?.foo
2424
// ^?

docs/zh/introduction/migration.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ export default () => {
5757
- 用法
5858

5959
```tsx
60-
import { defineComponent, ref } from 'vue'
60+
import { defineVaporComponent, ref } from 'vue'
6161
62-
const Comp = defineComponent(({ foo }) => {
62+
const Comp = defineVaporComponent(({ foo }) => {
6363
return <>{foo}</>
6464
})
6565
// 将被转换为:
66-
const Comp = defineComponent((_props) => {
66+
const Comp = defineVaporComponent((_props) => {
6767
return <>{_props.foo}</>
6868
}, { props: ['foo'] })
6969
@@ -130,7 +130,7 @@ const Comp = ({ count = 1 }) => {
130130
}
131131
132132
// 转换后
133-
const Comp = defineComponent(({ count = 1 }) => {
133+
const Comp = defineVaporComponent(({ count = 1 }) => {
134134
return <div>{count}</div>
135135
})
136136
```

packages/macros/src/core/define-component/index.ts

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
import type { Macros } from '..'
1616
import { transformAwait } from './await'
1717
import { transformReturn } from './return'
18-
import type { Node, ObjectExpression } from '@babel/types'
18+
import type { Node } from '@babel/types'
1919

2020
export function transformDefineComponent(
2121
root: FunctionalNode,
@@ -118,31 +118,13 @@ export function transformDefineComponent(
118118
.map(([key, value]) => `'${key}': ${value}`)
119119
.join(', \n')
120120
if (propsString) {
121-
const argument = macros.defineComponent.arguments[1]
122-
if (!argument) {
123-
s.appendRight(
124-
root.end!,
125-
`, {${hasRestProp ? 'inheritAttrs: false,' : ''} props: {\n${propsString}\n} }`,
126-
)
127-
} else if (argument.type === 'ObjectExpression') {
128-
const resolvedPropsString = `{\n${propsString}\n}`
129-
const prop = prependObjectExpression(
130-
argument,
131-
'props',
132-
resolvedPropsString,
133-
s,
134-
)
135-
if (
136-
prop &&
137-
prop.type === 'ObjectProperty' &&
138-
prop.value.type === 'ObjectExpression'
139-
) {
140-
s.appendLeft(prop.value.start!, `{...${resolvedPropsString}, ...`)
141-
s.appendRight(prop.value.end!, '}')
142-
}
143-
if (hasRestProp) {
144-
prependObjectExpression(argument, 'inheritAttrs', 'false', s)
145-
}
121+
const resolvedPropsString = `${hasRestProp ? 'inheritAttrs: false, ' : ''}props: {\n${propsString}\n}`
122+
const compOptions = macros.defineComponent.arguments[1]
123+
if (!compOptions) {
124+
s.appendRight(root.end!, `, { ${resolvedPropsString} }`)
125+
} else if (compOptions.type === 'ObjectExpression') {
126+
s.appendLeft(compOptions.start!, `{ ${resolvedPropsString}, ...`)
127+
s.appendRight(compOptions.end!, ' }')
146128
}
147129
}
148130

@@ -152,24 +134,6 @@ export function transformDefineComponent(
152134
}
153135
}
154136

155-
function prependObjectExpression(
156-
argument: ObjectExpression,
157-
name: string,
158-
value: string,
159-
s: MagicStringAST,
160-
) {
161-
const prop = argument.properties?.find(
162-
(prop) =>
163-
prop.type === 'ObjectProperty' &&
164-
prop.key.type === 'Identifier' &&
165-
prop.key.name === name,
166-
)
167-
if (!prop) {
168-
s.appendRight(argument.start! + 1, `${name}: ${value},`)
169-
}
170-
return prop
171-
}
172-
173137
function getWalkedIds(root: FunctionalNode, propsName: string) {
174138
const walkedIds = new Set<string>()
175139
walkIdentifiers(root.body, (id, parent) => {

0 commit comments

Comments
 (0)