Skip to content

Macros

Macros

import.meta.og.* is one namespace for work the build does once instead of on every request. The Vite plugin rewrites each call before your app runs, so no import.meta.og object exists at runtime.

On this page

Some of what an app does never changes between requests: which markdown files exist, how a class crosses an island boundary, what a code sample looks like once highlighted, the nav tree built from a corpus. That work belongs in the build, and import.meta.og.* is where ogygia puts it.

Everything under import.meta.og is a compile-time construct. It reads like a normal call, but the Vite plugin rewrites it while it builds the module, and there’s no import.meta.og object at runtime. If a call ever reached the browser unrewritten, it would throw on its first line rather than half work.

In the example, bake runs buildNav() at build time and the module ships only its result. bake comes with its own package; Bake covers it.

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

export const nav = import.meta.og.bake(() => buildNav());

Macro list

ogygia compiles these in every app:

MacroWhat it does
wire({ encode, decode })Lets a class’s instances cross into islands as one live object.
$(fn)Lets a function cross into an island, with what it captured. Given a value, checks that it can cross.
store(factory)Registers a store factory, so a store’s own methods survive the crossing.
source(fn)Declares a data source, so stored pages can be invalidated by the function and its arguments.

The others come with a package, and work when its plugin is listed beside ogygia():

MacroPackageWhat it does
bake(fn)@ogygia/bakeRuns fn at build time and inlines its result.
loader.markdown/folder/json/git@ogygia/contentA content source for content({ loader }).
code(source, lang, meta?)@ogygia/contentA highlighted snippet, baked to a static region.
md(text)@ogygia/contentA markdown string, rendered to a static region.
regions(glob)@ogygia/contentA block registry built from a folder.
image(path, options?)@ogygia/imageAn image descriptor for <Img>, made at build.
image.glob(pattern, options?)@ogygia/imageDescriptors for every image a glob matches.
image.card(component, options)@ogygia/imageA social card rendered from a component.

A region isn’t a macro: you mark it on its import (import { Header } from '$lib/ui' with { wake: 'load' }). See Client islands.

A plugin can claim a name of its own under import.meta.og with b.macro(); Macros in Build context describes it.

Types

TypeScript needs to know the macros exist. ogygia/types declares wire, $ and source, and each package declares its own. Add the lines for what you use to a .d.ts file in src.

ogygia/types doesn’t declare store yet. If you use it, declare it yourself in the same file, as the last lines below do. Keep the file free of import and export, so the declaration merges into the global one.

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

interface ImportMetaOg {
   store<F extends (...args: never[]) => unknown>(
    factory: F
  ): F ;
}

Design

These macros live on import.meta because they behave like Vite’s own import.meta.glob: a call that never runs, which the bundler answers at build time. The habits you have for import.meta.glob carry over. You don’t alias the call or pass it around, and what the build has to read is written out literally. One namespace also means one autocomplete entry, and nothing else can collide with it.

A runtime wrapper around import.meta.glob is the pattern these macros replace.

Rules

  • What the build reads is literal. A glob, a language or a repo spec is a plain string. A template literal with ${…} is a runtime value, so it fails the build and names the expression. $ and bake take their function written inline, since the build reads its source.
  • Found by parsing. A call is found by parsing the code, not by searching its text, so the same words in a comment, a string or a code block are never taken for a call.
  • Errors at build time. Misuse fails the build with an [ogygia] message that names the file, the line and the fix.
  • No runtime cost. The rewrite happens once, and what ships is only the result: a source, a region, a registry, a codec key or a constant.

Three errors apply to every package’s macros:

  • A macro whose plugin isn’t listed fails with macro_needs_plugin, which names the package and the plugin.
  • A macro works in your app’s own source, and in the files an installed package lists in "ogygia.files". Anywhere else it fails with macro_outside_app.
  • A package’s macro is compiled in a module and in a component’s <script>, not in a template expression. A call in markup fails with macro_in_markup: call it in the <script> and use the value in the markup. ($ from ogygia is the exception: it works in markup.)

Constraints lists every rule about what can cross into an island.