Skip to content

@ogygia/bake

Bake

@ogygia/bake adds import.meta.og.bake(fn), which runs a function at build time and writes its result into the module as a constant, even in browser code. Imports that only fed the function are dropped, so its server-only dependencies never ship.

On this page

Some values are expensive to compute and never change between requests: a nav tree built from a corpus, a config put together from several files, a feed’s payload. import.meta.og.bake(fn) computes them once, when you build. It runs fn, serializes the result, and rewrites the call to that value. At runtime there’s no function and no work left, only the answer.

The import of $lib/server/nav-builder is gone from the output too, because only the baked function used it. So src/lib/nav.ts can be imported by an island, and the nav builder’s own dependencies never reach the browser.

import { buildNav } from '$lib/server/nav-builder';

export const nav =  import.meta.og
  .bake 
  (() => buildNav());
// becomes, at build: export const nav = { items: [ … the computed tree … ] };

Installation

Install the package, list its plugin beside ogygia(), before sveltekit(), and add its types. The plugin takes no options, and it’s imported from the package root, not /vite.

Without the plugin, a call fails the build with macro_needs_plugin, which names the package and the plugin.

pnpm add @ogygia/bake
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { bake } from '@ogygia/bake';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [ogygia(), bake(), sveltekit()]
});

Types

@ogygia/bake/types declares import.meta.og.bake for TypeScript. Reference it next to ogygia/types in a .d.ts file under src, with no import or export in that file, so the declaration merges into the global one.

The macro is typed bake<T>(fn: () => T | Promise<T>): T: an async function’s result is typed as the value it resolves to, because the build awaits it.

/// <reference types="ogygia/types" />
/// <reference types="@ogygia/bake/types" />

How it works

At build time, the plugin finds each import.meta.og.bake(…) call by parsing the module, not by searching its text, so the same words in a comment or a string are left alone. Then for each module:

  1. It writes a small entry that holds the imports the baked functions use and the functions themselves.
  2. It bundles that entry with Vite, in Node mode. Your $lib and other aliases resolve, and npm packages are bundled in. Only node: modules stay outside.
  3. It runs the bundle and awaits every function, in the order they appear.
  4. It serializes each result with devalue and writes it in place of its call.
  5. It removes each import that only fed a baked function.

It runs after every other macro, since fn may use code those rewrite. In dev it runs too, and an edit to any of your own modules the bake bundled runs it again. Nothing is cached between runs: a bake that reads a file or the clock reads it again each time the module is transformed.

The bundle is written for the run into node_modules/.ogygia, ogygia’s build cache, and deleted when it finishes.

Use cases

bake is the general form of what the loaders do for content and what code and md do for snippets (those three come with @ogygia/content): settle at build time what can’t change between requests, and ship only the result.

Reach for it when the value is the same for every visitor until the next deploy: a nav tree, a list of locales, a parsed config, a lookup table built from a large file. Data that changes between deploys belongs in a load or a remote function.

Package contents

PageWhat it covers
Baked functionsWhat a baked function can use, where calls work, dropped imports, and the build errors.
SerializationWhich values a result can hold, and what fails the build.
@ogygia/bakeThe generated API reference for the plugin.

Moved sections

These sections used to be on this page, when it was Macros’ Build-time values page. Each now lives on Baked functions: