11import fs from 'node:fs' ;
22import path from 'node:path' ;
3- import type { Server } from '@callstack/repack-dev-server' ;
3+ import type { SendProgress , Server } from '@callstack/repack-dev-server' ;
44import { rspack } from '@rspack/core' ;
55import type {
66 MultiCompiler ,
@@ -22,6 +22,7 @@ export class Compiler {
2222 assetsCache : Record < string , Record < string , CompilerAsset > | undefined > = { } ;
2323 statsCache : Record < string , StatsCompilation | undefined > = { } ;
2424 resolvers : Record < string , Array < ( error ?: Error ) => void > > = { } ;
25+ progressSenders : Record < string , SendProgress [ ] > = { } ;
2526 isCompilationInProgress = false ;
2627 // late-init
2728 devServerContext ! : Server . DelegateContext ;
@@ -31,6 +32,28 @@ export class Compiler {
3132 private reporter : Reporter ,
3233 private rootDir : string
3334 ) {
35+ const handler = ( platform : string , value : number ) => {
36+ const percentage = Math . floor ( value * 100 ) ;
37+ this . progressSenders [ platform ] ?. forEach ( ( sendProgress ) => {
38+ sendProgress ( { completed : percentage , total : 100 } ) ;
39+ } ) ;
40+
41+ reporter . process ( {
42+ issuer : 'DevServer' ,
43+ message : [ { progress : { value, platform } } ] ,
44+ timestamp : Date . now ( ) ,
45+ type : 'progress' ,
46+ } ) ;
47+ } ;
48+
49+ configs . forEach ( ( config ) => {
50+ config . plugins ?. push (
51+ new rspack . ProgressPlugin ( ( percentage ) =>
52+ handler ( config . name as string , percentage )
53+ )
54+ ) ;
55+ } ) ;
56+
3457 this . compiler = rspack . rspack ( configs ) ;
3558 this . platforms = configs . map ( ( config ) => config . name as string ) ;
3659 this . filesystem = memfs . createFsFromVolume ( new memfs . Volume ( ) ) ;
@@ -53,6 +76,19 @@ export class Compiler {
5376 this . resolvers [ platform ] = [ ] ;
5477 }
5578
79+ private addProgressSender ( platform : string , callback ?: SendProgress ) {
80+ if ( ! callback ) return ;
81+ this . progressSenders [ platform ] = this . progressSenders [ platform ] ?? [ ] ;
82+ this . progressSenders [ platform ] . push ( callback ) ;
83+ }
84+
85+ private removeProgressSender ( platform : string , callback ?: SendProgress ) {
86+ if ( ! callback ) return ;
87+ this . progressSenders [ platform ] = this . progressSenders [ platform ] . filter (
88+ ( item ) => item !== callback
89+ ) ;
90+ }
91+
5692 setDevServerContext ( ctx : Server . DelegateContext ) {
5793 this . devServerContext = ctx ;
5894 }
@@ -190,14 +226,21 @@ export class Compiler {
190226 } ) ;
191227 }
192228
193- async getAsset ( filename : string , platform : string ) : Promise < CompilerAsset > {
229+ async getAsset (
230+ filename : string ,
231+ platform : string ,
232+ sendProgress ?: SendProgress
233+ ) : Promise < CompilerAsset > {
194234 // Return file from assetsCache if exists
195235 const fileFromCache = this . assetsCache [ platform ] ?. [ filename ] ;
196236 if ( fileFromCache ) {
197237 return fileFromCache ;
198238 }
199239
240+ this . addProgressSender ( platform , sendProgress ) ;
241+
200242 if ( ! this . isCompilationInProgress ) {
243+ this . removeProgressSender ( platform , sendProgress ) ;
201244 return Promise . reject (
202245 new Error (
203246 `File ${ filename } for ${ platform } not found in compilation assets (no compilation in progress)`
@@ -209,6 +252,7 @@ export class Compiler {
209252 // Add new resolver to be executed when compilation is finished
210253 this . resolvers [ platform ] = ( this . resolvers [ platform ] ?? [ ] ) . concat (
211254 ( error ?: Error ) => {
255+ this . removeProgressSender ( platform , sendProgress ) ;
212256 if ( error ) {
213257 reject ( error ) ;
214258 } else {
@@ -230,13 +274,14 @@ export class Compiler {
230274
231275 async getSource (
232276 filename : string ,
233- platform : string | undefined
277+ platform : string | undefined ,
278+ sendProgress ?: SendProgress
234279 ) : Promise < string | Buffer > {
235280 if ( DEV_SERVER_ASSET_TYPES . test ( filename ) ) {
236281 if ( ! platform ) {
237282 throw new CLIError ( `Cannot detect platform for ${ filename } ` ) ;
238283 }
239- const asset = await this . getAsset ( filename , platform ) ;
284+ const asset = await this . getAsset ( filename , platform , sendProgress ) ;
240285 return asset . data ;
241286 }
242287
0 commit comments