Skip to content

Commit 2bdda25

Browse files
committed
feat: add ignore option
1 parent 4d0746a commit 2bdda25

File tree

8 files changed

+99
-26
lines changed

8 files changed

+99
-26
lines changed

README.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ npm i -D unplugin-vue-reactivity-function
1818
import VueReactivityFunction from 'unplugin-vue-reactivity-function/vite'
1919

2020
export default defineConfig({
21-
plugins: [VueReactivityFunction()],
21+
plugins: [
22+
VueReactivityFunction({
23+
ignore: ['$fetch'],
24+
}),
25+
],
2226
})
2327
```
2428

@@ -32,7 +36,11 @@ export default defineConfig({
3236
import VueReactivityFunction from 'unplugin-vue-reactivity-function/rollup'
3337

3438
export default {
35-
plugins: [VueReactivityFunction()],
39+
plugins: [
40+
VueReactivityFunction({
41+
ignore: ['$fetch'],
42+
}),
43+
],
3644
}
3745
```
3846

@@ -46,7 +54,11 @@ export default {
4654
import { build } from 'esbuild'
4755

4856
build({
49-
plugins: [require('unplugin-vue-reactivity-function/esbuild')()],
57+
plugins: [
58+
require('unplugin-vue-reactivity-function/esbuild')({
59+
ignore: ['$fetch'],
60+
}),
61+
],
5062
})
5163
```
5264

@@ -59,7 +71,11 @@ build({
5971
// webpack.config.js
6072
module.exports = {
6173
/* ... */
62-
plugins: [require('unplugin-vue-reactivity-function/webpack')()],
74+
plugins: [
75+
require('unplugin-vue-reactivity-function/webpack')({
76+
ignore: ['$fetch'],
77+
}),
78+
],
6379
}
6480
```
6581

@@ -72,7 +88,11 @@ module.exports = {
7288
// vue.config.js
7389
module.exports = {
7490
configureWebpack: {
75-
plugins: [require('unplugin-vue-reactivity-function/webpack')()],
91+
plugins: [
92+
require('unplugin-vue-reactivity-function/webpack')({
93+
ignore: ['$fetch'],
94+
}),
95+
],
7696
},
7797
}
7898
```
@@ -158,7 +178,10 @@ defineExpose({
158178
{
159179
// ...
160180
"vueCompilerOptions": {
161-
"plugins": ["unplugin-vue-reactivity-function/volar"]
181+
"plugins": ["unplugin-vue-reactivity-function/volar"],
182+
"reactivityFunction": {
183+
"ignore": ["$fetch"]
184+
}
162185
}
163186
}
164187
```

playground/src/App.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const text = $inject$('text', token)
1010
const { base64 } = $useBase64$(text)
1111
provide$('base64', base64)
1212
13+
const $fetch = fetch
14+
1315
const stop = watch$(base64, () => {
16+
$fetch('')
1417
console.log$(base64)
1518
stop()
1619
})

playground/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"skipLibCheck": true
1818
},
1919
"vueCompilerOptions": {
20-
"plugins": ["../dist/volar.js"]
20+
"plugins": ["../dist/volar.js"],
21+
"reactivityFunction": {
22+
"ignore": ["$fetch"]
23+
}
2124
}
2225
}

