@@ -10,6 +10,7 @@ import type {
10
10
} from 'vue/compiler-sfc'
11
11
import type * as _compiler from 'vue/compiler-sfc'
12
12
/* eslint-enable import/no-duplicates */
13
+ import { computed , shallowRef } from 'vue'
13
14
import { version } from '../package.json'
14
15
import { resolveCompiler } from './compiler'
15
16
import { parseVueRequest } from './utils/query'
@@ -101,63 +102,57 @@ export interface ResolvedOptions extends Options {
101
102
}
102
103
103
104
export default function vuePlugin ( rawOptions : Options = { } ) : Plugin {
104
- const {
105
- include = / \. v u e $ / ,
106
- exclude,
107
- customElement = / \. c e \. v u e $ / ,
108
- reactivityTransform = false ,
109
- } = rawOptions
110
-
111
- const filter = createFilter ( include , exclude )
112
-
113
- const customElementFilter =
114
- typeof customElement === 'boolean'
115
- ? ( ) => customElement
116
- : createFilter ( customElement )
117
-
118
- const refTransformFilter =
119
- reactivityTransform === false
120
- ? ( ) => false
121
- : reactivityTransform === true
122
- ? createFilter ( / \. ( j | t ) s x ? $ / , / n o d e _ m o d u l e s / )
123
- : createFilter ( reactivityTransform )
124
-
125
- let options : ResolvedOptions = {
105
+ const options = shallowRef < ResolvedOptions > ( {
126
106
isProduction : process . env . NODE_ENV === 'production' ,
127
107
compiler : null as any , // to be set in buildStart
108
+ include : / \. v u e $ / ,
109
+ customElement : / \. c e \. v u e $ / ,
110
+ reactivityTransform : false ,
128
111
...rawOptions ,
129
- include,
130
- exclude,
131
- customElement,
132
- reactivityTransform,
133
112
root : process . cwd ( ) ,
134
113
sourceMap : true ,
135
114
cssDevSourcemap : false ,
136
115
devToolsEnabled : process . env . NODE_ENV !== 'production' ,
137
- }
116
+ } )
117
+
118
+ const filter = computed ( ( ) =>
119
+ createFilter ( options . value . include , options . value . exclude ) ,
120
+ )
121
+ const customElementFilter = computed ( ( ) =>
122
+ typeof options . value . customElement === 'boolean'
123
+ ? ( ) => options . value . customElement as boolean
124
+ : createFilter ( options . value . customElement ) ,
125
+ )
126
+ const refTransformFilter = computed ( ( ) =>
127
+ options . value . reactivityTransform === false
128
+ ? ( ) => false
129
+ : options . value . reactivityTransform === true
130
+ ? createFilter ( / \. ( j | t ) s x ? $ / , / n o d e _ m o d u l e s / )
131
+ : createFilter ( options . value . reactivityTransform ) ,
132
+ )
138
133
139
134
return {
140
135
name : 'vite:vue' ,
141
136
142
137
api : {
143
138
get options ( ) {
144
- return options
139
+ return options . value
145
140
} ,
146
141
set options ( value ) {
147
- options = value
142
+ options . value = value
148
143
} ,
149
144
version,
150
145
} ,
151
146
152
147
handleHotUpdate ( ctx ) {
153
- if ( options . compiler . invalidateTypeCache ) {
154
- options . compiler . invalidateTypeCache ( ctx . file )
148
+ if ( options . value . compiler . invalidateTypeCache ) {
149
+ options . value . compiler . invalidateTypeCache ( ctx . file )
155
150
}
156
151
if ( typeDepToSFCMap . has ( ctx . file ) ) {
157
152
return handleTypeDepChange ( typeDepToSFCMap . get ( ctx . file ) ! , ctx )
158
153
}
159
- if ( filter ( ctx . file ) ) {
160
- return handleHotUpdate ( ctx , options )
154
+ if ( filter . value ( ctx . file ) ) {
155
+ return handleHotUpdate ( ctx , options . value )
161
156
}
162
157
} ,
163
158
@@ -180,8 +175,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
180
175
} ,
181
176
182
177
configResolved ( config ) {
183
- options = {
184
- ...options ,
178
+ options . value = {
179
+ ...options . value ,
185
180
root : config . root ,
186
181
sourceMap : config . command === 'build' ? ! ! config . build . sourcemap : true ,
187
182
cssDevSourcemap : config . css ?. devSourcemap ?? false ,
@@ -192,14 +187,14 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
192
187
} ,
193
188
194
189
configureServer ( server ) {
195
- options . devServer = server
190
+ options . value . devServer = server
196
191
} ,
197
192
198
193
buildStart ( ) {
199
- const compiler = ( options . compiler =
200
- options . compiler || resolveCompiler ( options . root ) )
194
+ const compiler = ( options . value . compiler =
195
+ options . value . compiler || resolveCompiler ( options . value . root ) )
201
196
if ( compiler . invalidateTypeCache ) {
202
- options . devServer ?. watcher . on ( 'unlink' , ( file ) => {
197
+ options . value . devServer ?. watcher . on ( 'unlink' , ( file ) => {
203
198
compiler . invalidateTypeCache ( file )
204
199
} )
205
200
}
@@ -229,7 +224,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
229
224
if ( query . src ) {
230
225
return fs . readFileSync ( filename , 'utf-8' )
231
226
}
232
- const descriptor = getDescriptor ( filename , options ) !
227
+ const descriptor = getDescriptor ( filename , options . value ) !
233
228
let block : SFCBlock | null | undefined
234
229
if ( query . type === 'script' ) {
235
230
// handle <script> + <script setup> merge via compileScript()
@@ -258,13 +253,13 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
258
253
return
259
254
}
260
255
261
- if ( ! filter ( filename ) && ! query . vue ) {
256
+ if ( ! filter . value ( filename ) && ! query . vue ) {
262
257
if (
263
258
! query . vue &&
264
- refTransformFilter ( filename ) &&
265
- options . compiler . shouldTransformRef ( code )
259
+ refTransformFilter . value ( filename ) &&
260
+ options . value . compiler . shouldTransformRef ( code )
266
261
) {
267
- return options . compiler . transformRef ( code , {
262
+ return options . value . compiler . transformRef ( code , {
268
263
filename,
269
264
sourceMap : true ,
270
265
} )
@@ -277,26 +272,32 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
277
272
return transformMain (
278
273
code ,
279
274
filename ,
280
- options ,
275
+ options . value ,
281
276
this ,
282
277
ssr ,
283
- customElementFilter ( filename ) ,
278
+ customElementFilter . value ( filename ) ,
284
279
)
285
280
} else {
286
281
// sub block request
287
282
const descriptor = query . src
288
283
? getSrcDescriptor ( filename , query ) ||
289
284
getTempSrcDescriptor ( filename , query )
290
- : getDescriptor ( filename , options ) !
285
+ : getDescriptor ( filename , options . value ) !
291
286
292
287
if ( query . type === 'template' ) {
293
- return transformTemplateAsModule ( code , descriptor , options , this , ssr )
288
+ return transformTemplateAsModule (
289
+ code ,
290
+ descriptor ,
291
+ options . value ,
292
+ this ,
293
+ ssr ,
294
+ )
294
295
} else if ( query . type === 'style' ) {
295
296
return transformStyle (
296
297
code ,
297
298
descriptor ,
298
299
Number ( query . index || 0 ) ,
299
- options ,
300
+ options . value ,
300
301
this ,
301
302
filename ,
302
303
)
0 commit comments