@@ -14,9 +14,9 @@ import { getVBindVariableListByPath } from './runtime/process-css'
14
14
import { initOption } from './option'
15
15
import { getVariable , matchVariable , parserCompiledSfc } from './parser'
16
16
import {
17
+ injectCSSOnServer ,
17
18
injectCSSVars ,
18
19
injectCssOnBuild ,
19
- injectCSSOnServer ,
20
20
} from './inject'
21
21
import { viteHMR , webpackHMR } from './hmr/hmr'
22
22
import type { MagicStringBase } from 'magic-string-ast'
@@ -52,6 +52,7 @@ const unplugin = createUnplugin<Options>(
52
52
mgcStr ?: MagicStringBase ,
53
53
) {
54
54
const { descriptor } = parse ( code )
55
+ debugger
55
56
const lang = descriptor ?. script ?. lang ?? 'js'
56
57
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
57
58
if ( ! JSX_TSX_REG . test ( `.${ lang } ` ) ) {
@@ -64,11 +65,6 @@ const unplugin = createUnplugin<Options>(
64
65
const variableName = getVariable ( descriptor )
65
66
vbindVariableList . set ( id , matchVariable ( vbindVariableListByPath , variableName ) )
66
67
67
- // wepack 热更新统一
68
- // TODO: webpack 热更新时应该打平
69
- if ( ( id . includes ( 'vue&type=style' ) && framework === 'webpack' && isHMR ) ) {
70
- vbindVariableList . set ( id . split ( '?vue' ) [ 0 ] , matchVariable ( vbindVariableListByPath , variableName ) )
71
- }
72
68
// vite、rollup、esbuild 打包生效
73
69
if ( mgcStr && ! isServer && framework !== 'webpack' && framework !== 'rspack' ) {
74
70
mgcStr = injectCssOnBuild ( mgcStr , injectCSSContent , descriptor )
@@ -85,21 +81,29 @@ const unplugin = createUnplugin<Options>(
85
81
return filter ( id )
86
82
} ,
87
83
async transform ( code : string , id : string ) {
88
- const transId = transformSymbol ( id )
84
+ let transId = transformSymbol ( id )
89
85
let mgcStr = new MagicString ( code )
90
86
try {
91
87
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
92
- // webpack 时 使用 id.includes('vue&type=style') 判断
88
+ // webpack 时 使用 id.includes('? vue&type=style') 判断
93
89
// webpack dev 和 build 都回进入这里
94
- if ( transId . endsWith ( '.vue' )
95
- || ( transId . includes ( 'vue&type=style' ) && framework === 'webpack' ) ) {
96
- cacheCodeWepackHMR = code
97
90
91
+ if ( transId . endsWith ( '.vue' ) ) {
98
92
const res = handleVBindVariable ( code , transId , mgcStr )
99
93
if ( res )
100
94
mgcStr = res
101
95
}
102
96
97
+ if ( ( transId . includes ( '?vue&type=style' ) && isHMR && framework === 'webpack' ) ) {
98
+ cacheCodeWepackHMR = code
99
+ transId = transId . split ( '?vue&type=style' ) [ 0 ]
100
+ const res = handleVBindVariable ( code , transId , mgcStr )
101
+ if ( res )
102
+ mgcStr = res
103
+ }
104
+ // console.log('###### pre transId#######\n', id)
105
+ // console.log('###### pre mgcStr#######\n', mgcStr.toString())
106
+ // console.log(vbindVariableList)
103
107
return {
104
108
code : mgcStr . toString ( ) ,
105
109
get map ( ) {
@@ -136,8 +140,8 @@ const unplugin = createUnplugin<Options>(
136
140
} ,
137
141
webpack ( compiler ) {
138
142
// mark webpack hmr
139
- compiler . hooks . watchRun . tap ( ` ${ NAME } :webpack:watchRun` , ( compilation ) => {
140
- let file = ''
143
+ let file = ''
144
+ compiler . hooks . watchRun . tap ( ` ${ NAME } ` , ( compilation ) => {
141
145
if ( compilation . modifiedFiles ) {
142
146
file = transformSymbol ( setTArray ( compilation . modifiedFiles ) [ 0 ] as string )
143
147
if ( SUPPORT_FILE_REG . test ( file ) ) {
@@ -147,9 +151,10 @@ const unplugin = createUnplugin<Options>(
147
151
userOptions ,
148
152
file ,
149
153
)
154
+ // console.log(CSSFileModuleMap)
150
155
}
151
156
}
152
- compiler . hooks . compilation . tap ( `${ NAME } :webpack:watchRun:compilation` , ( compilation ) => {
157
+ /* compiler.hooks.compilation.tap(`${NAME}:webpack:watchRun:compilation`, (compilation) => {
153
158
compilation.hooks.finishModules.tap(`${NAME}:webpack:watchRun:finishModules`, (modules) => {
154
159
// TODO:
155
160
const keyPath = 'D:/project-github/unplugin-vue-cssvars/play/webpack/src/App.vue'
@@ -168,15 +173,19 @@ const unplugin = createUnplugin<Options>(
168
173
}
169
174
}
170
175
})
171
- } )
176
+ }) */
172
177
} )
173
-
174
- compiler . hooks . compilation . tap ( `${ NAME } :webpack:compilation` , ( compilation ) => {
178
+ compiler . hooks . afterCompile . tap ( `${ NAME } ` , ( compilation ) => {
179
+ if ( compilation . compiler === compiler ) {
180
+ compilation . fileDependencies . add ( '/Users/baiwusanyu/WebstormProjects/unplugin-vue-cssvars/play/webpack/src/App.vue' )
181
+ }
182
+ } )
183
+ /* compiler.hooks.compilation.tap(`${NAME}:webpack:compilation`, (compilation) => {
175
184
compilation.hooks.finishModules.tap(`${NAME}:webpack:finishModules`, (modules) => {
176
185
// cache module
177
186
for (const value of modules) {
178
187
const resource = transformSymbol(value.resource)
179
- if ( resource . includes ( 'vue&type=script' ) ) {
188
+ if (resource.includes('? vue&type=script')) {
180
189
const transId = resource.split('?vue&type=script')[0]
181
190
if (vbindVariableList.get(transId)){
182
191
let ca = cacheWebpackModule.get(transId)
@@ -189,7 +198,7 @@ const unplugin = createUnplugin<Options>(
189
198
}
190
199
}
191
200
})
192
- } )
201
+ }) */
193
202
} ,
194
203
} ,
195
204
@@ -222,7 +231,7 @@ const unplugin = createUnplugin<Options>(
222
231
if ( transId . endsWith ( '.vue' ) )
223
232
injectCSSVarsFn ( transId )
224
233
// inject css code
225
- if ( transId . includes ( 'vue&type=style' ) ) {
234
+ if ( transId . includes ( '? vue&type=style' ) ) {
226
235
mgcStr = injectCSSOnServer (
227
236
mgcStr ,
228
237
vbindVariableList . get ( transId . split ( '?vue' ) [ 0 ] ) ,
@@ -234,7 +243,7 @@ const unplugin = createUnplugin<Options>(
234
243
235
244
// webpack dev 和 build 都回进入这里
236
245
if ( framework === 'webpack' ) {
237
- if ( transId . includes ( 'vue&type=script' ) ) {
246
+ if ( transId . includes ( '? vue&type=script' ) ) {
238
247
transId = transId . split ( '?vue&type=script' ) [ 0 ]
239
248
injectCSSVarsFn ( transId )
240
249
}
@@ -250,7 +259,7 @@ const unplugin = createUnplugin<Options>(
250
259
} )
251
260
}
252
261
}
253
- //console.log(mgcStr.toString())
262
+
254
263
return {
255
264
code : mgcStr . toString ( ) ,
256
265
get map ( ) {
0 commit comments