Skip to content

@ogygia/bake

Baked functions

A baked function runs alone in Node at build time. It can use the module's imports, literals and await, but not the module's other variables or anything that exists only while the app runs. Imports that only fed it are dropped.

On this page

A baked function doesn’t run where you wrote it. The build copies its source, together with the module’s imports, into a bundle of its own and runs that in Node. So what the function can reach is what that bundle carries: the imports, not the rest of the module.

Rules

fn must be self-contained:

  • It’s the call’s only argument, written inline as an arrow function or a function expression. Two arguments, or none, fail with macro_bake_arity. A variable that holds a function fails with macro_bake_not_function, because the build reads the function’s source.
  • It can use the module’s imports and literals, and await freely: async functions and Promise.all work.
  • It must not use the module’s other local variables. The bundle carries the imports, not the surrounding scope, so such a reference fails when it runs.
  • It must not touch what exists only while the app runs: $app/*, a request, the browser.
  • Its result must be serializable. See Serialization.
  • Its own body must be plain JavaScript: no type annotations, as, ! or satisfies inside it. The build runs the function’s source as JavaScript, so in a .ts module TypeScript syntax there fails with macro_bake_eval_failed (#2313). Put typed code in the module you import.

When fn throws, or reaches something it can’t, the build fails with macro_bake_eval_failed, which carries the error.

In the example, the function awaits two imported loaders, and the module ships one constant.

import { readTheme } from '$lib/server/theme';
import { readLocales } from '$lib/server/locales';

export const config = import.meta.og.bake(async () => {
  const [theme, locales] = await Promise.all([
    readTheme(),
    readLocales()
  ]);
  return { theme, locales, builtAt: new Date() };
});

Local variables

A value the function needs goes in through an import, or is written inside the function. Here SIZE is a local of the module, so the first bake fails with macro_bake_eval_failed (SIZE is not defined). The second writes the value inside the function, and the third imports it from a module of its own. Both work.

import { widths } from '$lib/server/widths';
import { MAX_WIDTH } from '$lib/sizes-config';

const SIZE = 1200;

// fails: SIZE is a local of this module, and the bundle doesn't carry it
export const broken = import.meta.og.bake(() => widths(SIZE));

// works: the value is written inside the function
export const ladder = import.meta.og.bake(() => widths(1200));

// works: the value comes in through an import
export const wide = import.meta.og.bake(() =>
  widths(MAX_WIDTH)
);

Imports

The function can import anything Node can run at build time:

  • your own modules, through relative paths, $lib and the other aliases in your Vite config;
  • npm packages, which are bundled into the run;
  • Node’s own modules (node:fs, node:path), which stay outside the bundle and load at run time.

Only the imports the functions name go into the bundle. A .svelte file’s component imports stay out, so a bake in a component’s <script> works too.

import { readFile } from 'node:fs/promises';
import { marked } from 'marked';

export const changelog = import.meta.og.bake(async () =>
  marked.parse(await readFile('CHANGELOG.md', 'utf8'))
);

Dropped imports

Once a function is baked, an import that only fed it is dead code, so bake removes it. In the example above, node:fs/promises and marked disappear from the output: the module ships one string, and the browser never sees either package. An import the rest of the module still uses stays.

The check is careful in one direction. It looks for each imported name anywhere outside the import lines and the baked calls, as a whole word, so a property key or a string with the same name counts as a use. That can keep an import that’s really dead, which the bundler then removes, but it never removes one that’s live.

Where calls work

  • .ts and .js modules, and the <script> blocks of a .svelte component (both the instance and the module script).
  • Your app’s own source, and the files an installed package lists in "ogygia.files" (Packaging). Anywhere else a call fails with macro_outside_app.
  • Not in a component’s markup: a call in a template expression fails with macro_in_markup. Bake in the <script> and use the value in the markup.

A module can hold several calls. They run in one bundle, in the order they appear, and each is replaced by its own result.

In a component, keep the <script> that bakes free of TypeScript syntax: write it without lang="ts", or with no annotations anywhere in it. Today a component whose <script lang="ts"> holds any type annotation is skipped without a word, and its import.meta.og.bake call reaches the server render, which fails with Cannot read properties of undefined (reading 'bake') (#2313). Bake in a .ts module and import the value instead when the component needs types.

<script>
  import { readFile } from 'node:fs/promises';

  const year = import.meta.og.bake(() =>
    new Date().getFullYear()
  );
  const version = import.meta.og.bake(
    async () =>
      JSON.parse(await readFile('package.json', 'utf8')).version
  );
</script>

<footer>© {year} · v{version}</footer>

Dev

In dev the bake runs when Vite first transforms the module, and again whenever one of your own modules the bundle read changes: edit $lib/server/nav-builder.ts and the module that bakes from it gets the new value. A change inside node_modules, or to a file the function only reads from disk (as CHANGELOG.md above), isn’t watched. Restart the dev server, or save the baking module, to run it again.