11import { createUnplugin } from 'unplugin'
2- import { JSX_TSX_REG , NAME , SUPPORT_FILE_REG , setTArray } from '@unplugin-vue-cssvars/utils'
2+ import {
3+ JSX_TSX_REG , NAME ,
4+ SUPPORT_FILE_REG ,
5+ setTArray ,
6+ transformSymbol } from '@unplugin-vue-cssvars/utils'
37import { createFilter } from '@rollup/pluginutils'
48import { parse } from '@vue/compiler-sfc'
59import chalk from 'chalk'
@@ -44,13 +48,14 @@ const unplugin = createUnplugin<Options>(
4448 return filter ( id )
4549 } ,
4650 async transform ( code : string , id : string ) {
51+ const transId = transformSymbol ( id )
4752 let mgcStr = new MagicString ( code )
4853 try {
4954 // ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
5055 // webpack 时 使用 id.includes('vue&type=style') 判断
5156 // webpack dev 和 build 都回进入这里
52- if ( id . endsWith ( '.vue' )
53- || ( id . includes ( 'vue&type=style' ) && framework === 'webpack' ) ) {
57+ if ( transId . endsWith ( '.vue' )
58+ || ( transId . includes ( 'vue&type=style' ) && framework === 'webpack' ) ) {
5459 const { descriptor } = parse ( code )
5560 const lang = descriptor ?. script ?. lang ?? 'js'
5661 // ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
@@ -59,12 +64,12 @@ const unplugin = createUnplugin<Options>(
5964 const {
6065 vbindVariableListByPath,
6166 injectCSSContent,
62- } = getVBindVariableListByPath ( descriptor , id , CSSFileModuleMap , isServer , userOptions . alias )
67+ } = getVBindVariableListByPath ( descriptor , transId , CSSFileModuleMap , isServer , userOptions . alias )
6368 const variableName = getVariable ( descriptor )
64- vbindVariableList . set ( id , matchVariable ( vbindVariableListByPath , variableName ) )
69+ vbindVariableList . set ( transId , matchVariable ( vbindVariableListByPath , variableName ) )
6570
6671 // vite、rollup、esbuild 打包生效
67- if ( ! isServer && framework === 'webpack' && framework = == 'rspack' )
72+ if ( ! isServer && framework !== 'webpack' && framework ! == 'rspack' )
6873 mgcStr = injectCssOnBuild ( mgcStr , injectCSSContent , descriptor )
6974 }
7075 }
@@ -111,14 +116,15 @@ const unplugin = createUnplugin<Options>(
111116 return filter ( id )
112117 } ,
113118 async transform ( code : string , id : string ) {
119+ let transId = transformSymbol ( id )
114120 let mgcStr = new MagicString ( code )
115121 // ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
116122 try {
117123 function injectCSSVarsFn ( idKey : string ) {
118124 const parseRes = parserCompiledSfc ( code )
119125 const injectRes = injectCSSVars ( vbindVariableList . get ( idKey ) , isScriptSetup , parseRes , mgcStr )
120126 mgcStr = injectRes . mgcStr
121- injectRes . vbindVariableList && vbindVariableList . set ( id , injectRes . vbindVariableList )
127+ injectRes . vbindVariableList && vbindVariableList . set ( transId , injectRes . vbindVariableList )
122128 isHmring = false
123129 }
124130
@@ -128,13 +134,12 @@ const unplugin = createUnplugin<Options>(
128134 if ( framework === 'vite'
129135 || framework === 'rollup'
130136 || framework === 'esbuild' ) {
131- if ( id . endsWith ( '.vue' ) )
132- injectCSSVarsFn ( id )
133-
134- if ( id . includes ( 'vue&type=style' ) ) {
137+ if ( transId . endsWith ( '.vue' ) )
138+ injectCSSVarsFn ( transId )
139+ if ( transId . includes ( 'vue&type=style' ) ) {
135140 mgcStr = injectCssOnServer (
136141 mgcStr ,
137- vbindVariableList . get ( id . split ( '?vue' ) [ 0 ] ) ,
142+ vbindVariableList . get ( transId . split ( '?vue' ) [ 0 ] ) ,
138143 isHmring ,
139144 )
140145 }
@@ -143,25 +148,22 @@ const unplugin = createUnplugin<Options>(
143148
144149 // webpack dev 和 build 都回进入这里
145150 if ( framework === 'webpack' ) {
146- const { _module } = this
147-
148- // 判断是否是热更新引起的执行
149- const isHotUpdate = _module && _module . hot && _module . hot . data
150- console . log ( isHotUpdate )
151-
152- if ( id . includes ( 'vue&type=script' ) ) {
153- const transId = id . split ( '?vue&type=script' ) [ 0 ]
151+ if ( transId . includes ( 'vue&type=script' ) ) {
152+ transId = transId . split ( '?vue&type=script' ) [ 0 ]
154153 injectCSSVarsFn ( transId )
155154 }
156- const cssFMM = CSSFileModuleMap . get ( id )
155+ /*mgcStr = mgcStr.replaceAll(
156+ 'vue&type=template&id=7ba5bd90&scoped=true&ts=true", () => {',
157+ 'vue&type=template&id=7ba5bd90&scoped=true&ts=true", () => { console.log(render);')*/
158+ const cssFMM = CSSFileModuleMap . get ( transId )
157159 if ( cssFMM && cssFMM . sfcPath && cssFMM . sfcPath . size > 0 ) {
158160 const sfcPathIdList = setTArray ( cssFMM . sfcPath )
159161 sfcPathIdList . forEach ( ( v ) => {
160162 mgcStr = injectCssOnServer ( mgcStr , vbindVariableList . get ( v ) , isHmring )
161163 } )
162164 }
163165 }
164-
166+ // console.log(mgcStr.toString())
165167 return {
166168 code : mgcStr . toString ( ) ,
167169 get map ( ) {
0 commit comments