@@ -23,7 +23,6 @@ import type { MagicStringBase } from 'magic-string-ast'
2323import type { HmrContext , ResolvedConfig } from 'vite'
2424import type { TMatchVariable } from './parser'
2525import type { Options } from './types'
26-
2726// TODO: webpack hmr
2827const unplugin = createUnplugin < Options > (
2928 ( options : Options = { } , meta ) : any => {
@@ -44,15 +43,13 @@ const unplugin = createUnplugin<Options>(
4443 let isServer = ! ! userOptions . server
4544 let isHMR = false
4645 const cacheWebpackModule = new Map < string , any > ( )
47- let cacheCodeWepackHMR = ''
4846
4947 function handleVBindVariable (
5048 code : string ,
5149 id : string ,
5250 mgcStr ?: MagicStringBase ,
5351 ) {
5452 const { descriptor } = parse ( code )
55- debugger
5653 const lang = descriptor ?. script ?. lang ?? 'js'
5754 // ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
5855 if ( ! JSX_TSX_REG . test ( `.${ lang } ` ) ) {
@@ -95,7 +92,6 @@ const unplugin = createUnplugin<Options>(
9592 }
9693
9794 if ( ( transId . includes ( '?vue&type=style' ) && isHMR && framework === 'webpack' ) ) {
98- cacheCodeWepackHMR = code
9995 transId = transId . split ( '?vue&type=style' ) [ 0 ]
10096 const res = handleVBindVariable ( code , transId , mgcStr )
10197 if ( res )
@@ -117,6 +113,9 @@ const unplugin = createUnplugin<Options>(
117113 } catch ( err : unknown ) {
118114 this . error ( `[${ NAME } ] ${ err } ` )
119115 }
116+
117+ console . log ( '################## pev' , id )
118+ console . log ( mgcStr . toString ( ) )
120119 } ,
121120 vite : {
122121 // Vite plugin
@@ -141,64 +140,86 @@ const unplugin = createUnplugin<Options>(
141140 webpack ( compiler ) {
142141 // mark webpack hmr
143142 let file = ''
144- compiler . hooks . watchRun . tap ( `${ NAME } ` , ( compilation ) => {
145- if ( compilation . modifiedFiles ) {
146- file = transformSymbol ( setTArray ( compilation . modifiedFiles ) [ 0 ] as string )
143+ compiler . hooks . watchRun . tap ( NAME , ( compilation1 ) => {
144+ console . log ( 'watchRun' )
145+ if ( compilation1 . modifiedFiles ) {
146+ file = transformSymbol ( setTArray ( compilation1 . modifiedFiles ) [ 0 ] as string )
147147 if ( SUPPORT_FILE_REG . test ( file ) ) {
148148 isHMR = true
149149 webpackHMR (
150150 CSSFileModuleMap ,
151151 userOptions ,
152152 file ,
153153 )
154- // console.log(CSSFileModuleMap)
155154 }
156155 }
157- /* compiler.hooks.compilation.tap(`${NAME}:webpack:watchRun:compilation`, (compilation) => {
158- compilation.hooks.finishModules.tap(`${NAME}:webpack:watchRun:finishModules`, (modules) => {
159- // TODO:
160- const keyPath = 'D:/project-github/unplugin-vue-cssvars/play/webpack/src/App.vue'
161- // rebuild module to hmr
162- if (isHMR){
163- const cwm = cacheWebpackModule.get(keyPath)
164- console.log(cwm.size)
165- for (const mv of cwm) {
166- compilation.rebuildModule(mv, (e) => {
167- if (e) {
168- console.log(e)
169- return
170- }
171- console.log('hot updated')
172- })
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+ }
173181 }
174- }
175- })
176- }) */
177- } )
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+ registered = true
184+ }
185+ } )
182186 } )
183- /* compiler.hooks.compilation.tap(`${NAME}:webpack:compilation`, (compilation) => {
184- compilation.hooks.finishModules.tap(`${NAME}:webpack:finishModules`, (modules) => {
187+
188+ compiler . hooks . compilation . tap ( NAME , ( compilation ) => {
189+ compilation . hooks . finishModules . tap ( NAME , ( modules ) => {
185190 // cache module
186191 for ( const value of modules ) {
187192 const resource = transformSymbol ( value . resource )
193+ console . log ( resource )
194+ // 只有 script(两个) 只更新 style
195+ //只有 第二个 script 更新 style 和 sfc, 但 sfc 会延后一次
196+ //只有 第一个 script 只更新 style
188197 if ( resource . includes ( '?vue&type=script' ) ) {
189- const transId = resource.split('? vue&type=script')[0]
190- if (vbindVariableList.get(transId)){
198+ const transId = 'D:/project-github/unplugin- vue-cssvars/play/webpack/src/App.vue'
199+ if ( vbindVariableList . get ( transId ) ) {
191200 let ca = cacheWebpackModule . get ( transId )
192- if(!ca){
193- ca = new Set()
194- }
201+ // if (!ca){
202+ ca = new Set ( )
203+ // ca.add(value)
204+ // cacheWebpackModule.set(transId, ca)
205+ // }
195206 ca . add ( value )
196207 cacheWebpackModule . set ( transId , ca )
197208 }
198- }
209+ }
199210 }
200211 } )
201- }) */
212+ } )
213+
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+ }
221+ } ) ;
222+ } ) ;
202223 } ,
203224 } ,
204225
@@ -260,6 +281,8 @@ const unplugin = createUnplugin<Options>(
260281 }
261282 }
262283
284+ console . log ( '################## post' , id )
285+ console . log ( mgcStr . toString ( ) )
263286 return {
264287 code : mgcStr . toString ( ) ,
265288 get map ( ) {
0 commit comments