@@ -2,6 +2,8 @@ import { createUnplugin } from 'unplugin'
22import {
33 JSX_TSX_REG , NAME ,
44 SUPPORT_FILE_REG ,
5+ log ,
6+ runAsyncTaskList ,
57 setTArray ,
68 transformSymbol ,
79} from '@unplugin-vue-cssvars/utils'
@@ -97,9 +99,7 @@ const unplugin = createUnplugin<Options>(
9799 if ( res )
98100 mgcStr = res
99101 }
100- // console.log('###### pre transId#######\n', id)
101- // console.log('###### pre mgcStr#######\n', mgcStr.toString())
102- // console.log(vbindVariableList)
102+
103103 return {
104104 code : mgcStr . toString ( ) ,
105105 get map ( ) {
@@ -113,9 +113,6 @@ const unplugin = createUnplugin<Options>(
113113 } catch ( err : unknown ) {
114114 this . error ( `[${ NAME } ] ${ err } ` )
115115 }
116-
117- console . log ( '################## pev' , id )
118- console . log ( mgcStr . toString ( ) )
119116 } ,
120117 vite : {
121118 // Vite plugin
@@ -139,73 +136,63 @@ const unplugin = createUnplugin<Options>(
139136 } ,
140137 webpack ( compiler ) {
141138 // mark webpack hmr
142- let file = ''
139+ let modifiedFile = ''
143140 compiler . hooks . watchRun . tap ( NAME , ( compilation1 ) => {
144- console . log ( 'watchRun' )
145141 if ( compilation1 . modifiedFiles ) {
146- file = transformSymbol ( setTArray ( compilation1 . modifiedFiles ) [ 0 ] as string )
147- if ( SUPPORT_FILE_REG . test ( file ) ) {
142+ modifiedFile = transformSymbol ( setTArray ( compilation1 . modifiedFiles ) [ 0 ] as string )
143+ if ( SUPPORT_FILE_REG . test ( modifiedFile ) ) {
148144 isHMR = true
149145 webpackHMR (
150146 CSSFileModuleMap ,
151147 userOptions ,
152- file ,
148+ modifiedFile ,
153149 )
154150 }
155151 }
156152 } )
157153
158154 compiler . hooks . compilation . tap ( NAME , ( compilation ) => {
159-
160- compilation . hooks . finishModules . tapAsync ( NAME , ( modules , callback ) => {
161- // cache module
162- for ( const value of modules ) {
163- console . log ( '##### finishModules' , modules . size )
164- const resource = transformSymbol ( value . resource )
165- // 只有 script(两个) 只更新 style
166- //只有 第二个 script 更新 style 和 sfc, 但 sfc 会延后一次
167- //只有 第一个 script 只更新 style
168- if ( resource . includes ( '?vue&type=script' ) ) {
169- const transId = 'D:/project-github/unplugin-vue-cssvars/play/webpack/src/App.vue'
170- if ( vbindVariableList . get ( transId ) ) {
171- let ca = cacheWebpackModule . get ( transId )
172- ca = new Set ( )
173- ca . add ( value )
174- cacheWebpackModule . set ( transId , ca )
155+ compilation . hooks . finishModules . tapAsync ( NAME , async ( modules , callback ) => {
156+ if ( isHMR ) {
157+ const needRebuildModules = new Map < string , any > ( )
158+ for ( const value of modules ) {
159+ const resource = transformSymbol ( value . resource )
160+ if ( resource . includes ( '?vue&type=script' ) ) {
161+ const sfcPathKey = resource . split ( '?vue' ) [ 0 ]
162+ if ( CSSFileModuleMap . get ( modifiedFile ) . sfcPath . has ( sfcPathKey ) )
163+ needRebuildModules . set ( sfcPathKey , value )
175164 }
176165 }
177- }
178-
179- if ( isHMR ) {
180- const keyPath = 'D:/project-github/unplugin-vue-cssvars/play/webpack/src/App.vue'
181- const cwm = cacheWebpackModule . get ( keyPath )
182- console . log ( '############### cwm' , cwm . size )
183- //for (const mv of cwm) {
184- compilation . rebuildModule ( [ ...cwm ] [ 0 ] , ( e ) => {
185- console . log ( 'hot updated' )
186- callback ( )
187- if ( e ) {
188- console . log ( e )
189- }
166+ if ( needRebuildModules . size > 0 ) {
167+ const promises = [ ]
168+ for ( const [ key ] of needRebuildModules ) {
169+ // 创建一个 Promise 对象,表示异步操作
170+ const promise = new Promise ( ( resolve , reject ) => {
171+ compilation . rebuildModule ( needRebuildModules . get ( key ) , ( e ) => {
172+ if ( e )
173+ reject ( e )
174+ else
175+ resolve ( )
190176 } )
191- // }
192- } else {
177+ } )
178+ promises . push ( promise )
179+ }
180+ Promise . all ( promises )
181+ . then ( ( ) => {
182+ callback ( )
183+ } )
184+ . catch ( ( e ) => {
185+ log ( 'error' , e )
186+ } )
187+ callback ( )
188+ } else {
193189 callback ( )
194190 }
191+ } else {
192+ callback ( )
193+ }
195194 } )
196-
197195 } )
198- /**
199- * 现在问题是 通过watchRun在finishModuled的钩子里,
200- * 我 rebuildModule另一个模块,但 无法完成整个周期(我觉得 rebuildModule 后应该再次进入finishModuled),
201- * 实现热更新,只能在下次watchRun 时结束,这导致我第一层热更新失效,第二次热更新结果是第一次的
202- */
203- /* compiler.hooks.compilation.tap(NAME, (compilation) => {
204- compilation.hooks.afterOptimizeChunkAssets.tap(NAME, (chunks) => {
205-
206- });
207- });
208- */
209196 } ,
210197 } ,
211198
@@ -267,8 +254,6 @@ const unplugin = createUnplugin<Options>(
267254 }
268255 }
269256
270- console . log ( '################## post' , id )
271- // console.log(mgcStr.toString())
272257 return {
273258 code : mgcStr . toString ( ) ,
274259 get map ( ) {
0 commit comments