Browse the docs

Generate Phase

generate runs before TypeScript build and test jobs. It prepares generated source, exports, and static assets so downstream phases see a complete project.

What generate does

Key steps:

  • discover pre-build hooks from plugin dependencies
  • run hooks in ascending order, ties broken by extension name
  • write generated source and export metadata to .gen/
  • copy configured generated assets into the project output

Generate is not a command of its own: putnami build and putnami test run it first.

putnami build .
putnami build . --clear

Options

Option Use
--verbose Show detailed generation output
--clear Clear the dist folder before building

Pre-build hooks

A dependency contributes a pre-build hook by shipping a putnami.extension.json at its package root that declares hooks.preBuild:

{
  "$schema": "https://putnami.dev/schemas/putnami-extension.json",
  "name": "@my/extension",
  "hooks": {
    "preBuild": {
      "kind": "command",
      "command": "bun",
      "args": ["run", "{extensionRoot}/bin/generate"],
      "cwd": "{projectRoot}",
      "timeoutMs": 120000
    }
  }
}

The runner expands {workspaceRoot}, {projectRoot} and {extensionRoot} in the arguments, appends --putnami-context <path>, the JSON job context, and sets PUTNAMI_PREBUILD_CONTEXT=true. The hook writes JSONL events on stdout; its last one, the summary, carries the exports and assets it produced and its route evaluation, data.httpRoutes, which every pre-build hook reports: {"v":1,"state":"complete"} once it has evaluated its route contribution, or {"v":1,"state":"unavailable","reason":"<text>"} when it could not. A hook that omits it or reports it unavailable fails generate with HTTP_ROUTES_UNAVAILABLE, which names the hook and the cause. Hooks run in ascending order, 0 by default: raise it for a hook that must observe what the others generated.

Create a hook

runHookCommand from @putnami/utils/hooks parses the arguments, reads the context and writes the events and the summary:

import { completeHttpRoutesEvaluation } from '@putnami/application/http-routes';
import { runHookCommand, standardHookModel } from '@putnami/utils/hooks';

await runHookCommand({
  model: standardHookModel.extend({ name: 'generate' }),
  extension: '@my/extension',
  hook: 'preBuild',
  run: async (_options, context) => {
    // Write generated sources under context.outputRoot.
    return {
      exports: { './generated': `${context.outputRoot}/main.ts` },
      data: { httpRoutes: completeHttpRoutesEvaluation() },
    };
  },
});

Declare the hook's entry point in the package.json bin map: besides src/, the build transpiles only the scripts the bin map names, and the npm packager ships them next to the copied putnami.extension.json. A hook script missing from bin ships without its runnable .js.

{
  "name": "@my/extension",
  "bin": {
    "my-extension-generate": "./bin/generate.ts"
  }
}