Skip to main content
Organizations often set specific standards and conventions for engineers to follow. For example: “Creating a new React component with a certain design standard.”

Templating new source code

Code generators help teams maintain consistent patterns and standards across a codebase. Instead of manually creating files from scratch, you can use templates to generate standardized components.

Example: Using a Yeoman generator

This example uses a Yeoman generator to create a new Fastify route following a predefined template.
1

Run the generator

Run the generator from the Bazel module root (the directory containing MODULE.bazel). npx downloads the two packages to its cache and runs them without adding them to your workspace:
This generator uses Yeoman Generator 3, so pin the compatible Yeoman 4 runner shown above. Current Yeoman 7 releases are not compatible with it on Node.js 22.When prompted for a folder, enter packages/routes. The generator creates index.js, package.json, and BUILD.bazel in that folder.Open the generated BUILD.bazel. Its main target is an npm_package from rules_js:
Unlike js_binary, which launches an application, npm_package assembles an npm-compatible directory tree. That tree can be consumed by other Bazel targets or extended later with publishing rules. The generator therefore captures both the source convention and its Bazel packaging boundary.
2

Update the lockfile and build

The new folder matches the starter’s packages/* workspace pattern. Update only the pnpm lockfile so Bazel discovers the new workspace package, then build it:
Bazel should report //packages/routes:pkg up to date and write the assembled package under bazel-bin/packages/routes/pkg.