Skip to content

Commit 29dd39c

Browse files
committed
feat: support pinia
1 parent 79cc7bb commit 29dd39c

File tree

9 files changed

+130
-10
lines changed

9 files changed

+130
-10
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# unplugin-vue-reactivity-function [![npm](https://img.shields.io/npm/v/unplugin-vue-reactivity-function.svg)](https://npmjs.com/package/unplugin-vue-reactivity-function)
22

3-
[![Unit Test](https://github.com/zhiyuanzmj/unplugin-vue-reactivity-function/actions/workflows/unit-test.yml/badge.svg)](https://github.com/unplugin/unplugin-vue-reactivity-function/actions/workflows/unit-test.yml)
3+
[![Unit Test](https://github.com/zhiyuanzmj/unplugin-vue-reactivity-function/actions/workflows/unit-test.yml/badge.svg)](https://github.com/zhiyuanzmj/unplugin-vue-reactivity-function/actions/workflows/unit-test.yml)
44

5-
Named export for Vue SFC.
5+
Reactive Function.
66

77
## Installation
88

@@ -81,6 +81,34 @@ module.exports = {
8181

8282
## Usage
8383

84+
```ts
85+
// /store/user.ts
86+
export const useUserStore = $$defineStore('user', () => {
87+
let token = $ref('')
88+
function login() {
89+
token = 'TOKEN'
90+
}
91+
92+
return {
93+
token,
94+
login,
95+
}
96+
})
97+
98+
// is equivalent to:
99+
export const useUserStore = defineStore('user', () => {
100+
let token = $ref('')
101+
function login() {
102+
token = 'TOKEN'
103+
}
104+
105+
return {
106+
token: $$(token),
107+
login: $$(login),
108+
}
109+
})
110+
```
111+
84112
```vue
85113
<script setup lang="tsx">
86114
import { useBase64 } from '@vueuse/core'

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "unplugin-vue-reactivity-function",
33
"version": "0.1.0",
4-
"packageManager": "pnpm@8.9.0",
5-
"description": "Reactivity function for Vue SFC.",
4+
"packageManager": "pnpm@8.10.0",
5+
"description": "Reactivity function.",
66
"keywords": [
77
"unplugin",
88
"rollup",
@@ -11,13 +11,13 @@
1111
"webpack"
1212
],
1313
"license": "MIT",
14-
"homepage": "https://github.com/unplugin/unplugin-vue-reactivity-function#readme",
14+
"homepage": "https://github.com/zhiyuanzmj/unplugin-vue-reactivity-function#readme",
1515
"bugs": {
16-
"url": "https://github.com/unplugin/unplugin-vue-reactivity-function/issues"
16+
"url": "https://github.com/zhiyuanzmj/unplugin-vue-reactivity-function/issues"
1717
},
1818
"repository": {
1919
"type": "git",
20-
"url": "git+https://github.com/unplugin/unplugin-vue-reactivity-function.git"
20+
"url": "git+https://github.com/zhiyuanzmj/unplugin-vue-reactivity-function.git"
2121
},
2222
"author": "zhiyuanzmj",
2323
"files": [

playground/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"dependencies": {
1515
"@vueuse/core": "^10.5.0",
16+
"pinia": "^2.1.7",
1617
"vue": "^3.3.4"
1718
},
1819
"devDependencies": {

playground/pnpm-lock.yaml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

playground/src/App.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<script setup lang="ts">
2-
import { type Ref, inject, ref, watch } from 'vue'
2+
import { type Ref, inject, ref, toRefs, watch } from 'vue'
33
import { useBase64 } from '@vueuse/core'
4+
import { useUserStore } from '../store/user'
45
56
function logRef(...logs: Ref<any>[]) {
67
console.log(logs)
78
}
89
9-
const text = $inject('text', ref('vue'))
10+
const { token, login } = $toRefs(useUserStore())
11+
login()
12+
13+
const text = $inject('text', token)
1014
const { base64 } = $useBase64(text)
1115
$watch(base64, () => {
1216
$logRef(base64)

playground/src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { createApp } from 'vue'
2+
import { createPinia } from 'pinia'
23
import App from './App.vue'
34

4-
createApp(App).mount('#app')
5+
const pinia = createPinia()
6+
createApp(App).use(pinia).mount('#app')

playground/store/user.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineStore } from 'pinia'
2+
3+
export const useUserStore = $$defineStore('user', () => {
4+
let token = $ref('')
5+
function login() {
6+
token = 'TOKEN'
7+
}
8+
9+
return {
10+
token,
11+
login,
12+
}
13+
})

src/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ function transformArguments(argument: t.Node, s: MagicString, offset: number) {
2121
) {
2222
s.appendLeft(argument.start! + offset, '$$(')
2323
s.appendRight(argument.end! + offset, ')')
24+
} else if (
25+
argument.type === 'FunctionExpression' ||
26+
argument.type === 'ArrowFunctionExpression'
27+
) {
28+
transformFunctionReturn(argument, s, offset)
2429
} else if (argument.type === 'ArrayExpression') {
2530
argument.elements.forEach((arg) => {
2631
transformArguments(arg!, s, offset)
@@ -41,6 +46,23 @@ function transformArguments(argument: t.Node, s: MagicString, offset: number) {
4146
}
4247
}
4348

49+
function transformFunctionReturn(node: t.Node, s: MagicString, offset: number) {
50+
if (
51+
node.type === 'FunctionDeclaration' ||
52+
node.type === 'FunctionExpression' ||
53+
node.type === 'ArrowFunctionExpression'
54+
)
55+
if (node.body.type !== 'BlockStatement') {
56+
transformArguments(node.body, s, offset)
57+
} else {
58+
node.body.body?.forEach((statement) => {
59+
if (statement.type === 'ReturnStatement' && statement.argument) {
60+
transformArguments(statement.argument, s, offset)
61+
}
62+
})
63+
}
64+
}
65+
4466
function transformReactivityFunction(code: string, id: string) {
4567
const lang = getLang(id)
4668
let asts: {

src/volar.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ function transform({
3535
argument.getEnd(),
3636
')'
3737
)
38+
} else if (
39+
ts.isArrowFunction(argument) ||
40+
ts.isFunctionExpression(argument)
41+
) {
42+
transformFunctionReturn(argument)
3843
} else if (ts.isArrayLiteralExpression(argument)) {
3944
argument.forEachChild((arg) => transformArguments(arg))
4045
} else if (ts.isObjectLiteralExpression(argument)) {
@@ -62,6 +67,26 @@ function transform({
6267
}
6368
}
6469

70+
function transformFunctionReturn(
71+
node: import('typescript/lib/tsserverlibrary').Node
72+
) {
73+
if (
74+
ts.isArrowFunction(node) ||
75+
ts.isFunctionExpression(node) ||
76+
ts.isFunctionDeclaration(node)
77+
) {
78+
if (ts.isArrowFunction(node) && !ts.isBlock(node.body)) {
79+
transformArguments(node.body)
80+
} else {
81+
node.body?.forEachChild((statement) => {
82+
if (ts.isReturnStatement(statement) && statement.expression) {
83+
transformArguments(statement.expression)
84+
}
85+
})
86+
}
87+
}
88+
}
89+
6590
function walkReactivityFunction(
6691
node: import('typescript/lib/tsserverlibrary').Node,
6792
parent: import('typescript/lib/tsserverlibrary').Node

0 commit comments

Comments
 (0)