@ogygia/core
Build context
A plugin's build(b) runs once when Vite loads its config. Through b it claims route config keys, compiles import.meta.og macros, registers markup formats, adds transforms, names page modules and package files, and turns on the build's events and measurements.
On this page
Some of a plugin’s work happens before any request: a key pages set in their route config, a macro to compile, a file format that holds islands. build(b) is where a plugin asks the build for these. It runs once per build, when Vite loads its config, in plugin order. Each call adds to the build, and nothing happens at run time that the build didn’t set up.
import {
definePlugin,
type OgygiaPlugin
} from '@ogygia/compiler/vite';
export function stamp(): OgygiaPlugin {
return definePlugin({
name: 'ogygia-plugin-stamp',
server: import.meta.resolve('./server.js'),
build(b) {
b.routeConfig('stamp');
if (b.dev) b.events({ lazy: true });
}
});
}Build facts
b tells a plugin where it is building:
| Member | What it is |
|---|---|
root | The app’s root folder, absolute. |
dev | true under vite dev, false under vite build. |
routesDir | Kit’s routes folder (kit.files.routes), absolute. Read it later, in a transform or a hook, not inside build itself. |
regions | The app’s regions as the build knows them so far: names() (island id to component name), hosts() (each module that marks region imports, to the modules its regions are built from), whyStayed(file, importers). Read at call time. |
Route config
b.routeConfig(key) claims a key inside Kit’s own config page option, so a page or layout can configure your plugin without an export Kit doesn’t know:
The build reads each page’s config from its +page and +layout modules (.server included), merges it the way Kit merges config (the layouts from the root down, then the page, later winning), and keeps the claimed keys. Your server half reads the result with ctx.routeConfig(key). Values must be literals: a boolean, a number, a string or null. Nothing is removed from config, since Kit accepts any key there.
A key must be a camelCase name (route_config_key_invalid), and two plugins can’t claim the same one (route_config_key_claimed). A programmatic router answers for its own paths (Route config for routers).
export const config = { stamp: false };Macros
b.macro(name, handler) compiles import.meta.og.<name>(…) calls. The handler gets each module that contains the call, after core’s own macros (wire, $, source, store) ran, and before the region transform sees it. It returns the new code, the same string when nothing changed, or a list of { start, end, text } edits. Edits keep the module’s source map, and a string loses it.
The second argument is the module’s context: id (without its query), rel (relative to the app root), root, the app’s alias entries, markupExts, regions(code) (the parts of a module that hold its code: a whole JS module, or the <script> blocks of a .svelte or markdown file) and watch(file) (transform this module again when file changes in dev).
- Plugin macros run in plugin order.
{ last: true }runs one after every other plugin’s, the waybakeruns code the others may still rewrite. - They run on the app’s own source and on files a package declares in
ogygia.files. A call anywhere else innode_modulesfails the build withmacro_outside_app. - A name must be camelCase and not one of core’s four (
plugin_macro_name_invalid), and only one plugin may claim it (plugin_macro_claimed). The error’s page also listsbakeas reserved, but the code doesn’t reserve it:bakeis taken only when@ogygia/bakeis listed (#2314, item 8). - Type the call for apps with an ambient file your package exports, which adds a method to the global
ImportMetaOginterface. Apps reference it besideogygia/types.
When your plugin isn’t listed, a call to your macro reaches the bundle as written and fails where it runs. Only the first-party macros name their missing plugin (macro_needs_plugin, #2314, item 9).
import {
definePlugin,
type OgygiaPlugin
} from '@ogygia/compiler/vite';
export function buildTime(): OgygiaPlugin {
return definePlugin({
name: 'ogygia-plugin-build-time',
build(b) {
b.macro('buildTime', (code) =>
code.replaceAll(
'import.meta.og.buildTime()',
JSON.stringify(new Date().toISOString())
)
);
}
});
}interface ImportMetaOg {
/** The build's start time, as an ISO string. */
buildTime(): string;
}Markup formats
b.markup(source) registers a file format that becomes Svelte through a preprocessor, such as markdown, so its islands are found. source.extensions lists its extensions. scan({ root, readFile }), when given, reads every file up front and hands the Svelte it becomes to the transform, so its islands are known before chunks are written. modules is an iterable of [absolute path, preprocessed source] pairs the build reads late, to ship each module’s scoped CSS as one client asset.
It returns { transform(source, filename) }, the region transform to call from your preprocessor on the Svelte it made: the rewritten code, or null when nothing changed. @ogygia/content uses it for .svx and .md.
Transforms
b.transform({ stage, filter, transform }) runs a transform on every module filter(id) accepts, at a stage around the region transform: 'source' before it, on the module as written, or 'post' after it. The stage sets the order, not the plugin’s place in the list. Every transform of a stage edits the code it received through t.edit, a MagicString, and the stage emits one result with one source map.
The context also has id (with its query), code, environment (client or ssr), resolve(spec, importer) (as the bundler resolves it now, null when it can’t) and watch(file).
Three members serve a transform that rewrites imports, as @ogygia/barrels does: b.exports.map(file, spec, policy, t) reads a barrel’s names through the app’s export index, b.pinnedImports(code) says which imports carry a region’s identity and must stay as written, and b.stayed(answer) tells the findings why your transform left a module’s imports as they were.
import {
definePlugin,
type OgygiaPlugin
} from '@ogygia/compiler/vite';
export function banner(): OgygiaPlugin {
return definePlugin({
name: 'ogygia-plugin-banner',
build(b) {
b.transform({
stage: 'post',
filter: (id) => id.endsWith('.ts'),
transform(t) {
t.edit.prepend('/* built with ogygia */\n');
}
});
}
});
}Page modules
b.pageModules({ importers, files }) names modules that render pages outside Kit’s routes, as a programmatic router does. Every component such a module imports gets its whole CSS tree linked when it renders, through componentCssHead(). importers lists specifiers: any compiled module that imports one is a page module. files lists absolute paths that are page modules themselves (page_modules_not_absolute otherwise). @ogygia/router uses it.
Inventories
b.prescan({ imports, module, written }) reads the modules the build’s prescan reads (the app’s source, and the files packages declare, used or not) that import one of imports. module({ file, rel, code }) gets each, and written() is called after a vite build wrote its client output. @ogygia/flags builds its flag inventory this way.
b.definers(module, names) names calls that define something for the life of the module making them, such as content() or flag(). In dev each such call in a server module is marked with its module’s run, so a definition deleted from the code goes away when the module reloads.
Events and measurements
b.events({ lazy }) compiles core’s own events in, in the browser and on the server: island wakes, server renders, navigations. With no plugin asking, every emit folds out of the build. lazy: true measures only once someone opens a tool; when several plugins ask, it’s lazy only if all of them say so. b.measure() keeps the build’s measurement data: each island’s detail, chunk contents, server source maps and the module map. @ogygia/devtools asks for both.
Foreign hydration
b.foreignHydrate() keeps the exports that let another app’s page wake this app’s islands, which fragment federation needs. It costs every island about 185 bytes gzipped and a preload, so it’s off unless a plugin asks.
It also says the app’s pages carry regions this build never compiled (another app’s, or ones your browser half places), so the runtime ships every schedule and feature such a region can need. A browser half alone doesn’t say that: the runtime is built for the regions the app compiled.
Seed shaping
b.pageReads(reads) makes your plugin the one that answers which top-level page.data keys a module reads, so each island gets only those in its seed. Only one plugin may (page_reads_claimed); @ogygia/data-shake is the one ogygia ships. Without one, every region that reads the page gets all of page.data, and every server island looks the page up.
Build errors
A call to b that breaks a rule stops the build:
| Code | When |
|---|---|
route_config_key_invalid | A route config key isn’t a camelCase name. |
route_config_key_claimed | Two plugins claim the same route config key. |
plugin_macro_name_invalid | A macro name isn’t camelCase, or is one of core’s. |
plugin_macro_claimed | Two plugins register the same macro. |
page_modules_not_absolute | pageModules({ files }) got a relative path. |
page_reads_claimed | Two plugins call pageReads. |