@@ -10,6 +10,7 @@ import type {
1010} from 'vue/compiler-sfc'
1111import type * as _compiler from 'vue/compiler-sfc'
1212/* eslint-enable import/no-duplicates */
13+ import { computed , shallowRef } from 'vue'
1314import { version } from '../package.json'
1415import { resolveCompiler } from './compiler'
1516import { parseVueRequest } from './utils/query'
@@ -101,63 +102,57 @@ export interface ResolvedOptions extends Options {
101102}
102103
103104export 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 > ( {
126106 isProduction : process . env . NODE_ENV === 'production' ,
127107 compiler : null as any , // to be set in buildStart
108+ include : / \. v u e $ / ,
109+ customElement : / \. c e \. v u e $ / ,
110+ reactivityTransform : false ,
128111 ...rawOptions ,
129- include,
130- exclude,
131- customElement,
132- reactivityTransform,
133112 root : process . cwd ( ) ,
134113 sourceMap : true ,
135114 cssDevSourcemap : false ,
136115 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+ )
138133
139134 return {
140135 name : 'vite:vue' ,
141136
142137 api : {
143138 get options ( ) {
144- return options
139+ return options . value
145140 } ,
146141 set options ( value ) {
147- options = value
142+ options . value = value
148143 } ,
149144 version,
150145 } ,
151146
152147 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 )
155150 }
156151 if ( typeDepToSFCMap . has ( ctx . file ) ) {
157152 return handleTypeDepChange ( typeDepToSFCMap . get ( ctx . file ) ! , ctx )
158153 }
159- if ( filter ( ctx . file ) ) {
160- return handleHotUpdate ( ctx , options )
154+ if ( filter . value ( ctx . file ) ) {
155+ return handleHotUpdate ( ctx , options . value )
161156 }
162157 } ,
163158
@@ -180,8 +175,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
180175 } ,
181176
182177 configResolved ( config ) {
183- options = {
184- ...options ,
178+ options . value = {
179+ ...options . value ,
185180 root : config . root ,
186181 sourceMap : config . command === 'build' ? ! ! config . build . sourcemap : true ,
187182 cssDevSourcemap : config . css ?. devSourcemap ?? false ,
@@ -192,14 +187,14 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
192187 } ,
193188
194189 configureServer ( server ) {
195- options . devServer = server
190+ options . value . devServer = server
196191 } ,
197192
198193 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 ) )
201196 if ( compiler . invalidateTypeCache ) {
202- options . devServer ?. watcher . on ( 'unlink' , ( file ) => {
197+ options . value . devServer ?. watcher . on ( 'unlink' , ( file ) => {
203198 compiler . invalidateTypeCache ( file )
204199 } )
205200 }
@@ -229,7 +224,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
229224 if ( query . src ) {
230225 return fs . readFileSync ( filename , 'utf-8' )
231226 }
232- const descriptor = getDescriptor ( filename , options ) !
227+ const descriptor = getDescriptor ( filename , options . value ) !
233228 let block : SFCBlock | null | undefined
234229 if ( query . type === 'script' ) {
235230 // handle <script> + <script setup> merge via compileScript()
@@ -258,13 +253,13 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
258253 return
259254 }
260255
261- if ( ! filter ( filename ) && ! query . vue ) {
256+ if ( ! filter . value ( filename ) && ! query . vue ) {
262257 if (
263258 ! query . vue &&
264- refTransformFilter ( filename ) &&
265- options . compiler . shouldTransformRef ( code )
259+ refTransformFilter . value ( filename ) &&
260+ options . value . compiler . shouldTransformRef ( code )
266261 ) {
267- return options . compiler . transformRef ( code , {
262+ return options . value . compiler . transformRef ( code , {
268263 filename,
269264 sourceMap : true ,
270265 } )
@@ -277,26 +272,32 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
277272 return transformMain (
278273 code ,
279274 filename ,
280- options ,
275+ options . value ,
281276 this ,
282277 ssr ,
283- customElementFilter ( filename ) ,
278+ customElementFilter . value ( filename ) ,
284279 )
285280 } else {
286281 // sub block request
287282 const descriptor = query . src
288283 ? getSrcDescriptor ( filename , query ) ||
289284 getTempSrcDescriptor ( filename , query )
290- : getDescriptor ( filename , options ) !
285+ : getDescriptor ( filename , options . value ) !
291286
292287 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+ )
294295 } else if ( query . type === 'style' ) {
295296 return transformStyle (
296297 code ,
297298 descriptor ,
298299 Number ( query . index || 0 ) ,
299- options ,
300+ options . value ,
300301 this ,
301302 filename ,
302303 )
0 commit comments