1
1
import { createUnplugin } from 'unplugin'
2
- import { NAME } from '@unplugin-vue-cssvars/utils'
2
+ import { NAME , setTArray , SUPPORT_FILE_REG } from '@unplugin-vue-cssvars/utils'
3
3
import { createFilter } from '@rollup/pluginutils'
4
4
import { parse } from '@vue/compiler-sfc'
5
5
import chalk from 'chalk'
@@ -15,7 +15,7 @@ import {
15
15
import type { ResolvedConfig } from 'vite'
16
16
import type { TMatchVariable } from './parser'
17
17
import type { Options } from './types'
18
-
18
+ // TODO: webpack hmr
19
19
const unplugin = createUnplugin < Options > (
20
20
( options : Options = { } ) : any => {
21
21
const userOptions = initOption ( options )
@@ -25,15 +25,14 @@ const unplugin = createUnplugin<Options>(
25
25
)
26
26
// 预处理 css 文件
27
27
const CSSFileModuleMap = preProcessCSS ( userOptions , userOptions . alias )
28
- const vbindVariableList = new Map < string , {
29
- TMatchVariable : TMatchVariable
30
- orgTransformCode ?: string } > ( )
28
+ const vbindVariableList = new Map < string , TMatchVariable > ( )
31
29
let isScriptSetup = false
32
30
if ( userOptions . server === undefined ) {
33
31
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` ) )
34
32
console . warn ( chalk . yellowBright . bold ( `[${ NAME } ] See: https://github.com/baiwusanyu-c/unplugin-vue-cssvars/blob/master/README.md#option` ) )
35
33
}
36
34
let isServer = ! ! userOptions . server
35
+ let isHmring = false
37
36
return [
38
37
{
39
38
name : NAME ,
@@ -52,9 +51,7 @@ const unplugin = createUnplugin<Options>(
52
51
injectCSSContent,
53
52
} = getVBindVariableListByPath ( descriptor , id , CSSFileModuleMap , isServer , userOptions . alias )
54
53
const variableName = getVariable ( descriptor )
55
- vbindVariableList . set ( id , {
56
- TMatchVariable : matchVariable ( vbindVariableListByPath , variableName ) ,
57
- } )
54
+ vbindVariableList . set ( id , matchVariable ( vbindVariableListByPath , variableName ) )
58
55
59
56
if ( ! isServer )
60
57
code = injectCssOnBuild ( code , injectCSSContent , descriptor )
@@ -73,8 +70,29 @@ const unplugin = createUnplugin<Options>(
73
70
isServer = config . command === 'serve'
74
71
} ,
75
72
handleHotUpdate ( hmr ) {
76
- if ( hmr . file . endsWith ( 'foo.css' ) )
77
- return hmr . modules
73
+ // TODO refactor
74
+ if ( SUPPORT_FILE_REG . test ( hmr . file ) ) {
75
+ isHmring = true
76
+ const sfcModulesPathList = CSSFileModuleMap . get ( hmr . file )
77
+ if ( sfcModulesPathList && sfcModulesPathList . sfcPath ) {
78
+ const ls = setTArray ( sfcModulesPathList . sfcPath )
79
+ ls . forEach ( ( sfcp ) => {
80
+ const modules = hmr . server . moduleGraph . fileToModulesMap . get ( sfcp )
81
+ // update CSSFileModuleMap
82
+ const updatedCSSModules = preProcessCSS ( userOptions , userOptions . alias , [ hmr . file ] ) . get ( hmr . file )
83
+ if ( updatedCSSModules )
84
+ CSSFileModuleMap . set ( hmr . file , updatedCSSModules )
85
+
86
+ // update sfc
87
+ const modulesList = setTArray ( modules )
88
+ for ( let i = 0 ; i < modulesList . length ; i ++ ) {
89
+ // ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
90
+ if ( modulesList [ i ] . id . endsWith ( '.vue' ) )
91
+ hmr . server . reloadModule ( modulesList [ i ] )
92
+ }
93
+ } )
94
+ }
95
+ }
78
96
} ,
79
97
} ,
80
98
} ,
@@ -87,22 +105,13 @@ const unplugin = createUnplugin<Options>(
87
105
// transform in dev
88
106
if ( isServer ) {
89
107
if ( id . endsWith ( '.vue' ) ) {
90
- const orgCode = code
91
- // console.log('########', id)
92
- code = code . replaceAll ( 'if (!mod)' , 'console.log(mod)\n if (!mod)' )
93
- // console.log(code)
94
- const injectRes = injectCSSVars ( code , vbindVariableList . get ( id ) . TMatchVariable , isScriptSetup )
108
+ const injectRes = injectCSSVars ( code , vbindVariableList . get ( id ) , isScriptSetup )
95
109
code = injectRes . code
96
- injectRes . vbindVariableList && vbindVariableList . set ( id , {
97
- TMatchVariable : injectRes . vbindVariableList ,
98
- orgTransformCode : orgCode ,
99
- } )
100
- }
101
- if ( id . includes ( 'type=style' ) ) {
102
- console . log ( '########' , id )
103
- code = injectCssOnServer ( code , vbindVariableList . get ( id . split ( '?vue' ) [ 0 ] ) . TMatchVariable )
104
- console . log ( code )
110
+ injectRes . vbindVariableList && vbindVariableList . set ( id , injectRes . vbindVariableList )
111
+ isHmring = false
105
112
}
113
+ if ( id . includes ( 'type=style' ) )
114
+ code = injectCssOnServer ( code , vbindVariableList . get ( id . split ( '?vue' ) [ 0 ] ) , isHmring )
106
115
}
107
116
return code
108
117
} catch ( err : unknown ) {
0 commit comments