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