Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ jobs:
with:
name: packages-dist

# `--only`: the packages were built by the `build` job and downloaded
# above, so the `^build:plugins` dependency of `lint` must not rebuild them.
- name: Run lint
run: pnpm lint
run: pnpm turbo run lint --only --continue && pnpm format:check

test:
name: Test
Expand Down
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pnpm dev # build:scripts (CLI) -> build:plugins -> turbo dev; we
pnpm build # full build (plugins first)
pnpm lint # oxlint --type-aware in every package + oxfmt --check
pnpm lint:fix # fix lint + format; run before every commit, never commit lint errors
pnpm test # vitest across packages (incl. integration)
pnpm test # vitest across packages (incl. integration); cached by Turborepo, like lint
```

- After changing the `vitnode` CLI (`packages/api/scripts/**`) run `pnpm build:scripts`; after changing a plugin or `@vitnode/core` exports consumed by the app, `pnpm build:plugins` (dev does both on start).
Expand Down Expand Up @@ -85,6 +85,7 @@ Every plugin exports a frontend half (`src/config.tsx` → `buildPlugin` from `@

- Write regression tests in a separate session after a feature works, not while building it.
- File name selects the environment: `*.test.ts` → node (`unit`, `isolate: false`), `*.test.tsx` → happy-dom (`dom`). No `// @vitest-environment` pragmas. Tests asserting process-wide state go in the `ISOLATED` list, real builds/child processes in `INTEGRATION`, in the package's `vitest.config.ts`.
- No tests against a real database or any other running service (no `*.postgres.test.ts`): `pnpm test` must pass with nothing started, which is also what lets Turborepo cache `test` and `lint`. Cover queries through the `c.set("db", fake)` seam or `@/tests/memory-db`.
- Avoid `vi.mock`; use seams: `c.set("db" | "cache" | "admin" | "events", fake)`, `vi.stubGlobal`, `vi.spyOn`, props/providers. Staff permissions: `grantStaffPermissions` from `@/tests/staff-permissions` on `createTestCache()` from `@/tests/cache` — never mock `check-staff-permission`; assert `200`/`403`. `use-intl` is mocked globally in `packages/vitnode/src/tests/setup.ts` (`t("key")` → `namespace.key`); assert on keys.
- Test a rule once, in the layer that owns it; search for an existing test before adding one; prefer pure-function tests over renders; collapse near-duplicates into `it.each`. Content Engine route permissions belong in `content/server/permission-matrix.test.ts`. Use fake timers, never real delays.
- Don't test: wiring an existing mechanism somewhere new, Drizzle schemas/SQL, static config/constants/query keys/`staticData`, third-party or thin wrapper behavior, source text (`readFileSync` + `toContain`), locale JSON contents, CSS classes. Keep security tests (cookie scoping, auth ceremonies, timing-safe paths, open-redirect guards, per-route `403`s) even when they look redundant.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ npm run dev

Open `http://localhost:3000`, then sign in at `/admin`.

In a Turborepo workspace, every plugin package runs the TypeScript type checker
in watch mode while you develop. If that uses more memory than you have, turn it
off; your editor and the build still report type errors:

```bash
VITNODE_DEV_TYPECHECK=0 pnpm dev
```

See [Run without the type checker](https://vitnode.com/docs/dev/cli/dev#run-without-the-type-checker).

## Build features as plugins

1. [Create a plugin](https://vitnode.com/docs/dev/plugins/create).
Expand Down
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"oxlint": "^1.95.0",
"oxlint-tsgolint": "^7.0.2009",
"react-email": "^6.11.17",
"tsc-alias": "^1.9.12",
"tsdown": "^0.23.7",
"tsx": "^4.23.15",
"typescript": "7.1.0-beta"
}
Expand Down
10 changes: 6 additions & 4 deletions apps/web/content/docs/dev/cli/build.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,24 @@ The same command builds other project types:

| Project | What `vitnode build` does |
| -------------- | --------------------------------------------------------------------------------- |
| API app (Node) | Compiles with `tsc` and `tsc-alias` into `dist`, then lists the output |
| API app (Node) | Compiles `src` into `dist` with tsdown while `tsc` checks types, then lists the output |
| API app (Bun) | Nothing: Bun runs `src/index.ts` directly |
| Plugin package | Compiles `src` into `dist/src` with tsdown and checks types with `tsc --noEmit` |
| Plugin package | Compiles `src` into `dist/src` with tsdown; `tsc` checks types and writes the declarations |

```txt title="vitnode build in a plugin package"
◆ VitNode
Building @acme/blog

✓ API registry generated
✓ Compiling sources 8.0s
✓ Checking types 7.3s
✓ Checking types and writing declarations 7.3s

✓ Built in 8.0s
```

Compiling and type checking run at the same time, and both must pass. [Build and watch a plugin](/docs/dev/plugins/build) explains what ends up in `dist`.
Compiling and type checking run at the same time, and both must pass. In a plugin package the type check also writes the declarations (`.d.ts`).

An API app on Node needs `tsdown` in its `devDependencies`, as a plugin package does. Projects from `create-vitnode-app` have it. In an older API app, add it and remove `tsc-alias`, which `vitnode build` no longer runs. [Build and watch a plugin](/docs/dev/plugins/build) explains what ends up in `dist`.

## When a build fails

Expand Down
38 changes: 34 additions & 4 deletions apps/web/content/docs/dev/cli/dev.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Start a VitNode app, API app or plugin package in development with
icon: Play
---

`vitnode dev` starts whatever the current folder is: an app gets a Vite dev server, an API app gets a watching Node or Bun process, and a plugin package gets tsdown and the type checker in watch mode. Run it from the project's folder, usually through the `dev` script:
`vitnode dev` starts whatever the current folder is: an app gets a Vite dev server, an API app gets a watching Node or Bun process, and a plugin package gets tsdown and, unless you turn it off, the type checker in watch mode. Run it from the project's folder, usually through the `dev` script:

```bash
pnpm dev
Expand Down Expand Up @@ -94,7 +94,7 @@ Otherwise it runs on Node with `tsx watch`.

## Develop a plugin package

In a plugin package, `vitnode dev` regenerates the plugin's API registry (`types/api-registry.gen.d.ts`, when the plugin has a `src/config.api.ts`) and its manifest (`src/manifest.gen.ts`), then watches `src` with tsdown and runs `tsc` in watch mode to report type errors. Apps that use the plugin import its `dist` folder, so they reload as you save.
In a plugin package, `vitnode dev` regenerates the plugin's API registry (`types/api-registry.gen.d.ts`, when the plugin has a `src/config.api.ts`) and its manifest (`src/manifest.gen.ts`), then watches `src` with tsdown and runs `tsc` in watch mode to report type errors and write declarations. Apps that use the plugin import its `dist` folder, so they reload as you save.

```txt
◆ VitNode
Expand All @@ -105,13 +105,43 @@ In a plugin package, `vitnode dev` regenerates the plugin's API registry (`types
● Watching sources - apps using this plugin reload as dist/ changes

tsdown js Rebuilt in 71ms.
tsdown types Rebuilt in 4493ms.
4:12:07 PM - Found 0 errors. Watching for file changes.
tsc types 2 declarations updated in 9ms.
```

The JavaScript (`tsdown js`) is rebuilt within milliseconds of a save. Declarations (`tsdown types`) follow once TypeScript catches up, so your editor sees new types a few seconds later. A syntax error is printed and the next save rebuilds. If a watcher fails to start, for example because tsdown is not installed, the CLI stops the others and exits with code `1`.
The JavaScript (`tsdown js`) is rebuilt within milliseconds of a save. Declarations (`tsc types`) follow once `tsc` has checked the change, and only the ones that changed are written. A syntax error is printed and the next save rebuilds. If a watcher fails to start, for example because tsdown is not installed, the CLI stops the others and exits with code `1`.

[Build and watch a plugin](/docs/dev/plugins/build) covers added and deleted files, assets and what your app sees while a rebuild is written.

### Run without the type checker

`tsc --watch` keeps a whole TypeScript program in memory for every plugin package, which makes it the largest part of a plugin watcher. When your machine runs short of memory, turn it off. tsdown still writes the JavaScript, and a short `tsc --noCheck` run after each save still writes the declarations, so apps reload as before.

For one package, pass `--no-typecheck`:

```bash
vitnode dev --no-typecheck
```

For every package a workspace starts, set `VITNODE_DEV_TYPECHECK` to `0` or `false` before the root `dev` script:

```bash
VITNODE_DEV_TYPECHECK=0 pnpm dev
```

In PowerShell, run `$env:VITNODE_DEV_TYPECHECK = "0"` first, then `pnpm dev`. The CLI confirms the mode on start:

```txt
● Watching sources - apps using this plugin reload as dist/ changes
Type checking is off - your editor and `vitnode build` still report type errors
```

Type errors no longer appear in the terminal while you develop. Your editor still shows them, and `vitnode build` always type checks, so a build or CI run fails on an error either way. The option only affects plugin packages. Apps and API apps never started `tsc`.

<Callout type="info" title="Turborepo passes only the variables it knows">
A workspace created by `create-vitnode-app` lists `VITNODE_DEV_TYPECHECK` under `passThroughEnv` of the `dev` task in `turbo.json`. In an older workspace, add it there, or Turborepo does not hand the variable to the packages.
</Callout>

## Related

<Cards>
Expand Down
8 changes: 8 additions & 0 deletions apps/web/content/docs/dev/contribution.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ pnpm dev

`dev` builds the CLI scripts and the plugins, then starts every app with Turborepo. Before the web server starts, its `dev` script runs the API package's `db:prepare`, which generates and applies migrations and seeds the initial data.

Every package also starts `tsc --watch`, six TypeScript programs in this repository. If that is more memory than your machine has to spare, start without them:

```bash
VITNODE_DEV_TYPECHECK=0 pnpm dev
```

The apps and the plugin rebuilds run as before. Type errors then come from your editor, `pnpm build:plugins` and CI instead of the terminal. See [Run without the type checker](/docs/dev/cli/dev#run-without-the-type-checker).

</Step>
</Steps>

Expand Down
15 changes: 15 additions & 0 deletions apps/web/content/docs/dev/performance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,21 @@ In a production build the HTML and CSS load first, and a small inline script add

---

## Library chunks VitNode splits for you

The `vitnode()` Vite plugin gives a few large libraries a client chunk of their own in a production build. You don't configure this.

| Chunk | Holds | Why |
| -------------- | ----------------------------------------------- | ------------------------------------------------------------------------------------- |
| `react` | `react`, `react-dom`, `scheduler` | The entry chunk changes on every deploy. React does not, so returning visitors keep it cached. |
| `prosemirror` | The ProseMirror packages, without tables | Loaded when an editor first opens |
| `tiptap` | `@tiptap/core`, `@tiptap/pm`, `@tiptap/suggestion` | Loaded when an editor first opens |
| `editor-emoji` | The emoji data set of `@tiptap/extension-emoji` | The largest part of the editor, fetched in parallel with the two chunks above |

The split changes how modules are grouped into files, not which page loads them. `editor-emoji` stays above the size the report warns about, because it is one data set that cannot be divided.

---

## Measure the bundle

Build the app. `vitnode build` prints a bundle size report with every client file, its gzip size, warnings for oversized chunks and a comparison with your previous build:
Expand Down
28 changes: 13 additions & 15 deletions apps/web/content/docs/dev/plugins/build.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The output is ES modules that Node and Vite load as they are:

- **Imports of packages stay imports.** React, Hono, Drizzle, `@vitnode/core` and every other package are left for the app to install, exactly as written.
- **Relative imports get their `.js` extension**, and dynamic imports such as `lazy(() => import("./pages/home-page"))` keep working.
- **`@/` aliases become relative paths**, in the JavaScript and in the declarations. The aliases come from `paths` in `tsconfig.build.json`.
- **`@/` aliases become relative paths**. The aliases come from `paths` in `tsconfig.build.json`, and the declarations get the same treatment (see [Types and declarations](#types-and-declarations)).
- **JSX** uses React's automatic runtime (`react/jsx-runtime`).
- **File names are kept**, so `auth.server.ts` stays `auth.server.js`. Modules keep their side effects; nothing is tree-shaken away.

Expand All @@ -67,14 +67,11 @@ export default {

## Types and declarations

Two TypeScript jobs run side by side, and both must pass:
tsdown writes the JavaScript. One `tsc` program does the rest: it type checks the whole package with `tsconfig.build.json` and emits the `.d.ts` files and their maps. Both run side by side, and both must pass.

| Job | What it does |
| -------------- | ------------------------------------------------------------------------------ |
| tsdown | Writes the `.d.ts` files and their maps with the TypeScript compiler |
| `tsc --noEmit` | Type checks the whole package with `tsconfig.build.json` and fails on an error |
`tsc` does not write into `dist/src` directly. It emits into a cache folder under `node_modules/.cache/vitnode`, and the CLI copies each declaration into `dist/src` with its imports rewritten: an `@/` alias or an import without an extension becomes the relative path of the compiled module, such as `./lib/math.js`. That is what lets an app resolve your types whatever its own aliases and module resolution are. The cache also holds TypeScript's incremental build info, so the next build re-checks only what changed. Delete the folder to start from scratch.

tsdown writes declarations for every file `tsconfig.build.json` includes. That matters for files nothing imports, such as `global.d.ts` and the generated `types/api-registry.gen.d.ts`. They are what lets `fetcher({ plugin: "site-notes", ... })` infer its response, and that inferred type lands in your `.d.ts` files.
Declarations cover every file `tsconfig.build.json` includes. That matters for files nothing imports, such as `global.d.ts` and the generated `types/api-registry.gen.d.ts`. They are what lets `fetcher({ plugin: "site-notes", ... })` infer its response, and that inferred type lands in your `.d.ts` files. A build that fails on a type error leaves the declarations of the last good build in place.

`types/api-registry.gen.d.ts` registers the plugin's own API with the fetcher. `vitnode build` and `vitnode dev` write it before compiling, but only when the plugin has a `src/config.api.ts`. The template `.npmignore` keeps `/types` out of the published package; commit it like `src/manifest.gen.ts` so the plugin type-checks right after a checkout.

Expand All @@ -96,15 +93,16 @@ The CLI treats a folder as a plugin package when it has `tsconfig.build.json` an

## How `vitnode dev` watches a plugin

`vitnode dev` runs three watchers:
`vitnode dev` starts two processes:

- **tsdown js** rebuilds the JavaScript and copies changed assets, usually within 100 ms for a plugin.
- **tsdown types** rebuilds the declarations once TypeScript has caught up, a few seconds later.
- **`tsc --watch --noEmit`** prints type errors.
- **The package watcher.** tsdown rebuilds the JavaScript and copies changed assets, usually within 100 ms for a plugin (`tsdown js` in the output). It also copies new declarations into `dist/src` as they are emitted (`tsc types`).
- **`tsc --watch`.** One TypeScript program that prints type errors and emits the declarations.

The two tsdown watchers run as separate processes, so the TypeScript compiler never holds back the JavaScript your app reloads.
The compiler is a process of its own, so it never holds back the JavaScript your app reloads.

Adding, deleting or renaming a file restarts both tsdown watchers with the new list of modules, after a short pause so a branch switch counts as one change. Output for a deleted module is removed from `dist/src`. A file that fails to compile is reported, the last good output stays in place, and the next save rebuilds.
`tsc --watch` is the process that uses the most memory. `vitnode dev --no-typecheck`, or `VITNODE_DEV_TYPECHECK=0` for a whole workspace, leaves it out. The package watcher then runs a short `tsc --noCheck` after each save to emit the declarations, and no TypeScript program stays in memory between saves. See [Run without the type checker](/docs/dev/cli/dev#run-without-the-type-checker).

Adding, deleting or renaming a file restarts the tsdown watcher with the new list of modules, after a short pause so a branch switch counts as one change. Output for a deleted module is removed from `dist/src`. A file that fails to compile is reported, the last good output stays in place, and the next save rebuilds.

<kbd>Ctrl</kbd>+<kbd>C</kbd> stops every watcher. If one of them exits on its own, the CLI stops the rest and exits with an error.

Expand All @@ -120,7 +118,7 @@ VitNode uses different builds for different things. This page is about the first

| Build | Who runs it | Tool |
| -------------- | ---------------------------------- | ----------------------------------------------------------- |
| Plugin package | `vitnode build` in a plugin (its `build:plugins` script) | tsdown in unbundle mode, plus `tsc --noEmit` |
| Plugin package | `vitnode build` in a plugin (its `build:plugins` script) | tsdown in unbundle mode for JavaScript, `tsc` for type checking and declarations |
| VitNode CLI | `pnpm build:scripts` in `@vitnode/api` | tsdown with `packages/api/tsdown.config.ts`, into `dist/scripts` |
| App | `vitnode build` in an app | Vite and TanStack Start, unchanged |

Expand All @@ -129,7 +127,7 @@ VitNode uses different builds for different things. This page is about the first
## Limitations

- tsdown needs Node.js `^22.18.0`, `^24.11.0` or `26` and later. VitNode itself asks for a little more in the 22 line (`^22.22.0`), so stick to that range. Node.js 23 and 25 are not on the list.
- Declarations are generated by the TypeScript compiler, which is the slow part of a build. In watch mode they arrive a few seconds after the JavaScript. For a package as large as `@vitnode/core` or `@vitnode/api`, adding or deleting a file rebuilds all of its declarations.
- Type checking is the slow part of a build, and the first build after an install is the slowest: the cache under `node_modules/.cache/vitnode` is empty, so TypeScript checks every file. Later builds reuse it.
- Each JavaScript rebuild renders every module of the package again and writes only the changed ones. That takes under 100 ms for a typical plugin and about 2 seconds for the roughly 1,600 modules of `@vitnode/core`.
- A re-export-only `index.ts` may get no `.d.ts.map`, so "Go to definition" stops at its `.d.ts` first.

Expand Down
Loading