# Writing a plugin

**The plugin API is experimental**, except for the
[v1 data source contract](/documentation/plugins/data-sources): outside that contract, it may change
in a minor release ([Stability](/documentation/reference/stability#experimental)).

## Start from the template

Récit is not on npm: `create-recit`, which holds the template, comes as an archive of the private
channel, like the `recit` command line ([Quickstart](/documentation/getting-started/quickstart)).
Run it from that archive:

```bash
npm exec --package ./archives/create-recit.tgz -- create-recit recit-plugin-acme --template plugin
```

The template is the reference plugin of the Récit repository, `examples/plugin-lorem`: one of each
capability, its tests, and a build with `tsdown`. The package takes the name of its folder.

**It needs npm 11 or later** (`engines.npm` in its `package.json`). npm 10, the one Node 22 ships,
crashes while installing vitest 4.1 (« Cannot read properties of null (reading 'edgesOut') »).
`create-recit` says so when npm 10 runs it; update first with `npm install -g npm@11`. pnpm, yarn
and bun are not concerned.

**Naming.** Call the package `recit-plugin-<name>`, or `@scope/recit-plugin-<name>`, and give it the
`recit-plugin` keyword in `package.json`: that is how people find it on npm. Its `name` in
`definePlugin` is the short one, `acme`: it prefixes your diagnostics (`acme/quote-empty`) and your
commands (`recit acme sync`). A plugin cannot take the name of a command of Récit (`build`, `ai`,
`plugins`…).

## Two entries

```json
"exports": {
  ".": {
    "types": "./dist/index.d.ts",
    "browser": "./dist/client.js",
    "default": "./dist/index.js"
  }
},
"peerDependencies": { "recit": "^1", "zod": "^4" }
```

The **Node entry** declares everything, with its schemas:

```ts
// src/index.ts
import { definePlugin } from "recit/plugin";
import * as z from "zod";

export default definePlugin({
  name: "acme",
  api: 1,
  options: z.object({ accent: z.string().default("violet") }),
  directives: {
    "acme-quote": {
      attributes: z.object({ author: z.string() }),
      render: () => import("./quote.js"),
    },
  },
  components: { AcmeChart: () => import("./AcmeChart.js") },
  blocks: { "acme-counter": () => import("./counter-block.js") },
  themes: { "acme-dusk": () => import("./theme.js") },
  exporters: { "acme-pptx": () => import("./export-pptx.js") },
  sources: { "acme-rows": () => import("./rows.js") },
  commands: { sync: () => import("./sync.js") },
  ai: () => import("./ai.js"),
  hooks: { configResolved: () => import("./on-config.js") },
});
```

`name` is how a config and the diagnostics name the plugin, `api` the plugin API it is written for
(`1`), `version` (optional) its own version, part of the cache key of its data sources, and
`options` the schema of what `recit.config.ts` passes it. Every capability is a module loaded only
when it is used.

The **browser entry** holds a name, the components and the views of the film blocks: no zod, no
schema, no Node.

```ts
// src/client.ts
import { defineClientPlugin } from "recit/plugin";

export default defineClientPlugin({
  name: "acme",
  components: { AcmeChart: () => import("./AcmeChart.js") },
  blocks: { "acme-counter": () => import("./counter-view.js") },
});
```

Every capability is a lazy `import()`: a module loads only when it is used, and each component is a
chunk of its own, downloaded by a slide that mounts it.

## Each capability

| Capability   | Its module's default export                                                                                                                                   |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `directives` | static: a function that renders hast at build time; interactive: `component`, the name of one of `components`                                                 |
| `components` | a React component, given the directive's parsed attributes as props                                                                                           |
| `blocks`     | `{ type, props, text?, duration? }`, `props` a zod schema; its view, `defineBlock` of `@recit/motion-blocks/plugin`                                           |
| `themes`     | a complete theme of `@recit/theme`, every token, whose `id` is its key                                                                                        |
| `exporters`  | `{ description?, export(context) }`, returning the files it wrote                                                                                             |
| `sources`    | `{ schema?, ttl?, read(context) }`, returning `{ rows, columns?, meta? }` ([Writing a data source](/documentation/plugins/data-sources))                      |
| `commands`   | `{ description?, usage?, run(context) }`, returning the exit code                                                                                             |
| `ai`         | `{ jobs, provider? }` ([AI jobs](/documentation/plugins/ai))                                                                                                  |
| `hooks`      | `configResolved`: a function given the validated config, read only, once per load; a write or a thrown error is the `plugin-failed` diagnostic (experimental) |

A block's view draws in `Frame` of `@recit/motion-blocks/plugin` (experimental), given its `layout`
(`16x9` or `9x16`): the scene root with the theme's background, colours and embedded fonts, so the
render is the same on every machine (`recit-example-plugin-lorem` checks its block against a golden
still).

**A directive never exists in Markdown only.** Its `attributes` schema is required: it validates the
directive in a `.md` and in its JSON twin, and gives its JSON Schema. A static directive's output
goes in after sanitisation, like Récit's own: every text is a hast text node, never raw HTML.

**A secret is a `SecretRefSchema`** (`{ env: "NAME" }`), read with `readSecret(secret, context.env)`
when it is needed. Never read `process.env` at import time, never write a value into a file.

**Data sources.** The data source contract (`sources`, `PluginSource`, DCAT `meta`, the 30 s and
100,000-row bounds, `.recit/cache/` and `plugin-stale` fallback) is **stable in v1**; see
[Writing a data source](/documentation/plugins/data-sources).

**Style.** A component brings its styles as one CSS module, compiled into the package, never a
utility class or a `style` prop: the same rule as Récit's own components.

## Test it

```ts
import { expectRoundTrip, fakeAiProvider, runPluginJob } from "recit/plugin/testing";
import acme from "../index.js";

// A story that uses the directives survives Markdown → JSON → Markdown
await expectRoundTrip(acme(), ':::acme-quote{author="Cicero"}\nNeque porro.\n:::\n');

// An AI job, with no network
const { story } = await runPluginJob(acme(), "notes.write", {
  story: "# Lorem\n\nIpsum.\n",
  provider: fakeAiProvider(['{"notes": [{"id": "lorem", "notes": "Hello."}]}']),
});
```

## Diagnostics

Report through `context.report`, with a code prefixed by your plugin's name (`acme/quote-empty`).
Récit's own codes for plugins are `plugin-not-found`, `plugin-api-mismatch`,
`plugin-options-invalid`, `plugin-name-conflict`, `plugin-no-browser-entry`, `plugin-failed`,
`plugin-stale`, `plugin-missing` and `plugin-directive-attributes`. An error your plugin throws is
shown as yours, with its name, never as a bug of Récit.
