@@ -14,9 +14,9 @@ import { getVBindVariableListByPath } from './runtime/process-css'
1414import { initOption } from './option'
1515import { getVariable , matchVariable , parserCompiledSfc } from './parser'
1616import {
17+ injectCSSOnServer ,
1718 injectCSSVars ,
1819 injectCssOnBuild ,
19- injectCSSOnServer ,
2020} from './inject'
2121import { viteHMR , webpackHMR } from './hmr/hmr'
2222import type { MagicStringBase } from 'magic-string-ast'
@@ -52,6 +52,7 @@ const unplugin = createUnplugin<Options>(
5252 mgcStr ?: MagicStringBase ,
5353 ) {
5454 const { descriptor } = parse ( code )
55+ debugger
5556 const lang = descriptor ?. script ?. lang ?? 'js'
5657 // ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
5758 if ( ! JSX_TSX_REG . test ( `.${ lang } ` ) ) {
@@ -64,11 +65,6 @@ const unplugin = createUnplugin<Options>(
6465 const variableName = getVariable ( descriptor )
6566 vbindVariableList . set ( id , matchVariable ( vbindVariableListByPath , variableName ) )
6667
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- }
7268 // vite、rollup、esbuild 打包生效
7369 if ( mgcStr && ! isServer && framework !== 'webpack' && framework !== 'rspack' ) {
7470 mgcStr = injectCssOnBuild ( mgcStr , injectCSSContent , descriptor )
@@ -85,21 +81,29 @@ const unplugin = createUnplugin<Options>(
8581 return filter ( id )
8682 } ,
8783 async transform ( code : string , id : string ) {
88- const transId = transformSymbol ( id )
84+ let transId = transformSymbol ( id )
8985 let mgcStr = new MagicString ( code )
9086 try {
9187 // ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
92- // webpack 时 使用 id.includes('vue&type=style') 判断
88+ // webpack 时 使用 id.includes('? vue&type=style') 判断
9389 // webpack dev 和 build 都回进入这里
94- if ( transId . endsWith ( '.vue' )
95- || ( transId . includes ( 'vue&type=style' ) && framework === 'webpack' ) ) {
96- cacheCodeWepackHMR = code
9790
91+ if ( transId . endsWith ( '.vue' ) ) {
9892 const res = handleVBindVariable ( code , transId , mgcStr )
9993 if ( res )
10094 mgcStr = res
10195 }
10296
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)
103107 return {
104108 code : mgcStr . toString ( ) ,
105109 get map ( ) {
@@ -136,8 +140,8 @@ const unplugin = createUnplugin<Options>(
136140 } ,
137141 webpack ( compiler ) {
138142 // 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 ) => {
141145 if ( compilation . modifiedFiles ) {
142146 file = transformSymbol ( setTArray ( compilation . modifiedFiles ) [ 0 ] as string )
143147 if ( SUPPORT_FILE_REG . test ( file ) ) {
@@ -147,9 +151,10 @@ const unplugin = createUnplugin<Options>(
147151 userOptions ,
148152 file ,
149153 )
154+ // console.log(CSSFileModuleMap)
150155 }
151156 }
152- compiler . hooks . compilation . tap ( `${ NAME } :webpack:watchRun:compilation` , ( compilation ) => {
157+ /* compiler.hooks.compilation.tap(`${NAME}:webpack:watchRun:compilation`, (compilation) => {
153158 compilation.hooks.finishModules.tap(`${NAME}:webpack:watchRun:finishModules`, (modules) => {
154159 // TODO:
155160 const keyPath = 'D:/project-github/unplugin-vue-cssvars/play/webpack/src/App.vue'
@@ -168,15 +173,19 @@ const unplugin = createUnplugin<Options>(
168173 }
169174 }
170175 })
171- } )
176+ }) */
172177 } )
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) => {
175184 compilation.hooks.finishModules.tap(`${NAME}:webpack:finishModules`, (modules) => {
176185 // cache module
177186 for (const value of modules) {
178187 const resource = transformSymbol(value.resource)
179- if ( resource . includes ( 'vue&type=script' ) ) {
188+ if (resource.includes('? vue&type=script')) {
180189 const transId = resource.split('?vue&type=script')[0]
181190 if (vbindVariableList.get(transId)){
182191 let ca = cacheWebpackModule.get(transId)
@@ -189,7 +198,7 @@ const unplugin = createUnplugin<Options>(
189198 }
190199 }
191200 })
192- } )
201+ }) */
193202 } ,
194203 } ,
195204
@@ -222,7 +231,7 @@ const unplugin = createUnplugin<Options>(
222231 if ( transId . endsWith ( '.vue' ) )
223232 injectCSSVarsFn ( transId )
224233 // inject css code
225- if ( transId . includes ( 'vue&type=style' ) ) {
234+ if ( transId . includes ( '? vue&type=style' ) ) {
226235 mgcStr = injectCSSOnServer (
227236 mgcStr ,
228237 vbindVariableList . get ( transId . split ( '?vue' ) [ 0 ] ) ,
@@ -234,7 +243,7 @@ const unplugin = createUnplugin<Options>(
234243
235244 // webpack dev 和 build 都回进入这里
236245 if ( framework === 'webpack' ) {
237- if ( transId . includes ( 'vue&type=script' ) ) {
246+ if ( transId . includes ( '? vue&type=script' ) ) {
238247 transId = transId . split ( '?vue&type=script' ) [ 0 ]
239248 injectCSSVarsFn ( transId )
240249 }
@@ -250,7 +259,7 @@ const unplugin = createUnplugin<Options>(
250259 } )
251260 }
252261 }
253- //console.log(mgcStr.toString())
262+
254263 return {
255264 code : mgcStr . toString ( ) ,
256265 get map ( ) {
0 commit comments