Skip to content

Commit 0e44f13

Browse files
committed
feat(volar): auto infer type for useRef
1 parent 543a426 commit 0e44f13

File tree

14 files changed

+111
-126
lines changed

14 files changed

+111
-126
lines changed

packages/unplugin/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"rollup",
1212
"transform",
1313
"vue-jsx",
14-
"volar"
14+
"volar",
15+
"vapor"
1516
],
1617
"license": "MIT",
1718
"homepage": "https://github.com/unplugin/unplugin-vue-jsx-vapor#readme",
@@ -25,6 +26,9 @@
2526
"files": [
2627
"dist"
2728
],
29+
"main": "dist/index.cjs",
30+
"module": "dist/index.js",
31+
"types": "dist/index.d.ts",
2832
"exports": {
2933
".": {
3034
"dev": "./src/index.ts",
@@ -182,6 +186,7 @@
182186
"@vue-jsx-vapor/compiler": "workspace:*",
183187
"@vue-macros/volar": "3.0.0-beta.4",
184188
"magic-string-stack": "^0.1.1",
189+
"ts-macro": "0.1.19",
185190
"unplugin": "^1.11.0",
186191
"unplugin-utils": "^0.2.4"
187192
},
@@ -195,6 +200,7 @@
195200
"unplugin-raw": "^0.3.1",
196201
"vite": "^6.0.0",
197202
"vitest": "^1.6.0",
203+
"vue": "catalog:",
198204
"webpack": "^5.92.1"
199205
}
200206
}

packages/unplugin/src/astro.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unplugin from '.'
1+
import unplugin from './core/unplugin'
22
import type { Options } from './types'
33

44
export default (options: Options) => ({
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { type UnpluginFactory, createUnplugin } from 'unplugin'
2+
import { createFilter, transformWithEsbuild } from 'vite'
3+
import { plugin } from './plugin'
4+
import type { Options } from '../types'
5+
6+
export const unpluginFactory: UnpluginFactory<Options | undefined> = (
7+
options = {},
8+
meta,
9+
) => {
10+
return [
11+
plugin(options, meta),
12+
options.interop
13+
? { name: 'interop' }
14+
: {
15+
name: 'unplugin-esbuild',
16+
transformInclude: createFilter(
17+
options?.include || /\.[jt]sx$/,
18+
options?.exclude,
19+
),
20+
transform(code, id) {
21+
return transformWithEsbuild(code, id, {
22+
target: 'esnext',
23+
charset: 'utf8',
24+
minify: false,
25+
minifyIdentifiers: false,
26+
minifySyntax: false,
27+
minifyWhitespace: false,
28+
treeShaking: false,
29+
keepNames: false,
30+
supported: {
31+
'dynamic-import': true,
32+
'import-meta': true,
33+
},
34+
})
35+
},
36+
},
37+
]
38+
}
39+
40+
export const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)
41+
42+
export default unplugin

packages/unplugin/src/esbuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { createEsbuildPlugin } from 'unplugin'
2-
import { unpluginFactory } from '.'
2+
import { unpluginFactory } from './core/unplugin'
33

44
export default createEsbuildPlugin(unpluginFactory)

packages/unplugin/src/index.ts

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1 @@
1-
import { type UnpluginFactory, createUnplugin } from 'unplugin'
2-
import { createFilter, transformWithEsbuild } from 'vite'
3-
import { plugin } from './core/plugin'
4-
import type { Options } from './types'
5-
6-
export const unpluginFactory: UnpluginFactory<Options | undefined> = (
7-
options = {},
8-
meta,
9-
) => {
10-
return [
11-
plugin(options, meta),
12-
options.interop
13-
? { name: 'interop' }
14-
: {
15-
name: 'unplugin-esbuild',
16-
transformInclude: createFilter(
17-
options?.include || /\.[jt]sx$/,
18-
options?.exclude,
19-
),
20-
transform(code, id) {
21-
return transformWithEsbuild(code, id, {
22-
target: 'esnext',
23-
charset: 'utf8',
24-
minify: false,
25-
minifyIdentifiers: false,
26-
minifySyntax: false,
27-
minifyWhitespace: false,
28-
treeShaking: false,
29-
keepNames: false,
30-
supported: {
31-
'dynamic-import': true,
32-
'import-meta': true,
33-
},
34-
})
35-
},
36-
},
37-
]
38-
}
39-
40-
export const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)
41-
42-
export default unplugin
1+
export { shallowRef as useRef } from 'vue'

packages/unplugin/src/rollup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { createRollupPlugin } from 'unplugin'
2-
import { unpluginFactory } from '.'
2+
import { unpluginFactory } from './core/unplugin'
33

44
export default createRollupPlugin(unpluginFactory)

packages/unplugin/src/rspack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { createRspackPlugin } from 'unplugin'
2-
import { unpluginFactory } from '.'
2+
import { unpluginFactory } from './core/unplugin'
33

44
export default createRspackPlugin(unpluginFactory)

packages/unplugin/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ export interface Options {
77
exclude?: FilterPattern
88
interop?: boolean
99
compile?: CompilerOptions
10+
ref?:
11+
| {
12+
alias?: string[]
13+
}
14+
| boolean
1015
}

packages/unplugin/src/vite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { createVitePlugin } from 'unplugin'
2-
import { unpluginFactory } from '.'
2+
import { unpluginFactory } from './core/unplugin'
33

44
export default createVitePlugin(unpluginFactory)

packages/unplugin/src/volar.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
1-
import plugin from '@vue-macros/volar/jsx-directive'
1+
import jsxDirective from '@vue-macros/volar/jsx-directive'
2+
import jsxRef from '@vue-macros/volar/jsx-ref'
3+
import { type PluginReturn, createPlugin } from 'ts-macro'
4+
import type { Options } from './types'
5+
6+
const plugin: PluginReturn<Options | undefined, true> = createPlugin(
7+
(ctx, options) => {
8+
return [
9+
jsxDirective()(ctx),
10+
options?.ref === false
11+
? []
12+
: jsxRef(options?.ref === true ? undefined : options?.ref)(ctx),
13+
].flat()
14+
},
15+
)
16+
217
export default plugin

packages/unplugin/src/webpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { createWebpackPlugin } from 'unplugin'
2-
import { unpluginFactory } from '.'
2+
import { unpluginFactory } from './core/unplugin'
33

44
export default createWebpackPlugin(unpluginFactory)

packages/unplugin/tsup.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default {
99
watch: !!process.env.DEV,
1010
dts: !process.env.DEV,
1111
cjsInterop: true,
12+
external: ['vue'],
1213
onSuccess: 'npm run build:fix',
1314
define: {
1415
__DEV__: 'true',

playground/src/App.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type Ref, ref } from 'vue'
2+
import { useRef } from 'unplugin-vue-jsx-vapor'
23
import Count2 from './count'
34
import If from './if'
45
import For from './for'
@@ -19,11 +20,14 @@ export default () => {
1920
return <div>{value.value}</div>
2021
}
2122

23+
const inputRef = useRef()
24+
2225
return (
2326
<>
2427
<fieldset>
2528
<legend>Component</legend>
2629
<input
30+
ref={inputRef}
2731
value_prop={count.value}
2832
onInput={(e) => (count.value = e.target.value)}
2933
/>

0 commit comments

Comments
 (0)