Build tools
Choose and set up the build tool that rewrites model<T>(), parse<T>() and the other serde calls.
Why a build tool is needed
model<T>(), parse<T>, stringify<T>, toWireFormat<T>, fromWireFormat<T>, validate<T> and assertValue<T> take a type argument. TypeScript erases that argument at run time. A build tool compiles or loads your TypeScript. It reads each of these calls with your project's TypeScript and replaces model<T>() with modelBase(...) plus a side-effect import of the generated codec file, and replaces each serde call with generated code. tsc does not rewrite these calls.
A call that no build tool rewrote throws a LibError of kind ValidationUnavailable (validation_unavailable). The message starts with the name of the call, as in model<T> was not rewritten... or parse<T> was not rewritten to a generated function. It then lists the build tools that rewrite the call.
A call that no build tool can rewrite fails the build with its file, its line and what to run. golem check reports the same calls.
validateInstance works without a build tool once the model class has been built.
Supported build tools
| Build tool | Use it when | Setup |
|---|---|---|
golem build | You compile with tsc and run the JavaScript | Replace tsc -p |
| Vite plugin | Vite bundles your app | golem() in plugins |
| esbuild plugin | esbuild bundles your app | golem() in plugins |
| Node loader | Node runs your TypeScript files | --import flag |
Every build tool runs on TypeScript 7.1+ and produces the same rewrite.
The plugins and the loader open your project from golem.yaml:
- A plugin uses its
configoption first. Then it reads theGOLEM_CONFIGenvironment variable, then it opens./golem.yaml. - The loader reads
GOLEM_CONFIG, then./golem.yaml.
A plugin, the loader and golem build read the blocks of golem.yaml, so they need the packages of those blocks and typescript installed. When one of them runs without dev dependencies, list those packages in dependencies.
The plugins and the loader scan your files themselves. They need the generated files to exist. Run golem generate once before you use them.
Node versions
Golem needs Node.js 22.15 or newer (requirements). The flags that each build tool needs depend on the Node.js version:
| Node.js version | golem build | Vite and esbuild plugins | Node loader |
|---|---|---|---|
| 22.15 to 22.17 | No flag | No flag | Add --experimental-strip-types |
| 22.18 or newer | No flag | No flag | No flag |
golem build writes JavaScript, and the plugins run inside Vite or esbuild, which strip the types. Only the loader gives TypeScript files to Node.
Build with golem build
npx golem buildgolem build generates first and type-checks your sources with the TypeScript in your project. It starts no tsc process. Then it writes JavaScript with the calls rewritten into outDir. A fresh clone builds with this one command. Run the result with Node:
node .build/src/main.jsThe build needs a golem.yaml and a tsconfig with outDir. Use the project settings.
- It prints type errors as
tsc -pdoes. It uses colors and aFound N errorsline when theprettyoption is true, or when stdout is a terminal andNO_COLORis not set. - A failed check still writes JavaScript, unless
noEmitOnErroris true. The exit status is not0after a failed check. - It writes an output file only when the file changes. A build with nothing changed writes no file.
- A source map points at your original sources, not at the rewritten text. This holds for
sourceMap,inlineSourceMap,sourceRoot,mapRootandinlineSources. --listFilesprints the files of the program.--generateTrace,--extendedDiagnostics,--explainFiles,--traceResolutionand--diagnosticsstop the build with a message, also when the tsconfig sets them. Runtsc -p <tsconfig> --noEmitwith the option instead.outFilestops the build because TypeScript 7 removed it (TS5102). Emit one file for each source, then bundle with esbuild or Vite and the golem plugin.- When
model.outputlies outsiderootDir(such as the default.golem/modelswithrootDir: "src"),golem buildemits the compiled generated model modules under<outDir>/__golem__/models/and rewrites imports automatically. - Do not run
tsc -pwithout--noEmiton the same project. It writes JavaScript with no rewrite into the sameoutDir.
Project references
A tsconfig with references builds as tsc -p builds it. The check reads the declaration files that the referenced projects wrote, not their sources.
- Set
composite: truein each referenced project. - Build each referenced project first. A project with golem calls builds with
golem build --config <project folder>/golem.yaml. A project with none builds withtsc -p <project folder>. - Point
tsconfigingolem.yamlat the project that lists the files, and build each referenced project with its own command above. - A model that a referenced project declares gets its codec in your project when the name of its declaration file ends in
.model.d.tsor.models.d.ts.
The options, the import paths that the build changes and the compiler arguments that it passes on are in the CLI reference.
Vite
import react from '@vitejs/plugin-react';
import { golem } from '@exprtlabs/golem/vite';
const plugins = [golem({ config: 'web/golem.yaml' }), react()];golem()rewrites each TypeScript module in memory before Vite strips its types. This holds in the dev server and in builds.- Put
golem()before plugins that transform TypeScript. - The
configoption is optional. It names thegolem.yamlrelative to the Vite root. Without it, the plugin readsGOLEM_CONFIG, then./golem.yaml. - While you run
golem watch, the dev server transforms again only the modules whose rewrite changed.
esbuild
import { golem } from '@exprtlabs/golem/esbuild';
import { build } from 'esbuild';
await build({
entryPoints: ['src/main.ts'],
bundle: true,
platform: 'node',
format: 'esm',
outfile: '.build/bundle/main.js',
plugins: [golem()],
});golem()rewrites each TypeScript file in memory before esbuild strips its types.- It takes each TypeScript file outside
node_modulesthat holds a call to rewrite. Other files go to the next plugins. - A workspace package that is linked into
node_modulescounts as outside it, also withpreserveSymlinks. Build a linked workspace package with its owngolem build. - A plugin that needs a file that
golem()takes goes beforegolem()inplugins. confignames thegolem.yamlrelative toabsWorkingDir.
Node loader
node --import @exprtlabs/golem/register src/main.ts- The loader rewrites TypeScript files as Node loads them.
- The loader also loads
.tsxfiles. Thejsxoptions of your tsconfig set their output. - Run
golem generateonce, so the generated files exist. - The loader needs ES modules. Use
"type": "module"or.mtsfiles. - Node before 22.18 needs
--experimental-strip-typesas well (Node versions). - The loader compiles the files that Node cannot strip, such as a file with a decorator or an
enum, with your TypeScript. A NestJS app needs no second loader (run with the Node loader).
Lint for unrewritable code
A build tool rewrites each call that it can read. It cannot rewrite parse<TPayment> used as a value, such as texts.map(parse<TPayment>). That code throws when it runs. golem build and golem check report it. The ESLint rule golem/no-unrewritable-code reports the same errors when ESLint runs, also in your editor.
golem() returns one flat-config object. It turns the rule on for .ts, .mts, .cts and .tsx files. ESLint needs a parser for these files. Choose one setup:
- Use the first setup when your project lints TypeScript with typescript-eslint. Its parser stays in use.
- Use the second setup when your project has no ESLint parser for TypeScript. The rule reads the text of the file, so
parser: 'text'gives ESLint an empty program in its place.
// eslint.config.mjs: with typescript-eslint
import { golem } from '@exprtlabs/golem/eslint';
import tseslint from 'typescript-eslint';
export default [tseslint.configs.base, await golem()];// eslint.config.mjs: without an ESLint parser for TypeScript
import { golem } from '@exprtlabs/golem/eslint';
export default [await golem({ parser: 'text' })];- The rule reads your
golem.yamland yourtsconfig.json, as the build tools do. Theconfigoption names anothergolem.yaml, andcwdsets the folder where the search starts. Without them,golem()readsGOLEM_CONFIG, then./golem.yamlin the working directory. - Run
golem generatefirst, so the generated files exist. A lint run that goes on while you rungolem generatereads the new files. eslint-disablecomments work with both setups.- A report names the code, such as
RUNM1079, and the position. The diagnostics page gives the fix for each code.
Editor setup (generate or watch)
Your model classes extend model<T>() with zero imports from generated files, so your editor type-checks clean clones immediately. Run npx golem generate once, or keep npx golem watch running (Generate and watch), whenever you use generated SDK types or want the Vite plugin, esbuild plugin or Node loader to read .golem/models/.
Build in CI
npx --no-install golem buildgolem build generates first, so a clone needs no committed generated files. With --no-install, npx fails when the project does not install Golem. golem check reports stale generated files without writing.