playground/vite.config.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ export default defineConfig({
99
},
1010
plugins: [
1111
Vue({
12-
include: [/\.vue$/],
1312
reactivityTransform: true,
14-
script: {
15-
hoistStatic: false,
16-
},
1713
}),
18-
VueReactivityFunction(),
14+
VueReactivityFunction({
15+
ignore: ['$fetch'],
16+
}),
1917
Inspect({
2018
build: true,
2119
}),

src/core/options.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
1-
import type { BaseOptions } from '@vue-macros/common'
1+
import {
2+
type BaseOptions,
3+
REGEX_NODE_MODULES,
4+
REGEX_SUPPORTED_EXT,
5+
} from '@vue-macros/common'
26

3-
export interface Options extends Pick<BaseOptions, 'include' | 'exclude'> {}
7+
export interface Options extends Pick<BaseOptions, 'include' | 'exclude'> {
8+
ignore?: string[]
9+
}
410

5-
export type OptionsResolved = Pick<Required<Options>, 'include'> &
11+
export type OptionsResolved = Pick<Required<Options>, 'include' | 'ignore'> &
612
Pick<Options, 'exclude'>
713

14+
export const ignore = [
15+
'ref',
16+
'computed',
17+
'shallowRef',
18+
'toRef',
19+
'customRef',
20+
'defineProp',
21+
'defineProps',
22+
'defineModels',
23+
]
24+
825
export function resolveOption(options: Options): OptionsResolved {
926
return {
10-
include: [/\.([cm]?[jt]sx?|vue)$/],
11-
exclude: [/node_modules/],
27+
include: [REGEX_SUPPORTED_EXT],
28+
exclude: [REGEX_NODE_MODULES],
1229
...options,
30+
ignore: [...ignore, ...(options.ignore || []).map((str) => str.slice(1))],
1331
}
1432
}

src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ function transformFunctionReturn(node: t.Node, s: MagicString, offset: number) {
6666
}
6767
}
6868

69-
function transformReactivityFunction(code: string, id: string) {
69+
function transformReactivityFunction(
70+
code: string,
71+
id: string,
72+
ignore: string[]
73+
) {
7074
const lang = getLang(id)
7175
let asts: {
7276
ast: t.Program
@@ -100,7 +104,7 @@ function transformReactivityFunction(code: string, id: string) {
100104
if (node.type !== 'CallExpression') return
101105

102106
if (
103-
/^\$(?!(\$|ref|computed|shallowRef|toRef|customRef|defineProp|defineProps|defineModels)?$)/.test(
107+
new RegExp(`^\\$(?!(\\$|${ignore.join('|')})?$)`).test(
104108
s.sliceNode(node.callee, { offset })
105109
)
106110
) {
@@ -135,7 +139,7 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
135139
return filter(id)
136140
},
137141
transform(code, id) {
138-
return transformReactivityFunction(code, id)
142+
return transformReactivityFunction(code, id, options.ignore)
139143
},
140144
}
141145
})

src/shim.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { FilterPattern } from '@rollup/pluginutils'
2+
3+
declare module '@vue/language-core' {
4+
export interface VueCompilerOptions {
5+
reactivityFunction?: {
6+
include?: FilterPattern
7+
exclude?: FilterPattern
8+
ignore?: string[]
9+
}
10+
}
11+
}
12+
13+
export {}

src/volar.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ import {
55
type VueLanguagePlugin,
66
replaceSourceRange,
77
} from '@vue/language-core'
8+
import { ignore } from './core/options'
89

910
function transform({
1011
codes,
1112
ast,
1213
ts,
1314
source,
15+
ignore,
1416
}: {
1517
codes: Segment<FileRangeCapabilities>[]
1618
ast: import('typescript/lib/tsserverlibrary').SourceFile
1719
ts: typeof import('typescript/lib/tsserverlibrary')
1820
source: 'script' | 'scriptSetup'
21+
ignore: string[]
1922
}) {
2023
function transformArguments(
2124
argument: import('typescript/lib/tsserverlibrary').Node
@@ -92,12 +95,11 @@ function transform({
9295
}
9396

9497
function walkReactivityFunction(
95-
node: import('typescript/lib/tsserverlibrary').Node,
96-
parent: import('typescript/lib/tsserverlibrary').Node
98+
node: import('typescript/lib/tsserverlibrary').Node
9799
) {
98100
if (ts.isCallExpression(node)) {
99101
if (
100-
/^\$(?!(\$|ref|computed|shallowRef|toRef|customRef|defineProp|defineProps|defineModels)?$)/.test(
102+
new RegExp(`^\\$(?!(\\$|${ignore.join('|')})?$)`).test(
101103
node.expression.getText(ast)
102104
)
103105
) {
@@ -125,13 +127,16 @@ function transform({
125127
}
126128

127129
node.forEachChild((child) => {
128-
walkReactivityFunction(child, node)
130+
walkReactivityFunction(child)
129131
})
130132
}
131-
ast.forEachChild((child) => walkReactivityFunction(child, ast))
133+
ast.forEachChild(walkReactivityFunction)
132134
}
133135

134-
const plugin: VueLanguagePlugin = ({ modules: { typescript: ts } }) => {
136+
const plugin: VueLanguagePlugin = ({
137+
modules: { typescript: ts },
138+
vueCompilerOptions,
139+
}) => {
135140
return {
136141
name: 'vue-reactivity-function',
137142
version: 1,
@@ -145,6 +150,12 @@ const plugin: VueLanguagePlugin = ({ modules: { typescript: ts } }) => {
145150
ast: sfc[source]!.ast,
146151
ts,
147152
source,
153+
ignore: [
154+
...ignore,
155+
...(vueCompilerOptions.reactivityFunction?.ignore || []).map(
156+
(str) => str.slice(1)
157+
),
158+
],
148159
})
149160
}
150161
}

0 commit comments

Comments
 (0)