From 8d29505643f7bc7fca66b604fdcde02213baa130 Mon Sep 17 00:00:00 2001 From: Brenley Dueck Date: Fri, 13 Nov 2026 21:36:22 -0600 Subject: [PATCH] feat(vite): hot-swap a dependency's components under vite dev A dependency compiles to a factory over the app, so Solid's refresh pass finds no component to swap, and the entry's watch on every module of its program reloads the page on any edit. The front end now wraps the factory with the runtime's new hot entry, which re-runs a new version against the same app with its module-level calls answered from the first run, and its hotUpdate leaves the entry out when no other module compiles differently and the text outside the components' bodies is unchanged. --- CHANGELOG.md | 4 ++ compiler/README.md | 10 +++- compiler/src/app.rs | 2 +- compiler/src/emit.rs | 50 ++++++++++++++++++- compiler/test/vite.mjs | 66 +++++++++++++++++++++++- compiler/vite.mjs | 56 +++++++++++++++++++-- ref/compile/rt-hot.ts | 111 +++++++++++++++++++++++++++++++++++++++++ runtime/build.mjs | 6 +-- runtime/package.json | 4 ++ 9 files changed, 297 insertions(+), 12 deletions(-) create mode 100644 ref/compile/rt-hot.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index e4b64a8b..806ef72f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,10 @@ - `props` in a component declared without props (`component C()`) is `UNBOUND` (RG-4b); it read `undefined`. - `guide/async.md`: "One store over the server"; the `GUESS_WHILE_LOADING` advice says what holds when a selection changes while a guess is out (05 DV-22). +- Under `vite dev`, an edit inside a dependency's components re-renders them in place instead of reloading the page: + the module's top-level sources and every other component's state are kept. An edit to the module's top level or to + an entry still reloads. The runtime's new `@reactivescript/runtime/hot` entry does the swap; it needs a + `@solidjs/vite-plugin` with the front end's `hotUpdate` hook. ## 0.3.1 diff --git a/compiler/README.md b/compiler/README.md index 17769fb8..94a2bdd8 100644 --- a/compiler/README.md +++ b/compiler/README.md @@ -88,8 +88,14 @@ What happens to a `.rx` module in Vite: The app mounts a program with `createApp` from `@reactivescript/runtime` (`../runtime`, CP-4a), as the examples do. -Not yet: hot updates of `.rx` modules are untested; server rendering in Vite -needs a request-scoped app for server functions to run in. +Hot updates: with a Solid plugin that has the front end's `hotUpdate` hook, +an edit inside a dependency's components (markup, logic, `\n \n

{said}

\n}\n`; + writeFileSync(panel, source()); + const server = await vite.createServer(config(dir, "hot/main.rx")); await server.listen(0); + await drive("hot (dev)", server.resolvedUrls.local[0], async page => { + const seen = async () => ({ + panel: (await page.textContent(".panel").catch(() => "")).trim(), other: (await page.textContent(".other").catch(() => "")).trim(), + color: await page.$eval(".panel", e => getComputedStyle(e).color).catch(() => ""), kept: await page.evaluate(() => window.__kept === 1).catch(() => false), + said: (await page.textContent(".said").catch(() => "")).trim(), + }); + const until = async (what, want) => { + let got; + 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); } + throw new Error(`${what}: ${JSON.stringify(got)}, not ${JSON.stringify(want)}`); + }; + for (let i = 0; i < 3; i++) await page.click(".panel"); + for (let i = 0; i < 2; i++) await page.click(".other"); + await page.evaluate(() => { window.__kept = 1; }); + await until("before the edits", { panel: "total 3", other: "other 2" }); + // the module-level source and the other module's component state survive; the page is not reloaded + writeFileSync(panel, source({ label: "sum" })); + await until("after a markup edit", { panel: "sum 3", other: "other 2", kept: true }); + writeFileSync(panel, source({ label: "sum", color: "rgb(255, 0, 0)" })); + await until("after a