Node rules
The Node family lays out node_modules from your package-lock.json and runs programs of those packages (tsc, jest, cdk) with a pinned Node.js. There is no npm and no network in any action.
Load the lockfile-driven rules from @buildrules//rules/node:modules.bzl. The single-package rules are in @buildrules//rules/node:defs.bzl.
npm_modules #
npm_modules(name, lockfile, npm)node_modules laid out from lockfile (a lockfile version 3 package-lock.json) and npm, the NPM dict of a pins.lock.bzl. It is the offline equivalent of npm ci --ignore-scripts for Linux x86_64:
- every package is a download pinned by sha256, and its sha512 must equal the lockfile's
integrity; - each is unpacked at the path the lockfile gives, nested
node_modulesincluded, and itspackage.jsonmust state the locked name and version; .binlinks are made for every package with executables;- optional packages for another operating system or CPU (such as
fsevents) are skipped; - no install script runs.
A lockfile package with no download, a changed byte or a mismatched name fails the target.
Programs over node_modules #
These macros run a program of the node_modules tree with the pinned node, in a copy of srcs and data, with a scratch HOME, UTC, no network and nothing else inherited.
| Rule | Runs | Target is |
|---|---|---|
tsc_check(name, modules, srcs, data, project) | tsc --noEmit -p <project> | a pass marker; a type error fails it |
tsc_build(name, modules, srcs, data, project, out_dir) | tsc -p <project> | the out_dir the project writes |
jest_test(name, modules, srcs, data, config, max_workers, coverage) | jest --ci with its cache in the scratch copy and no watchman; with coverage, --coverage too | a pass marker; a failing test fails it. With coverage, the coverage directory the run writes (a coverage-summary.json in it) |
cdk_synth(name, modules, srcs, app, data, context, env) | cdk synth of app, with no AWS and no network | a pass marker |
node_tree_test(name, modules, program, args, srcs, data, env, tools) | any program of node_modules or the package | a pass marker if it exits 0 |
node_tree_build(name, modules, program, args, srcs, data, env, out_dir) | the same, with a directory result | the out_dir the program writes |
data is a dict of {destination: source} from the repository root; a source can be the output of another target, which is how a build chains: this site compiles its build program with tsc_build, then runs it with node_tree_build to produce dist/. Because a test is a build, buck2 build //:test runs it, and a cached rebuild means it passed on identical inputs.
Single packages and scripts #
| Rule | What it does |
|---|---|
node_dist | unpacks a pinned Node.js release and fails unless its node runs and prints the pinned version |
npm_package(path, table, node, srcs, exclude, data, step_data, step_tools, step_env, visibility) | the front door, described below |
npm_tarball | unpacks one pinned npm tarball after checking its sha512 and the name and version in its package.json. This is the rule that npm_package used to be called; the name now belongs to the front door. |
node_test | runs one script with the pinned node; the output exists only if it exited 0, or, with expect_error, failed with that text |
esbuild_bundle | bundles an entry module and the packages it uses into one file with the pinned esbuild |
c_shared_lib | builds C sources into a shared library with the pinned Zig, for Node-API addons |
Pins #
The pins.lock.bzl next to your lockfile is generated by kpin, which reads package-lock.json and writes the download URL, sha256, size and sha512 of each package. kpin check fails if the file is stale, and fails with a separate exit code if a hash changed for a version that was already locked. See Getting started.
The npm_package front door #
buildrules npm sync PATH writes the table (buildrules.lock.bzl) and the glue (buildrules.bzl) next to package.json and package-lock.json, and a BUCK file if there is none:
load(":buildrules.bzl", "npm_package")
npm_package(path = ".")The lockfile decides what is installed and package.json decides what is run. path must be .: the BUCK file sits next to package.json. The other arguments: node (a node_dist of your own), srcs and exclude (the files the steps read), data (files from elsewhere in the repository), step_data, step_tools and step_env (the same, for one step), and visibility. npm_package cannot be called without the table, which the generated buildrules.bzl binds.
| Target | What it is |
|---|---|
:node_modules | the tree npm ci --ignore-scripts makes, one cached action per locked tarball |
:typecheck, :lint, :test, :build | the package.json script of that name, run by the pinned Node.js. A step the package has no script for fails when built by name and is skipped by a wildcard. |
:dist | the directory the build script writes, named by the dist policy key |
:check | the table against package.json and package-lock.json; every step carries it as a validation |
| one per action | each entry of the actions policy key, a named chain of steps |
The "buildrules" key of package.json, which npm ignores, holds the policy: steps renames the script a step runs, actions names chains of steps, and dist names the output directory. Any other key is refused. A script is read without a shell: &&, quotes and leading NAME=value are understood, and a pipe, a redirection, ;, ||, $, a glob, npm or npx is refused with the fix.