3
3
JSX_TSX_REG , NAME ,
4
4
SUPPORT_FILE_REG ,
5
5
log ,
6
- runAsyncTaskList ,
7
6
setTArray ,
8
7
transformSymbol ,
9
8
} from '@unplugin-vue-cssvars/utils'
@@ -25,7 +24,7 @@ import type { MagicStringBase } from 'magic-string-ast'
25
24
import type { HmrContext , ResolvedConfig } from 'vite'
26
25
import type { TMatchVariable } from './parser'
27
26
import type { Options } from './types'
28
- // TODO: webpack hmr
27
+ // TODO refactor
29
28
const unplugin = createUnplugin < Options > (
30
29
( options : Options = { } , meta ) : any => {
31
30
const framework = meta . framework
@@ -39,12 +38,13 @@ const unplugin = createUnplugin<Options>(
39
38
const vbindVariableList = new Map < string , TMatchVariable > ( )
40
39
let isScriptSetup = false
41
40
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' )
42
43
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` ) )
43
44
console . warn ( chalk . yellowBright . bold ( `[${ NAME } ] See: https://github.com/baiwusanyu-c/unplugin-vue-cssvars/blob/master/README.md#option` ) )
44
45
}
45
46
let isServer = ! ! userOptions . server
46
47
let isHMR = false
47
- const cacheWebpackModule = new Map < string , any > ( )
48
48
49
49
function handleVBindVariable (
50
50
code : string ,
@@ -93,8 +93,9 @@ const unplugin = createUnplugin<Options>(
93
93
mgcStr = res
94
94
}
95
95
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 ]
98
99
const res = handleVBindVariable ( code , transId , mgcStr )
99
100
if ( res )
100
101
mgcStr = res
@@ -134,10 +135,12 @@ const unplugin = createUnplugin<Options>(
134
135
}
135
136
} ,
136
137
} ,
138
+
139
+ // TODO unit test
137
140
webpack ( compiler ) {
138
141
// mark webpack hmr
139
142
let modifiedFile = ''
140
- compiler . hooks . watchRun . tap ( NAME , ( compilation1 ) => {
143
+ compiler . hooks . watchRun . tapAsync ( NAME , ( compilation1 , watchRunCallBack ) => {
141
144
if ( compilation1 . modifiedFiles ) {
142
145
modifiedFile = transformSymbol ( setTArray ( compilation1 . modifiedFiles ) [ 0 ] as string )
143
146
if ( SUPPORT_FILE_REG . test ( modifiedFile ) ) {
@@ -149,6 +152,7 @@ const unplugin = createUnplugin<Options>(
149
152
)
150
153
}
151
154
}
155
+ watchRunCallBack ( )
152
156
} )
153
157
154
158
compiler . hooks . compilation . tap ( NAME , ( compilation ) => {
@@ -180,6 +184,8 @@ const unplugin = createUnplugin<Options>(
180
184
Promise . all ( promises )
181
185
. then ( ( ) => {
182
186
callback ( )
187
+ // hmr end
188
+ isHMR = false
183
189
} )
184
190
. catch ( ( e ) => {
185
191
log ( 'error' , e )
@@ -202,6 +208,7 @@ const unplugin = createUnplugin<Options>(
202
208
return filter ( id )
203
209
} ,
204
210
async transform ( code : string , id : string ) {
211
+ console . log ( id )
205
212
let transId = transformSymbol ( id )
206
213
let mgcStr = new MagicString ( code )
207
214
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
@@ -211,7 +218,7 @@ const unplugin = createUnplugin<Options>(
211
218
const injectRes = injectCSSVars ( vbindVariableList . get ( idKey ) , isScriptSetup , parseRes , mgcStr )
212
219
mgcStr = injectRes . mgcStr
213
220
injectRes . vbindVariableList && vbindVariableList . set ( transId , injectRes . vbindVariableList )
214
- isHMR = false
221
+ // TODO vite hmr close ? isHMR -> false
215
222
}
216
223
217
224
// transform in dev
@@ -237,7 +244,7 @@ const unplugin = createUnplugin<Options>(
237
244
// webpack dev 和 build 都回进入这里
238
245
if ( framework === 'webpack' ) {
239
246
if ( transId . includes ( '?vue&type=script' ) ) {
240
- transId = transId . split ( '?vue&type=script ' ) [ 0 ]
247
+ transId = transId . split ( '?vue' ) [ 0 ]
241
248
injectCSSVarsFn ( transId )
242
249
}
243
250
0 commit comments