npm packaging
Bazel

npm packaging


Overview

Note, this is intended for sharing library code with non-Bazel consumers.

If all users of your library code use Bazel, they should just add your library to the deps of one of their targets.


create_package

create_package(ctx, devmode_sources, nested_packages)

Creates an action that produces the npm package.

It copies srcs and deps into the artifact and produces the .pack and .publish scripts.

Returns: The tree artifact which is the publishable directory.

Attributes

ctx

Unknown; Required

the skylark rule context

devmode_sources

Unknown; Required

the .js files which belong in the package

nested_packages

Unknown; Required

list of TreeArtifact outputs from other actions which are to be nested inside this package


npm_package

npm_package(name, deps, srcs, packages, replacements)

The npm_package rule creates a directory containing a publishable npm artifact.

load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")

npm_package(
    name = "my_package",
    srcs = ["package.json"],
    deps = [":my_typescript_lib"],
    replacements = {"//internal/": "//"},
)

You can use a pair of // BEGIN-INTERNAL ... // END-INTERNAL comments to mark regions of files that should be elided during publishing. For example:

function doThing() {
    // BEGIN-INTERNAL
    // This is a secret internal-only comment
    doInternalOnlyThing();
    // END-INTERNAL
}

Usage:

npm_package yields three labels. Build the package directory using the default label:

$ bazel build :my_package
Target //:my_package up-to-date:
  bazel-out/fastbuild/bin/my_package
$ ls -R bazel-out/fastbuild/bin/my_package

Dry-run of publishing to npm, calling npm pack (it builds the package first if needed):

$ bazel run :my_package.pack
INFO: Running command line: bazel-out/fastbuild/bin/my_package.pack
my-package-name-1.2.3.tgz
$ tar -tzf my-package-name-1.2.3.tgz

Actually publish the package with npm publish (also builds first):

# Check login credentials
$ bazel run @nodejs//:npm who
# Publishes the package
$ bazel run :my_package.publish

You can pass arguments to npm by escaping them from Bazel using a double-hyphen bazel run my_package.publish -- --tag=next

Attributes

name

Name; Required

A unique name for this rule.

deps

List of labels; Optional; Default is []

Other targets which produce files that should be included in the package, such as rollup_bundle

srcs

List of labels; Optional; Default is []

Files inside this directory which are simply copied into the package.

packages

List of labels; Optional; Default is []

Other npm_package rules whose content is copied into this package.

replacements

Dictionary mapping strings to string; Optional; Default is {}

Key-value pairs which are replaced in all the files while building the package. Note that the special value 0.0.0-PLACEHOLDER is always replaced with the version stamp data. See the section on stamping in the README.