@@ -19,8 +19,6 @@ import { cachedStats } from "./runner";
1919
2020type TTarget = RspackOptions [ "target" ] ;
2121
22- const MAX_COMPILER_INDEX = 100 ;
23-
2422function createCacheProcessor (
2523 name : string ,
2624 src : string ,
@@ -35,16 +33,12 @@ function createCacheProcessor(
3533 } ,
3634 config : async ( context : ITestContext ) => {
3735 const compiler = context . getCompiler ( ) ;
38- let options = defaultOptions ( context , temp , target ) ;
39- options = await config (
36+ const options = await generateOptions (
4037 context ,
41- name ,
42- [ "rspack.config.js" , "webpack.config.js" ] . map ( i =>
43- path . resolve ( temp , i )
44- ) ,
45- options
38+ temp ,
39+ target ,
40+ updatePlugin
4641 ) ;
47- overrideOptions ( options , temp , target , updatePlugin ) ;
4842 compiler . setOptions ( options ) ;
4943 } ,
5044 compiler : async ( context : ITestContext ) => {
@@ -111,12 +105,13 @@ export function createCacheCase(
111105
112106const creators : Map < TTarget , BasicCaseCreator > = new Map ( ) ;
113107
114- function defaultOptions (
108+ async function generateOptions (
115109 context : ITestContext ,
116110 temp : string ,
117- target : TTarget
118- ) : RspackOptions {
119- const options = {
111+ target : TTarget ,
112+ updatePlugin : HotUpdatePlugin
113+ ) : Promise < RspackOptions > {
114+ let options = {
120115 context : temp ,
121116 mode : "production" ,
122117 cache : true ,
@@ -147,15 +142,14 @@ function defaultOptions(
147142 options . plugins ??= [ ] ;
148143 options . plugins . push ( new rspack . HotModuleReplacementPlugin ( ) ) ;
149144
150- return options ;
151- }
145+ options = await config (
146+ context ,
147+ "cacheCase" ,
148+ [ "rspack.config.js" , "webpack.config.js" ] . map ( i => path . resolve ( temp , i ) ) ,
149+ options
150+ ) ;
152151
153- function overrideOptions (
154- options : RspackOptions ,
155- temp : string ,
156- target : TTarget ,
157- updatePlugin : HotUpdatePlugin
158- ) : void {
152+ // overwrite
159153 if ( ! options . entry ) {
160154 options . entry = "./index.js" ;
161155 }
@@ -176,6 +170,8 @@ function overrideOptions(
176170 level : "error"
177171 } ;
178172 }
173+
174+ return options ;
179175}
180176
181177function findBundle (
@@ -245,6 +241,7 @@ function createRunner(
245241 moduleScope . COMPILER_INDEX = compilerIndex ;
246242 moduleScope . NEXT_HMR = nextHmr ;
247243 moduleScope . NEXT_START = nextStart ;
244+ moduleScope . NEXT_MOVE_DIR_START = nextMoveDirStart ;
248245 return moduleScope ;
249246 }
250247 } ,
@@ -253,34 +250,35 @@ function createRunner(
253250 compilerOptions : options
254251 } ) ;
255252 } ;
256- const nextHmr = async ( m : any , options ?: any ) : Promise < StatsCompilation > => {
257- await updatePlugin . goNext ( ) ;
258- const stats = await compiler . build ( ) ;
259- if ( ! stats ) {
260- throw new Error ( "Should generate stats during build" ) ;
261- }
262- const jsonStats = stats . toJson ( {
263- // errorDetails: true
264- } ) ;
265- const compilerOptions = compiler . getOptions ( ) ;
266253
254+ const checkStats = async ( stats : StatsCompilation ) => {
255+ const compilerOptions = compiler . getOptions ( ) ;
267256 const updateIndex = updatePlugin . getUpdateIndex ( ) ;
268257 await checkArrayExpectation (
269258 source ,
270- jsonStats ,
259+ stats ,
271260 "error" ,
272261 `errors${ updateIndex } ` ,
273262 "Error" ,
274263 compilerOptions
275264 ) ;
276265 await checkArrayExpectation (
277266 source ,
278- jsonStats ,
267+ stats ,
279268 "warning" ,
280269 `warnings${ updateIndex } ` ,
281270 "Warning" ,
282271 compilerOptions
283272 ) ;
273+ } ;
274+
275+ const nextHmr = async ( m : any , options ?: any ) : Promise < StatsCompilation > => {
276+ await updatePlugin . goNext ( ) ;
277+ const stats = await compiler . build ( ) ;
278+ const jsonStats = stats . toJson ( {
279+ // errorDetails: true
280+ } ) ;
281+ await checkStats ( jsonStats ) ;
284282
285283 const updatedModules = await m . hot . check ( options || true ) ;
286284 if ( ! updatedModules ) {
@@ -292,43 +290,48 @@ function createRunner(
292290
293291 const nextStart = async ( ) : Promise < StatsCompilation > => {
294292 await compiler . close ( ) ;
295- compiler . createCompiler ( ) ;
293+
296294 await updatePlugin . goNext ( ) ;
295+ compilerIndex ++ ;
296+
297+ compiler . createCompiler ( ) ;
297298 const stats = await compiler . build ( ) ;
298- if ( ! stats ) {
299- throw new Error ( "Should generate stats during build" ) ;
300- }
301299 const jsonStats = stats . toJson ( {
302300 // errorDetails: true
303301 } ) ;
304- const compilerOptions = compiler . getOptions ( ) ;
302+ await checkStats ( jsonStats ) ;
305303
306- const updateIndex = updatePlugin . getUpdateIndex ( ) ;
307- await checkArrayExpectation (
308- source ,
309- jsonStats ,
310- "error" ,
311- `errors${ updateIndex } ` ,
312- "Error" ,
313- compilerOptions
314- ) ;
315- await checkArrayExpectation (
316- source ,
317- jsonStats ,
318- "warning" ,
319- `warnings${ updateIndex } ` ,
320- "Warning" ,
321- compilerOptions
304+ env . it ( `NEXT_START run with compilerIndex==${ compilerIndex } ` , async ( ) => {
305+ return getWebRunner ( ) . run ( file ) ;
306+ } ) ;
307+ return jsonStats ;
308+ } ;
309+
310+ const nextMoveDirStart = async ( ) : Promise < StatsCompilation > => {
311+ await compiler . close ( ) ;
312+
313+ const tempDir = await updatePlugin . moveTempDir ( ) ;
314+ const options = await generateOptions (
315+ context ,
316+ tempDir ,
317+ compiler . getOptions ( ) . target ,
318+ updatePlugin
322319 ) ;
320+ compiler . setOptions ( options ) ;
321+ await updatePlugin . goNext ( ) ;
322+ compilerIndex ++ ;
323+
324+ compiler . createCompiler ( ) ;
325+ const stats = await compiler . build ( ) ;
326+ const jsonStats = stats . toJson ( {
327+ // errorDetails: true
328+ } ) ;
329+
330+ await checkStats ( jsonStats ) ;
331+
323332 env . it (
324- `NEXT_START run with compilerIndex==${ compilerIndex + 1 } ` ,
333+ `NEXT_MOVE_DIR_START run with compilerIndex==${ compilerIndex } ` ,
325334 async ( ) => {
326- if ( compilerIndex > MAX_COMPILER_INDEX ) {
327- throw new Error (
328- "NEXT_START has been called more than the maximum times"
329- ) ;
330- }
331- compilerIndex ++ ;
332335 return getWebRunner ( ) . run ( file ) ;
333336 }
334337 ) ;
0 commit comments