33 JSX_TSX_REG , NAME ,
44 SUPPORT_FILE_REG ,
55 log ,
6- runAsyncTaskList ,
76 setTArray ,
87 transformSymbol ,
98} from '@unplugin-vue-cssvars/utils'
@@ -25,7 +24,7 @@ import type { MagicStringBase } from 'magic-string-ast'
2524import type { HmrContext , ResolvedConfig } from 'vite'
2625import type { TMatchVariable } from './parser'
2726import type { Options } from './types'
28- // TODO: webpack hmr
27+ // TODO refactor
2928const unplugin = createUnplugin < Options > (
3029 ( options : Options = { } , meta ) : any => {
3130 const framework = meta . framework
@@ -39,12 +38,13 @@ const unplugin = createUnplugin<Options>(
3938 const vbindVariableList = new Map < string , TMatchVariable > ( )
4039 let isScriptSetup = false
4140 if ( userOptions . server === undefined ) {
41+ log ( 'warning' , 'The server of option is not set, you need to specify whether you are using the development server or building the project' )
42+ log ( 'warning' , 'The server of option is not set, you need to specify whether you are using the development server or building the project' )
4243 console . warn ( chalk . yellowBright . bold ( `[${ NAME } ] The server of option is not set, you need to specify whether you are using the development server or building the project` ) )
4344 console . warn ( chalk . yellowBright . bold ( `[${ NAME } ] See: https://github.com/baiwusanyu-c/unplugin-vue-cssvars/blob/master/README.md#option` ) )
4445 }
4546 let isServer = ! ! userOptions . server
4647 let isHMR = false
47- const cacheWebpackModule = new Map < string , any > ( )
4848
4949 function handleVBindVariable (
5050 code : string ,
@@ -93,8 +93,9 @@ const unplugin = createUnplugin<Options>(
9393 mgcStr = res
9494 }
9595
96- if ( ( transId . includes ( '?vue&type=style' ) && isHMR && framework === 'webpack' ) ) {
97- transId = transId . split ( '?vue&type=style' ) [ 0 ]
96+ if ( ( transId . includes ( '?vue&type=style' ) || transId . includes ( '?vue&type=script' ) )
97+ && isHMR && framework === 'webpack' ) {
98+ transId = transId . split ( '?vue' ) [ 0 ]
9899 const res = handleVBindVariable ( code , transId , mgcStr )
99100 if ( res )
100101 mgcStr = res
@@ -134,10 +135,12 @@ const unplugin = createUnplugin<Options>(
134135 }
135136 } ,
136137 } ,
138+
139+ // TODO unit test
137140 webpack ( compiler ) {
138141 // mark webpack hmr
139142 let modifiedFile = ''
140- compiler . hooks . watchRun . tap ( NAME , ( compilation1 ) => {
143+ compiler . hooks . watchRun . tapAsync ( NAME , ( compilation1 , watchRunCallBack ) => {
141144 if ( compilation1 . modifiedFiles ) {
142145 modifiedFile = transformSymbol ( setTArray ( compilation1 . modifiedFiles ) [ 0 ] as string )
143146 if ( SUPPORT_FILE_REG . test ( modifiedFile ) ) {
@@ -149,6 +152,7 @@ const unplugin = createUnplugin<Options>(
149152 )
150153 }
151154 }
155+ watchRunCallBack ( )
152156 } )
153157
154158 compiler . hooks . compilation . tap ( NAME , ( compilation ) => {
@@ -180,6 +184,8 @@ const unplugin = createUnplugin<Options>(
180184 Promise . all ( promises )
181185 . then ( ( ) => {
182186 callback ( )
187+ // hmr end
188+ isHMR = false
183189 } )
184190 . catch ( ( e ) => {
185191 log ( 'error' , e )
@@ -202,6 +208,7 @@ const unplugin = createUnplugin<Options>(
202208 return filter ( id )
203209 } ,
204210 async transform ( code : string , id : string ) {
211+ console . log ( id )
205212 let transId = transformSymbol ( id )
206213 let mgcStr = new MagicString ( code )
207214 // ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
@@ -211,7 +218,7 @@ const unplugin = createUnplugin<Options>(
211218 const injectRes = injectCSSVars ( vbindVariableList . get ( idKey ) , isScriptSetup , parseRes , mgcStr )
212219 mgcStr = injectRes . mgcStr
213220 injectRes . vbindVariableList && vbindVariableList . set ( transId , injectRes . vbindVariableList )
214- isHMR = false
221+ // TODO vite hmr close ? isHMR -> false
215222 }
216223
217224 // transform in dev
@@ -237,7 +244,7 @@ const unplugin = createUnplugin<Options>(
237244 // webpack dev 和 build 都回进入这里
238245 if ( framework === 'webpack' ) {
239246 if ( transId . includes ( '?vue&type=script' ) ) {
240- transId = transId . split ( '?vue&type=script ' ) [ 0 ]
247+ transId = transId . split ( '?vue' ) [ 0 ]
241248 injectCSSVarsFn ( transId )
242249 }
243250
0 commit comments