1
1
import { 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'
3
7
import { createFilter } from '@rollup/pluginutils'
4
8
import { parse } from '@vue/compiler-sfc'
5
9
import chalk from 'chalk'
@@ -44,13 +48,14 @@ const unplugin = createUnplugin<Options>(
44
48
return filter ( id )
45
49
} ,
46
50
async transform ( code : string , id : string ) {
51
+ const transId = transformSymbol ( id )
47
52
let mgcStr = new MagicString ( code )
48
53
try {
49
54
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
50
55
// webpack 时 使用 id.includes('vue&type=style') 判断
51
56
// 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' ) ) {
54
59
const { descriptor } = parse ( code )
55
60
const lang = descriptor ?. script ?. lang ?? 'js'
56
61
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
@@ -59,12 +64,12 @@ const unplugin = createUnplugin<Options>(
59
64
const {
60
65
vbindVariableListByPath,
61
66
injectCSSContent,
62
- } = getVBindVariableListByPath ( descriptor , id , CSSFileModuleMap , isServer , userOptions . alias )
67
+ } = getVBindVariableListByPath ( descriptor , transId , CSSFileModuleMap , isServer , userOptions . alias )
63
68
const variableName = getVariable ( descriptor )
64
- vbindVariableList . set ( id , matchVariable ( vbindVariableListByPath , variableName ) )
69
+ vbindVariableList . set ( transId , matchVariable ( vbindVariableListByPath , variableName ) )
65
70
66
71
// vite、rollup、esbuild 打包生效
67
- if ( ! isServer && framework === 'webpack' && framework = == 'rspack' )
72
+ if ( ! isServer && framework !== 'webpack' && framework ! == 'rspack' )
68
73
mgcStr = injectCssOnBuild ( mgcStr , injectCSSContent , descriptor )
69
74
}
70
75
}
@@ -111,14 +116,15 @@ const unplugin = createUnplugin<Options>(
111
116
return filter ( id )
112
117
} ,
113
118
async transform ( code : string , id : string ) {
119
+ let transId = transformSymbol ( id )
114
120
let mgcStr = new MagicString ( code )
115
121
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
116
122
try {
117
123
function injectCSSVarsFn ( idKey : string ) {
118
124
const parseRes = parserCompiledSfc ( code )
119
125
const injectRes = injectCSSVars ( vbindVariableList . get ( idKey ) , isScriptSetup , parseRes , mgcStr )
120
126
mgcStr = injectRes . mgcStr
121
- injectRes . vbindVariableList && vbindVariableList . set ( id , injectRes . vbindVariableList )
127
+ injectRes . vbindVariableList && vbindVariableList . set ( transId , injectRes . vbindVariableList )
122
128
isHmring = false
123
129
}
124
130
@@ -128,13 +134,12 @@ const unplugin = createUnplugin<Options>(
128
134
if ( framework === 'vite'
129
135
|| framework === 'rollup'
130
136
|| 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' ) ) {
135
140
mgcStr = injectCssOnServer (
136
141
mgcStr ,
137
- vbindVariableList . get ( id . split ( '?vue' ) [ 0 ] ) ,
142
+ vbindVariableList . get ( transId . split ( '?vue' ) [ 0 ] ) ,
138
143
isHmring ,
139
144
)
140
145
}
@@ -143,25 +148,22 @@ const unplugin = createUnplugin<Options>(
143
148
144
149
// webpack dev 和 build 都回进入这里
145
150
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 ]
154
153
injectCSSVarsFn ( transId )
155
154
}
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 )
157
159
if ( cssFMM && cssFMM . sfcPath && cssFMM . sfcPath . size > 0 ) {
158
160
const sfcPathIdList = setTArray ( cssFMM . sfcPath )
159
161
sfcPathIdList . forEach ( ( v ) => {
160
162
mgcStr = injectCssOnServer ( mgcStr , vbindVariableList . get ( v ) , isHmring )
161
163
} )
162
164
}
163
165
}
164
-
166
+ // console.log(mgcStr.toString())
165
167
return {
166
168
code : mgcStr . toString ( ) ,
167
169
get map ( ) {
0 commit comments