@@ -23,7 +23,6 @@ import type { MagicStringBase } from 'magic-string-ast'
23
23
import type { HmrContext , ResolvedConfig } from 'vite'
24
24
import type { TMatchVariable } from './parser'
25
25
import type { Options } from './types'
26
-
27
26
// TODO: webpack hmr
28
27
const unplugin = createUnplugin < Options > (
29
28
( options : Options = { } , meta ) : any => {
@@ -44,15 +43,13 @@ const unplugin = createUnplugin<Options>(
44
43
let isServer = ! ! userOptions . server
45
44
let isHMR = false
46
45
const cacheWebpackModule = new Map < string , any > ( )
47
- let cacheCodeWepackHMR = ''
48
46
49
47
function handleVBindVariable (
50
48
code : string ,
51
49
id : string ,
52
50
mgcStr ?: MagicStringBase ,
53
51
) {
54
52
const { descriptor } = parse ( code )
55
- debugger
56
53
const lang = descriptor ?. script ?. lang ?? 'js'
57
54
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
58
55
if ( ! JSX_TSX_REG . test ( `.${ lang } ` ) ) {
@@ -95,7 +92,6 @@ const unplugin = createUnplugin<Options>(
95
92
}
96
93
97
94
if ( ( transId . includes ( '?vue&type=style' ) && isHMR && framework === 'webpack' ) ) {
98
- cacheCodeWepackHMR = code
99
95
transId = transId . split ( '?vue&type=style' ) [ 0 ]
100
96
const res = handleVBindVariable ( code , transId , mgcStr )
101
97
if ( res )
@@ -117,6 +113,9 @@ const unplugin = createUnplugin<Options>(
117
113
} catch ( err : unknown ) {
118
114
this . error ( `[${ NAME } ] ${ err } ` )
119
115
}
116
+
117
+ console . log ( '################## pev' , id )
118
+ console . log ( mgcStr . toString ( ) )
120
119
} ,
121
120
vite : {
122
121
// Vite plugin
@@ -141,64 +140,86 @@ const unplugin = createUnplugin<Options>(
141
140
webpack ( compiler ) {
142
141
// mark webpack hmr
143
142
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 )
147
147
if ( SUPPORT_FILE_REG . test ( file ) ) {
148
148
isHMR = true
149
149
webpackHMR (
150
150
CSSFileModuleMap ,
151
151
userOptions ,
152
152
file ,
153
153
)
154
- // console.log(CSSFileModuleMap)
155
154
}
156
155
}
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
+ }
173
181
}
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
+ } )
182
186
} )
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 ) => {
185
190
// cache module
186
191
for ( const value of modules ) {
187
192
const resource = transformSymbol ( value . resource )
193
+ console . log ( resource )
194
+ // 只有 script(两个) 只更新 style
195
+ //只有 第二个 script 更新 style 和 sfc, 但 sfc 会延后一次
196
+ //只有 第一个 script 只更新 style
188
197
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 ) ) {
191
200
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
+ // }
195
206
ca . add ( value )
196
207
cacheWebpackModule . set ( transId , ca )
197
208
}
198
- }
209
+ }
199
210
}
200
211
} )
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
+ } ) ;
202
223
} ,
203
224
} ,
204
225
@@ -260,6 +281,8 @@ const unplugin = createUnplugin<Options>(
260
281
}
261
282
}
262
283
284
+ console . log ( '################## post' , id )
285
+ console . log ( mgcStr . toString ( ) )
263
286
return {
264
287
code : mgcStr . toString ( ) ,
265
288
get map ( ) {
0 commit comments