Skip to content

@ogygia/bake

Serialization

A baked result is written into the module with devalue, so it can hold Dates, Maps, Sets, RegExps, BigInts, typed arrays and repeated or circular references, not only JSON. A function, a symbol or a class instance fails the build.

On this page

A bake’s result becomes source code: the build writes it in place of the call as a JavaScript expression. It does that with devalue’s uneval, the same library Kit uses for load data, so a result can be much more than JSON.

import { loadHolidays } from '$lib/server/holidays';

export const holidays = import.meta.og.bake(async () => {
  const list = await loadHolidays();
  return new Map(list.map((h) => [h.date, h.name]));
});
// becomes, at build: export const holidays = (new Map([["2026-01-01","New Year's Day"], …]));

Supported values

  • JSON values: strings, numbers, booleans, null, plain objects and arrays.
  • undefined, NaN, Infinity, -0 and BigInt.
  • Date, RegExp, Map and Set, nested in any way.
  • Typed arrays such as Uint8Array, and ArrayBuffer.
  • Repeated and circular references: an object that appears twice is one object in the output, and a cycle stays a cycle.
  • A getter is read once, at build, and its value is written.

The result is written as a literal, so it’s an ordinary value of the module, built when the module loads. A frozen object comes back unfrozen.

Errors

A result devalue can’t write fails the build with macro_bake_not_serializable, naming the file, the line and what kind of value it was (Cannot stringify a function, Cannot stringify arbitrary non-POJOs). It doesn’t say where in the result the value sits yet (#2313). That’s:

  • a function, anywhere in the result;
  • a symbol;
  • an instance of your own class, or of any class that isn’t one of the built-ins above;
  • a promise nested inside the result (the top-level promise of an async function is awaited, so it’s fine).

bake produces data. If you need a class instance at run time, bake its data and construct it where it’s used. In the example, baking new Catalog(…) would fail, so the module bakes the entries and builds the Catalog when it loads.

import { Catalog } from '$lib/catalog-class';
import { loadEntries } from '$lib/server/entries';

const entries = import.meta.og.bake(() => loadEntries());
export const catalog = new Catalog(entries);