Skip to content

@ogygia/core

Plugins

definePlugin turns a plugin definition into the Vite plugin an app lists beside ogygia(). It names the plugin, points at its server and browser halves, carries JSON options, says which plugins it needs, and contributes to the build.

On this page

Every feature package beside ogygia is a plugin: content, flags, postrender, federation, the server router, devtools, testing and the rest. (The CLI, the tsdown plugin, insights and ogygia’s own core, compiler and machine aren’t.) A plugin is the value definePlugin returns, a Vite plugin the app lists once beside ogygia(). When Vite loads its config, ogygia() finds every plugin in the list, checks it, runs its build contributions, and imports its server and browser halves into the app’s bundles. For the app’s side of this (installing a package and listing it), see Packages.

Early adopters

This API is internal and can change in any release. Core says what that means for a plugin you write today.

definePlugin

definePlugin comes from @ogygia/compiler/vite, which ogygia/vite re-exports. The package’s ./vite export holds a factory that returns it, so the app calls the factory with its options:

FieldWhat it is
nameThe plugin’s package name, which is its identity. It must resolve from the app (<name>/package.json), and each name can be listed once.
serverThe server half’s location, a file: URL or an absolute path: import.meta.resolve('./server.js'). Optional.
clientThe browser half’s location, the same way. Optional.
optionsPlain JSON, written into the server build. The server half reads it as app.options and ctx.options. The type parameter of definePlugin<Options> types it.
enforce'pre' or 'post': run before or after the other plugins, in every realm. Otherwise, list order.
needsPlugins this one works only beside: { name, call, from } for each.
build(b)Build contributions, with the build context. Runs once, when Vite loads its config.

definePlugin returns a Vite plugin named after the plugin, carrying the definition under Symbol.for('ogygia.plugin'). That symbol is how ogygia() finds it, and since it’s a registered symbol, a second copy of the compiler in one app still recognizes it. The Vite plugin itself only checks that ogygia() is listed too, and fails the build with plugin_without_ogygia when it isn’t.

Vite hooks of its own

A plugin that needs Vite hooks of its own returns an array with its definePlugin(…) and a plain Vite plugin. ogygia() walks nested arrays and promises in plugins, so the app still lists one call. content() and devtools() work this way.

import {
  definePlugin,
  type OgygiaPlugin
} from '@ogygia/compiler/vite';

export interface StampOptions {
  /** The header to set (default `x-stamp`). */
  header?: string;
}

export function stamp(
  options: StampOptions = {}
): OgygiaPlugin<{ header: string }> {
  return definePlugin<{ header: string }>({
    name: 'ogygia-plugin-stamp',
    server: import.meta.resolve('./server.js'),
    options: { header: options.header ?? 'x-stamp' },
    build(b) {
      b.routeConfig('stamp');
    }
  });
}
import { definePlugin } from '@ogygia/compiler/vite';
import type { Plugin } from 'vite';

export function yaml(): Plugin[] {
  return [
    definePlugin({
      name: 'ogygia-plugin-yaml',
      server: import.meta.resolve('./server.js')
    }),
    {
      name: 'ogygia-plugin-yaml:transform',
      transform(code, id) {
        if (!id.endsWith('.yaml')) return null;
        return `export default ${JSON.stringify(code)};`;
      }
    }
  ];
}

Plugin order

Plugins run in one order everywhere: their build contributions, their server hooks on every request, and their browser hooks in the page. It’s the order of the app’s plugins list, with every enforce: 'pre' plugin moved to the front and every enforce: 'post' plugin to the back, each group keeping its list order.

ogygia() and every plugin must come before sveltekit(), since Vite runs hooks in list order and theirs must see a module before Kit’s do. A plugin after sveltekit() fails the build with plugin_after_sveltekit, and a factory listed without being called (flags instead of flags()) with plugin_factory_not_called.

import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { stamp } from 'ogygia-plugin-stamp/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    ogygia(),
    stamp({ header: 'x-built-by' }),
    sveltekit()
  ]
});

Needs

needs names the plugins yours only works beside. An app that lists yours without one of them fails the build with plugin_needs_plugin, which shows the line to add: the call to list, and from where to import it. postrender() needs dataShake() this way.

A need isn’t installed or listed for the app: it’s a check, so the app’s config says what runs.

import {
  definePlugin,
  type OgygiaPlugin
} from '@ogygia/compiler/vite';

export function stored(): OgygiaPlugin {
  return definePlugin({
    name: 'ogygia-plugin-stored',
    server: import.meta.resolve('./server.js'),
    needs: [
      {
        name: '@ogygia/data-shake',
        call: 'dataShake()',
        from: '@ogygia/data-shake/vite'
      }
    ]
  });
}

Options

options is written into the server build with JSON.stringify, so only plain data survives it. Today the check is narrow: only a value JSON.stringify throws on, such as a cycle or a BigInt, fails the build with plugin_options_not_json. A function is dropped and a class instance loses its methods and prototype, with nothing said, so the server half just doesn’t get them (#2314, item 10). Anything live, such as a store client or credentials, belongs in the app’s hooks.server.ts. The usual shape is a function your server module exports for the app to call there, the way postrender.handle({ store }) takes its store.

Settle defaults in the factory, as stamp() does above, so the server half reads complete options. The server half types them through defineServer<Options>.

Shipping

A plugin’s halves are files inside its own package, named by location, so each module exists once and needs no entry in the package’s exports. Those stay yours to name: usually ./vite for the factory, and whatever your app-facing API is.

The location must be a file that exists where the factory runs, or the build fails with plugin_realm_not_file; a location that isn’t a string fails with plugin_realm_not_location. Write the name of the file your package ships. If your build turns server.ts into server.js, write ./server.js in the source too, or have the build rewrite the specifier: ogygia’s own packages do the second with a small step in their tsdown config.

The build reads two things from the halves’ files without running them. It reads which shared hooks a server half implements from the keys of its defineServer({ … }), and which runtime hooks a browser half subscribes to from the keys of its hooks. So write the default export as defineServer({ … }) or defineClient({ … }) with an object literal. Parts of it may sit in module-level consts.

ogygia-plugin-stamp/
├─ package.json      exports: { "./vite": "./dist/vite.js" }
└─ dist/
   ├─ vite.js        definePlugin({ server: import.meta.resolve('./server.js') })
   ├─ server.js      export default defineServer({ … })
   └─ client.js      export default defineClient({ … })

Build errors

The build checks every plugin when Vite loads its config, and stops with a message code on the first problem:

CodeWhen
plugin_without_ogygiaA plugin is listed, and ogygia() isn’t.
plugin_after_sveltekitogygia() or a plugin is listed after sveltekit().
plugin_factory_not_calledA function is listed in plugins instead of its result.
plugin_name_missingA definition has no name.
plugin_listed_twiceTwo plugins have the same name.
plugin_package_unresolvedNo package by the plugin’s name resolves from the app.
plugin_realm_not_locationserver or client isn’t a string.
plugin_realm_not_fileserver or client doesn’t point at a file.
plugin_options_not_jsonJSON.stringify throws on options.
plugin_needs_pluginA plugin in needs isn’t listed.
plugin_client_hooks_unreadThe build can’t read the hooks keys of a browser half.
plugin_client_hook_unknownA browser half subscribes to a name that isn’t a runtime hook.

Build context lists the errors a build(b) call can raise.

Moved sections

These sections used to be on this page. Each now lives on its own page: