Skip to content

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 toolUse it whenSetup
golem buildYou compile with tsc and run the JavaScriptReplace tsc -p
Vite pluginVite bundles your appgolem() in plugins
esbuild pluginesbuild bundles your appgolem() in plugins
Node loaderNode 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 config option first. Then it reads the GOLEM_CONFIG environment 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 versiongolem buildVite and esbuild pluginsNode loader
22.15 to 22.17No flagNo flagAdd --experimental-strip-types
22.18 or newerNo flagNo flagNo 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 ​

sh
npx golem build

golem 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:

sh
node .build/src/main.js

The build needs a golem.yaml and a tsconfig with outDir. Use the project settings.

  • It prints type errors as tsc -p does. It uses colors and a Found N errors line when the pretty option is true, or when stdout is a terminal and NO_COLOR is not set.
  • A failed check still writes JavaScript, unless noEmitOnError is true. The exit status is not 0 after 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, mapRoot and inlineSources.
  • --listFiles prints the files of the program. --generateTrace, --extendedDiagnostics, --explainFiles, --traceResolution and --diagnostics stop the build with a message, also when the tsconfig sets them. Run tsc -p <tsconfig> --noEmit with the option instead.
  • outFile stops 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.output lies outside rootDir (such as the default .golem/models with rootDir: "src"), golem build emits the compiled generated model modules under <outDir>/__golem__/models/ and rewrites imports automatically.
  • Do not run tsc -p without --noEmit on the same project. It writes JavaScript with no rewrite into the same outDir.

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: true in 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 with tsc -p <project folder>.
  • Point tsconfig in golem.yaml at 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.ts or .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 ​

ts
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 config option is optional. It names the golem.yaml relative to the Vite root. Without it, the plugin reads GOLEM_CONFIG, then ./golem.yaml.
  • While you run golem watch, the dev server transforms again only the modules whose rewrite changed.

esbuild ​

js
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_modules that holds a call to rewrite. Other files go to the next plugins.
  • A workspace package that is linked into node_modules counts as outside it, also with preserveSymlinks. Build a linked workspace package with its own golem build.
  • A plugin that needs a file that golem() takes goes before golem() in plugins.
  • config names the golem.yaml relative to absWorkingDir.

Node loader ​

sh
node --import @exprtlabs/golem/register src/main.ts
  • The loader rewrites TypeScript files as Node loads them.
  • The loader also loads .tsx files. The jsx options of your tsconfig set their output.
  • Run golem generate once, so the generated files exist.
  • The loader needs ES modules. Use "type": "module" or .mts files.
  • Node before 22.18 needs --experimental-strip-types as 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.
js
// eslint.config.mjs: with typescript-eslint
import { golem } from '@exprtlabs/golem/eslint';
import tseslint from 'typescript-eslint';

export default [tseslint.configs.base, await golem()];
js
// 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.yaml and your tsconfig.json, as the build tools do. The config option names another golem.yaml, and cwd sets the folder where the search starts. Without them, golem() reads GOLEM_CONFIG, then ./golem.yaml in the working directory.
  • Run golem generate first, so the generated files exist. A lint run that goes on while you run golem generate reads the new files.
  • eslint-disable comments 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 ​

sh
npx --no-install golem build

golem 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.