Skip to content

HMR in app code

Pages, layouts and components of every UI library update in place under hamur dev, and each adapter wires that up for you (the table below). This page is for the code that is yours: a store, a timer, a socket, a canvas, a module that holds state or a side effect. It uses import.meta.hot, the hot module replacement API of Bun's dev server, in the subset Bun 1.4.4 implements.

The API Bun 1.4.4 has

CallStatusWhat it does
import.meta.hot.accept()worksthe module can be replaced by running its file again; Bun patches the modules that import it
import.meta.hot.accept(callback)worksVite's form: the callback gets the new module (undefined after a syntax error) and importers are not patched. Bun recommends the form without an argument
import.meta.hot.accept("./dep", callback), accept(["./a", "./b"], callback)worksaccepts an update of a dependency; the callback gets the new module, or for a list an array with the updated module at its index and undefined for the others
import.meta.hot.dataworksan object that survives from one version of the module to the next. Writing to it marks the module as accepting itself
import.meta.hot.dispose(callback)workscalled with data just before the module is replaced, and when it is detached. A returned promise delays the replacement; all dispose callbacks run in parallel
import.meta.hot.on(event, callback), offworkslistens for bun:beforeUpdate, bun:afterUpdate, bun:beforeFullReload, bun:beforePrune, bun:invalidate, bun:error, bun:ws:connect and bun:ws:disconnect; a vite: prefix works for the same names. Bun removes a module's listeners when it replaces the module
import.meta.hot.decline()no-opthere for Vite's sake
import.meta.hot.prune(callback)not usableBun does not call the callback yet, and its types leave it out
import.meta.hot.invalidate()not implemented
import.meta.hot.send()not implementeduse the dev channel

The source is devserver.d.ts and docs/bundler/hot-reloading.mdx of bun-types 1.4.4. When no module of the chain accepts an update, the page reloads and the console lists the files Bun checked.

Guard it, and call it in full

import.meta.hot exists only in the client bundle that hamur dev serves, and a production build leaves it out, so write the guard Bun's own types suggest:

ts
const data: { startedAt?: number } = import.meta.hot ? import.meta.hot.data : {}
const startedAt = (data.startedAt ??= Date.now())

const timer = setInterval(() => {
  document.title = `up ${Math.round((Date.now() - startedAt) / 1000)} s`
}, 1000)

if (import.meta.hot) {
  import.meta.hot.dispose(() => clearInterval(timer))
}

export const started = startedAt

Hamur proves the guarded form: the conformance fixtures of the six libraries hold the module hot-dispose.client.ts, which calls import.meta.hot.dispose behind the guard, and the production-client check (K21) finds the module in a built chunk with no import.meta.hot in it, or in any other .js of .output/public (tests/conformance/conformance.test.ts, "K21"; tests/prod-markers.test.ts looks for import.meta.hot in every client chunk of the six fixtures). Bun's reference says it also removes unguarded calls in production; Hamur does not test that, so keep the guard.

Bun needs to see the whole phrase import.meta.hot.<name> to remove it. These forms do not work:

ts
const hot = import.meta.hot
hot.accept()

const meta = import.meta
meta.hot.accept()

doSomething(import.meta.hot.dispose)

const data = import.meta.hot ? import.meta.hot.data : {} and import.meta.hot.data.timer are fine: Hamur's own dev client reads data in both ways, and the sample above uses the first. Do not pass import.meta.hot.data to a function: Bun's reference says that works, but Bun 1.4.4 throws, and no Hamur code does it. Hamur's generated adapter code reads and writes import.meta.hot.data.<key> itself.

State and side effects

Keep what must survive an edit under a key of data, and give back what the module started in dispose. In production Bun inlines data as {}, so it holds no state there; it is for dev only, and ??= on it folds away.

In the sample, data.startedAt is set on the first run and read by every later version, so the uptime in the title survives an edit. The timer is not kept: dispose stops it just before the module is replaced and the new version starts its own, so there is never more than one. Without the dispose line, each edit would leave one more timer running.

Accepting a dependency by hand is rarely needed, since the adapters accept pages, layouts and components. When a module you wrote holds something other modules read, accept the dependency's update where it is used:

ts
import { label } from "./hmr-label.ts"

export function paint(root: HTMLElement): void {
  root.textContent = label()
}

if (import.meta.hot) {
  import.meta.hot.accept("./hmr-label.ts", (updated) => {
    if (updated) document.body.dataset.label = updated.label()
  })
}
ts
export const label = (): string => "Hamur"

dispose is not called on a route navigation or when the tab closes, so it is no place for cleanup that must always happen.

What the adapters do

LibraryAn edit of a page, layout or componentCSS edit
ReactFast Refresh: component state is keptthe stylesheet is swapped
Preacthook state is kept; a remount happens only when the module's hook list changesswapped
Solidsignal and store values are carried into the new render; a remount when the module's createSignal and createStore calls changeswapped
Svelte$state is kept (Svelte's own hmr); a fresh component when the $state declarations changeswapped
Vuea template edit re-renders and keeps state; a script edit reloads the componentswapped
htmlthe edited module is imported again and the current tree is rendered again into the root, with no document requestswapped

Editing a .server.ts loader re-runs the loader for the current route and updates the page without a reload. tests/browser/hmr.test.ts and tests/browser/hmr-state.test.ts check it in a browser.

devServer.hmr: false turns it all off: Bun's hot replacement stops, the adapters emit no hot-update code, and every change to a file the browser loads reloads the page. Hamur's own dev modules read import.meta.hot only behind the guard, so they run either way; whether Bun defines import.meta.hot at all when hmr is false was not measured, which is one more reason for your code to guard it.

Known limits of Bun 1.4.4

  • import.meta.hot.send() and invalidate() do not exist, and prune() never calls back.
  • A module that calls import.meta.hot.on(...) and is itself replaced throws TypeError: l.onDispose is not iterable inside Bun's HMR runtime, and the page reloads. Register on listeners in a module that you do not edit while the page is open, or do without them.
  • Once any plugin's onResolve has matched a module's import, Bun reloads that module on every update instead of replacing it in place and keeping its state (oven-sh/bun#19951). Give an onResolve a filter that names only what it must resolve; see Bun plugins.
  • The import.meta.hot calls must be written out in full, as above.
  • A syntax error gives undefined to an accept callback; check for it.

From Vite

ViteHamur
if (import.meta.hot)the same
import.meta.hot.accept(cb), accept(deps, cb)the same; accept() without an argument patches importers, which Vite cannot do
import.meta.hot.dispose(cb)the same; cb gets data
import.meta.hot.datathe same; writing to it also accepts the module
import.meta.hot.prune(cb)does not call back yet
import.meta.hot.invalidate()not implemented
import.meta.hot.decline()a no-op
import.meta.hot.on("vite:beforeUpdate", cb)on("bun:beforeUpdate", cb); the vite: prefix works
import.meta.hot.on("my-event", cb) and server.ws.send("my-event", data)onDevEvent("<module>:my-event", cb) and useDevChannel().send(...), the dev channel
import.meta.hot.send("my-event", data)sendDevEvent("<module>:my-event", data)
plugin hooks handleHotUpdate and hotUpdatenot run by @hamur/vite-compat; a module uses the dev:change hook
import.meta.hot.accept in a Vite React or Vue pluginthe adapter does it; there is no plugin to add