@@ -153,73 +153,59 @@ const unplugin = createUnplugin<Options>(
153153 )
154154 }
155155 }
156- let registered = false
157- compilation1 . hooks . compilation . tap ( NAME , ( compilation2 ) => {
158- if ( ! registered ) {
159- compilation2 . hooks . finishModules . tap ( NAME , ( ) => {
160- console . log ( 'watchRun finishModules' , isHMR )
161- const keyPath = 'D:/project-github/unplugin-vue-cssvars/play/webpack/src/App.vue'
162- // rebuild module to hmr
163- if ( isHMR ) {
164- debugger
165- const cwm = cacheWebpackModule . get ( keyPath )
166- console . log ( '############### cwm' , cwm . size )
167- for ( const mv of cwm ) {
168- console . log ( compilation1 )
169- debugger
170- compilation2 . rebuildModule ( mv , ( e ) => {
171- console . log ( compiler )
172- debugger
173- if ( e ) {
174- debugger
175- console . log ( e )
176- return
177- }
178- console . log ( 'hot updated' )
179- } )
180- }
181- }
182- } )
183- registered = true
184- }
185- } )
186156 } )
187157
188158 compiler . hooks . compilation . tap ( NAME , ( compilation ) => {
189- compilation . hooks . finishModules . tap ( NAME , ( modules ) => {
159+
160+ compilation . hooks . finishModules . tapAsync ( NAME , ( modules , callback ) => {
190161 // cache module
191162 for ( const value of modules ) {
163+ console . log ( '##### finishModules' , modules . size )
192164 const resource = transformSymbol ( value . resource )
193- console . log ( resource )
194165 // 只有 script(两个) 只更新 style
195166 //只有 第二个 script 更新 style 和 sfc, 但 sfc 会延后一次
196167 //只有 第一个 script 只更新 style
197168 if ( resource . includes ( '?vue&type=script' ) ) {
198169 const transId = 'D:/project-github/unplugin-vue-cssvars/play/webpack/src/App.vue'
199170 if ( vbindVariableList . get ( transId ) ) {
200171 let ca = cacheWebpackModule . get ( transId )
201- // if (!ca){
202- ca = new Set ( )
203- // ca.add(value)
204- // cacheWebpackModule.set(transId, ca)
205- // }
172+ ca = new Set ( )
206173 ca . add ( value )
207174 cacheWebpackModule . set ( transId , ca )
208175 }
209176 }
210177 }
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+ }
190+ } )
191+ // }
192+ } else {
193+ callback ( )
194+ }
211195 } )
196+
212197 } )
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) => {
213205
214- compiler . hooks . compilation . tap ( 'MyPlugin' , ( compilation ) => {
215- compilation . hooks . optimizeModules . tap ( 'MyPlugin' , ( modules ) => {
216- const moduleIds = compilation . moduleIds ;
217- for ( const module of modules ) {
218- const moduleId = moduleIds . get ( module ) ;
219- console . log ( moduleId ) ; // 模块的标识符
220- }
221206 });
222207 });
208+ */
223209 } ,
224210 } ,
225211
@@ -282,7 +268,7 @@ const unplugin = createUnplugin<Options>(
282268 }
283269
284270 console . log ( '################## post' , id )
285- console . log ( mgcStr . toString ( ) )
271+ // console.log(mgcStr.toString())
286272 return {
287273 code : mgcStr . toString ( ) ,
288274 get map ( ) {
0 commit comments