@@ -14,7 +14,9 @@ import {
14
14
SFCBlock ,
15
15
SFCScriptCompileOptions ,
16
16
SFCStyleCompileOptions ,
17
- SFCTemplateCompileOptions
17
+ SFCTemplateCompileOptions ,
18
+ shouldTransformRef ,
19
+ transformRef
18
20
} from '@vue/compiler-sfc'
19
21
import { parseVueRequest } from './utils/query'
20
22
import { getDescriptor } from './utils/descriptorCache'
@@ -54,6 +56,17 @@ export interface Options {
54
56
*/
55
57
customElement ?: boolean | string | RegExp | ( string | RegExp ) [ ]
56
58
59
+ /**
60
+ * Enable Vue ref transform (experimental).
61
+ * **requires Vue \>= 3.2.5**
62
+ * - `true`: transform will be enabled for all vue,js(x),ts(x) files
63
+ * - `string | RegExp`: apply to vue + only matched files
64
+ * - `false`: disable in all cases
65
+ *
66
+ * @default false
67
+ */
68
+ refTransform ?: boolean | string | RegExp | ( string | RegExp ) [ ]
69
+
57
70
/**
58
71
* @deprecated the plugin now auto-detects whether it's being invoked for ssr.
59
72
*/
@@ -66,22 +79,33 @@ export interface ResolvedOptions extends Options {
66
79
}
67
80
68
81
export default function vuePlugin ( rawOptions : Options = { } ) : Plugin {
82
+ const {
83
+ include = / \. v u e $ / ,
84
+ exclude,
85
+ customElement = / \. c e \. v u e $ / ,
86
+ refTransform = false
87
+ } = rawOptions
88
+
89
+ const filter = createFilter ( include , exclude )
90
+
91
+ const customElementFilter =
92
+ typeof customElement === 'boolean'
93
+ ? ( ) => customElement
94
+ : createFilter ( customElement )
95
+
96
+ const refTransformFilter =
97
+ refTransform === false
98
+ ? ( ) => false
99
+ : refTransform === true
100
+ ? createFilter ( / \. ( j | t ) s x ? $ / )
101
+ : createFilter ( refTransform )
102
+
69
103
let options : ResolvedOptions = {
70
104
isProduction : process . env . NODE_ENV === 'production' ,
71
105
...rawOptions ,
72
106
root : process . cwd ( )
73
107
}
74
108
75
- const filter = createFilter (
76
- rawOptions . include || / \. v u e $ / ,
77
- rawOptions . exclude
78
- )
79
-
80
- const customElementFilter =
81
- typeof rawOptions . customElement === 'boolean'
82
- ? ( ) => rawOptions . customElement as boolean
83
- : createFilter ( rawOptions . customElement || / \. c e \. v u e $ / )
84
-
85
109
return {
86
110
name : 'vite:vue' ,
87
111
@@ -154,7 +178,20 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
154
178
155
179
transform ( code , id , ssr = ! ! options . ssr ) {
156
180
const { filename, query } = parseVueRequest ( id )
157
- if ( ( ! query . vue && ! filter ( filename ) ) || query . raw ) {
181
+ if ( query . raw ) {
182
+ return
183
+ }
184
+ if ( ! filter ( filename ) && ! query . vue ) {
185
+ if (
186
+ refTransformFilter ( filename ) &&
187
+ ! query . vue &&
188
+ shouldTransformRef ( code )
189
+ ) {
190
+ return transformRef ( code , {
191
+ filename,
192
+ sourceMap : true
193
+ } )
194
+ }
158
195
return
159
196
}
160
197
0 commit comments