@@ -53,7 +53,8 @@ function project(name, entry) {
5353 return dir ;
5454}
5555const config = ( dir , entry ) => ( {
56- root : dir , configFile : false , logLevel : "warn" , clearScreen : false ,
56+ // a cache per project: the default, under the nearest package.json, would serve one run's runtime to the next
57+ root : dir , cacheDir : path . join ( dir , ".vite" ) , configFile : false , logLevel : "warn" , clearScreen : false ,
5758 plugins : [ solid ( ) ] ,
5859 server : { fs : { allow : [ repo , path . dirname ( deps ) , path . resolve ( repo , "../solid-next" ) , pluginDir ] } } ,
5960 build : { outDir : "dist" , sourcemap : true , emptyOutDir : true } ,
@@ -145,6 +146,69 @@ if (!filter || "styles".includes(filter)) { // CSS-4 under the dev server: a d
145146 } ) ;
146147 await server . close ( ) ;
147148}
149+ if ( ! filter || "hot" . includes ( filter ) ) { // a dependency's component edit swaps in place; a top-level or entry edit reloads
150+ const dir = project ( "hot-dev" , "hot/main.rx" ) ;
151+ mkdirSync ( path . join ( dir , "src/hot" ) ) ;
152+ const main = path . join ( dir , "src/hot/main.rx" ) , panel = path . join ( dir , "src/hot/panel.rx" ) ;
153+ writeFileSync ( main , `import { Panel } from "./panel.rx"\nimport { Other } from "./other.rx"\nimport { Tick } from "./tick.rx"\n\nexport default component Main() {\n <main><Panel /><Other /><Tick /></main>\n}\n` ) ;
154+ writeFileSync ( path . join ( dir , "src/hot/other.rx" ) , `export component Other() {\n source n := 0\n <button class="other" onClick={() => { n := n + 1 }}>other {n}</button>\n}\n` ) ;
155+ // a top level that calls into JS: a replay would call it again, so its module always reloads
156+ const tick = path . join ( dir , "src/hot/tick.rx" ) ;
157+ writeFileSync ( path . join ( dir , "src/hot/clock.mjs" ) , `export const now = () => "one";\n` ) ;
158+ writeFileSync ( tick , `import pure { now } from "./clock"\n\nfirst = now()\n\nexport component Tick() {\n <p class="tick">{first}</p>\n}\n` ) ;
159+ const lib = path . join ( dir , "src/hot/lib.mjs" ) ;
160+ writeFileSync ( lib , `export const word = () => "one";\n` ) ;
161+ const source = ( { top = "" , label = "total" , color = "rgb(0, 0, 255)" , step = 1 } = { } ) =>
162+ `import pure { word } from "./lib"\n\n${ top } source total := 0\n\nexport component Panel() {\n said = word()\n <style>\n .panel { color: ${ color } }\n </style>\n <button class="panel" onClick={() => { total := total + ${ step } }}>${ label } {total}</button>\n <p class="said">{said}</p>\n}\n` ;
163+ writeFileSync ( panel , source ( ) ) ;
164+ const server = await vite . createServer ( config ( dir , "hot/main.rx" ) ) ; await server . listen ( 0 ) ;
165+ await drive ( "hot (dev)" , server . resolvedUrls . local [ 0 ] , async page => {
166+ const seen = async ( ) => ( {
167+ panel : ( await page . textContent ( ".panel" ) . catch ( ( ) => "" ) ) . trim ( ) , other : ( await page . textContent ( ".other" ) . catch ( ( ) => "" ) ) . trim ( ) ,
168+ color : await page . $eval ( ".panel" , e => getComputedStyle ( e ) . color ) . catch ( ( ) => "" ) , kept : await page . evaluate ( ( ) => window . __kept === 1 ) . catch ( ( ) => false ) ,
169+ said : ( await page . textContent ( ".said" ) . catch ( ( ) => "" ) ) . trim ( ) ,
170+ } ) ;
171+ const until = async ( what , want ) => {
172+ let got ;
173+ for ( let i = 0 ; i < 50 ; i ++ ) { got = await seen ( ) ; if ( Object . entries ( want ) . every ( ( [ k , v ] ) => got [ k ] === v ) ) return ; await page . waitForTimeout ( 100 ) ; }
174+ throw new Error ( `${ what } : ${ JSON . stringify ( got ) } , not ${ JSON . stringify ( want ) } ` ) ;
175+ } ;
176+ for ( let i = 0 ; i < 3 ; i ++ ) await page . click ( ".panel" ) ;
177+ for ( let i = 0 ; i < 2 ; i ++ ) await page . click ( ".other" ) ;
178+ await page . evaluate ( ( ) => { window . __kept = 1 ; } ) ;
179+ await until ( "before the edits" , { panel : "total 3" , other : "other 2" } ) ;
180+ // the module-level source and the other module's component state survive; the page is not reloaded
181+ writeFileSync ( panel , source ( { label : "sum" } ) ) ;
182+ await until ( "after a markup edit" , { panel : "sum 3" , other : "other 2" , kept : true } ) ;
183+ writeFileSync ( panel , source ( { label : "sum" , color : "rgb(255, 0, 0)" } ) ) ;
184+ await until ( "after a <style> edit" , { color : "rgb(255, 0, 0)" , panel : "sum 3" , other : "other 2" , kept : true } ) ;
185+ writeFileSync ( panel , source ( { label : "plus" , color : "rgb(255, 0, 0)" , step : 10 } ) ) ;
186+ await until ( "after a logic edit" , { panel : "plus 3" , kept : true } ) ;
187+ await page . click ( ".panel" ) ; await until ( "the new handler" , { panel : "plus 13" , other : "other 2" , kept : true } ) ;
188+ // a JS module the dependency imports changed: what its top level read from it may be stale, so the page reloads
189+ writeFileSync ( lib , `export const word = () => "two";\n` ) ;
190+ await until ( "after an import's edit" , { said : "two" , panel : "plus 0" , kept : false } ) ;
191+ await page . evaluate ( ( ) => { window . __kept = 1 ; } ) ; await page . click ( ".other" ) ;
192+ writeFileSync ( tick , readFileSync ( tick , "utf8" ) . replace ( 'class="tick"' , 'class="tick edited"' ) ) ;
193+ await until ( "after an edit to a module whose top level calls JS" , { other : "other 0" , kept : false } ) ;
194+ await page . evaluate ( ( ) => { window . __kept = 1 ; } ) ;
195+ // the module's top level changed: a reload, with the module's new state
196+ writeFileSync ( panel , source ( { top : "base = 1\n" , label : "plus" , color : "rgb(255, 0, 0)" } ) ) ;
197+ await until ( "after a top-level edit" , { panel : "plus 0" , other : "other 0" , kept : false } ) ;
198+ await page . evaluate ( ( ) => { window . __kept = 1 ; } ) ; await page . click ( ".other" ) ;
199+ writeFileSync ( main , readFileSync ( main , "utf8" ) . replace ( "<main>" , `<main class="edited">` ) ) ;
200+ await until ( "after an entry edit" , { other : "other 0" , kept : false } ) ;
201+ if ( ! await page . $ ( "main.edited" ) ) throw new Error ( "the entry's edit did not reach the page" ) ;
202+ // a dependency that stops compiling, then compiles again, swaps as before
203+ writeFileSync ( panel , source ( { label : "plus" } ) + "\nexport bad = missingName\n" ) ; await page . waitForTimeout ( 800 ) ;
204+ writeFileSync ( panel , source ( { label : "back" } ) ) ;
205+ await until ( "after the fix" , { panel : "back 0" } ) ;
206+ await page . evaluate ( ( ) => { window . __kept = 1 ; } ) ; await page . click ( ".panel" ) ;
207+ writeFileSync ( panel , source ( { label : "again" } ) ) ;
208+ await until ( "after an edit that follows a failed compile" , { panel : "again 1" , kept : true } ) ;
209+ } ) ;
210+ await server . close ( ) ;
211+ }
148212if ( ! filter || "cold" . includes ( filter ) ) { // a dependency asked for before its entry, as Vitest's `fsModuleCache` leaves it
149213 const { default : rx } = await import ( pathToFileURL ( path . join ( pkg , "vite.mjs" ) ) . href ) ;
150214 const root = project ( "cold" , "shop/main.rx" ) , catalog = path . join ( root , "src/shop/catalog.rx" ) ;
0 commit comments