@@ -9,6 +9,8 @@ import chokidar from 'chokidar'
9
9
import { createFilter } from '@rollup/pluginutils'
10
10
import * as rspackCore from '@rspack/core'
11
11
import { Volume , createFsFromVolume } from 'memfs'
12
+ import { toArray } from '@pengzhanbo/utils'
13
+ import color from 'picocolors'
12
14
import type { MockOptions } from '../types'
13
15
import { lookupFile , normalizePath } from './utils'
14
16
import { loadFromCode } from './loadFromCode'
@@ -19,8 +21,8 @@ export interface MockCompilerOptions {
19
21
alias ?: Record < string , false | string | ( string | false ) [ ] >
20
22
plugins : RspackPluginInstance [ ]
21
23
cwd ?: string
22
- include : string [ ]
23
- exclude : string [ ]
24
+ include : string | string [ ]
25
+ exclude : string | string [ ]
24
26
}
25
27
26
28
const vfs = createFsFromVolume ( new Volume ( ) )
@@ -38,16 +40,19 @@ export class MockCompiler extends EventEmitter {
38
40
private _mockData : Record < string , MockOptions > = { }
39
41
private fileFilter ! : ( file : string ) => boolean
40
42
43
+ private watchInfo ?: {
44
+ filepath : string
45
+ type : 'add' | 'change' | 'unlink'
46
+ }
47
+
41
48
compiler ?: Compiler | null
42
49
43
50
constructor ( public options : MockCompilerOptions ) {
44
51
super ( )
45
52
this . cwd = options . cwd || process . cwd ( )
46
53
const { include, exclude } = this . options
47
54
48
- this . fileFilter = createFilter ( include , exclude , {
49
- resolve : false ,
50
- } )
55
+ this . fileFilter = createFilter ( include , exclude , { resolve : false } )
51
56
52
57
try {
53
58
const pkg = lookupFile ( this . cwd , [ 'package.json' ] )
@@ -69,22 +74,27 @@ export class MockCompiler extends EventEmitter {
69
74
70
75
this . createCompiler ( async ( err , stats ) => {
71
76
const name = '[rspack:mock]'
72
- if ( err ) {
73
- const error = stats ?. compilation . getLogger ( name ) . error
74
- || ( ( ...args : string [ ] ) => console . error ( name , ...args ) )
77
+ const logError = stats ?. compilation . getLogger ( name ) . error
78
+ || ( ( ...args : string [ ] ) => console . error ( color . red ( name ) , ...args ) )
75
79
76
- error ( err . stack || err )
80
+ if ( err ) {
81
+ logError ( err . stack || err )
77
82
if ( 'details' in err ) {
78
- error ( err . details )
83
+ logError ( err . details )
79
84
}
80
85
return
81
86
}
82
87
83
88
if ( stats ?. hasErrors ( ) ) {
84
- stats . compilation . getLogger ( name ) . error ( stats . toString ( { colors : true } ) )
89
+ const info = stats . toJson ( )
90
+ logError ( info . errors )
85
91
return
86
92
}
87
93
94
+ // if (stats) {
95
+ // console.log('json name', stats.toJson().modules?.map(m => m.name).filter(name => name.startsWith('external')))
96
+ // }
97
+
88
98
const content = vfs . readFileSync ( `/${ this . outputFile } ` , 'utf-8' ) as string
89
99
try {
90
100
const result = await loadFromCode ( {
@@ -94,10 +104,10 @@ export class MockCompiler extends EventEmitter {
94
104
cwd : this . cwd ,
95
105
} )
96
106
this . _mockData = transformMockData ( transformRawData ( result ) )
97
- this . emit ( 'update' )
107
+ this . emit ( 'update' , this . watchInfo || { } )
98
108
}
99
109
catch ( e ) {
100
- console . error ( '[rspack:mock-server]' , e )
110
+ logError ( e )
101
111
}
102
112
} )
103
113
}
@@ -128,7 +138,7 @@ export class MockCompiler extends EventEmitter {
128
138
129
139
watchMockFiles ( ) {
130
140
const { include } = this . options
131
- const [ firstGlob , ...otherGlob ] = include
141
+ const [ firstGlob , ...otherGlob ] = toArray ( include )
132
142
const watcher = ( this . mockWatcher = chokidar . watch ( firstGlob , {
133
143
ignoreInitial : true ,
134
144
cwd : this . cwd ,
@@ -137,11 +147,21 @@ export class MockCompiler extends EventEmitter {
137
147
if ( otherGlob . length > 0 )
138
148
otherGlob . forEach ( glob => watcher . add ( glob ) )
139
149
140
- watcher . on ( 'add' , ( ) => {
141
- this . updateMockEntry ( )
150
+ watcher . on ( 'add' , ( filepath ) => {
151
+ if ( this . fileFilter ( filepath ) ) {
152
+ this . watchInfo = { filepath, type : 'add' }
153
+ this . updateMockEntry ( )
154
+ }
155
+ } )
156
+
157
+ watcher . on ( 'change' , ( filepath ) => {
158
+ if ( this . fileFilter ( filepath ) ) {
159
+ this . watchInfo = { filepath, type : 'change' }
160
+ }
142
161
} )
143
162
144
- watcher . on ( 'unlink' , async ( ) => {
163
+ watcher . on ( 'unlink' , async ( filepath ) => {
164
+ this . watchInfo = { filepath, type : 'unlink' }
145
165
this . updateMockEntry ( )
146
166
} )
147
167
}
@@ -163,14 +183,15 @@ export class MockCompiler extends EventEmitter {
163
183
await fsp . writeFile ( this . entryFile , code , 'utf8' )
164
184
}
165
185
166
- createCompiler ( callback : ( e : Error | null , res ?: rspackCore . Stats ) => void ) {
186
+ createCompiler ( callback : ( e : Error | null , stats ?: rspackCore . Stats ) => void ) {
187
+ const { plugins, alias } = this . options
167
188
const options = resolveRspackOptions ( {
168
189
isEsm : this . moduleType === 'esm' ,
169
190
cwd : this . cwd ,
170
- plugins : this . options . plugins ,
191
+ plugins,
171
192
entryFile : this . entryFile ,
172
193
outputFile : this . outputFile ,
173
- alias : this . options . alias ,
194
+ alias,
174
195
watch : true ,
175
196
} )
176
197
0 commit comments