11import { createUnplugin } from 'unplugin'
2- import { NAME } from '@unplugin-vue-cssvars/utils'
2+ import { NAME , setTArray , SUPPORT_FILE_REG } from '@unplugin-vue-cssvars/utils'
33import { createFilter } from '@rollup/pluginutils'
44import { parse } from '@vue/compiler-sfc'
55import chalk from 'chalk'
@@ -15,7 +15,7 @@ import {
1515import type { ResolvedConfig } from 'vite'
1616import type { TMatchVariable } from './parser'
1717import type { Options } from './types'
18-
18+ // TODO: webpack hmr
1919const unplugin = createUnplugin < Options > (
2020 ( options : Options = { } ) : any => {
2121 const userOptions = initOption ( options )
@@ -25,15 +25,14 @@ const unplugin = createUnplugin<Options>(
2525 )
2626 // 预处理 css 文件
2727 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 > ( )
3129 let isScriptSetup = false
3230 if ( userOptions . server === undefined ) {
3331 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` ) )
3432 console . warn ( chalk . yellowBright . bold ( `[${ NAME } ] See: https://github.com/baiwusanyu-c/unplugin-vue-cssvars/blob/master/README.md#option` ) )
3533 }
3634 let isServer = ! ! userOptions . server
35+ let isHmring = false
3736 return [
3837 {
3938 name : NAME ,
@@ -52,9 +51,7 @@ const unplugin = createUnplugin<Options>(
5251 injectCSSContent,
5352 } = getVBindVariableListByPath ( descriptor , id , CSSFileModuleMap , isServer , userOptions . alias )
5453 const variableName = getVariable ( descriptor )
55- vbindVariableList . set ( id , {
56- TMatchVariable : matchVariable ( vbindVariableListByPath , variableName ) ,
57- } )
54+ vbindVariableList . set ( id , matchVariable ( vbindVariableListByPath , variableName ) )
5855
5956 if ( ! isServer )
6057 code = injectCssOnBuild ( code , injectCSSContent , descriptor )
@@ -73,8 +70,29 @@ const unplugin = createUnplugin<Options>(
7370 isServer = config . command === 'serve'
7471 } ,
7572 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+ }
7896 } ,
7997 } ,
8098 } ,
@@ -87,22 +105,13 @@ const unplugin = createUnplugin<Options>(
87105 // transform in dev
88106 if ( isServer ) {
89107 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 )
95109 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
105112 }
113+ if ( id . includes ( 'type=style' ) )
114+ code = injectCssOnServer ( code , vbindVariableList . get ( id . split ( '?vue' ) [ 0 ] ) , isHmring )
106115 }
107116 return code
108117 } catch ( err : unknown ) {
0 commit comments