Rules for linking npm dependencies and packaging and linking first-party deps.

Load these with,

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

Rules

npm_package

A rule that packages sources into a directory (a tree artifact) and provides an NpmPackageInfo.

This target can be used as the src attribute to npm_link_package.

npm_package makes use of copy_to_directory (https://github.com/aspect-build/bazel-lib/blob/main/docs/copy_to_directory.md) under the hood, adopting its API and its copy action using composition. However, unlike copy_to_directory, npm_package includes transitive_sources and transitive_declarations files from JsInfo providers in srcs.

The default include_srcs_packages, [".", "./**"], prevents files from outside of the target's package and subpackages from being included.

The default exclude_srcs_patterns, of ["node_modules/**", "**/node_modules/**"], prevents node_modules files from being included.

To stamp the current git tag as the "version" in the package.json file, see stamped_package_json

Example usage (generated)

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

npm_package(
    # A unique name for this target.
    name = "",
)

name

A unique name for this target.

allow_overwrites

If True, allow files to be overwritten if the same output file is copied to twice.

If set, then the order of srcs matters as the last copy of a particular file will win.

This setting has no effect on Windows where overwrites are always allowed.

data

Runtime / linktime npm dependencies of this npm package.

NpmPackageStoreInfo providers are gathered from JsInfo of the targets specified. Targets can be linked npm packages, npm package store targets or other targets that provide JsInfo. This is done directly from the npm_package_store_deps field of these. For linked npm package targets, the underlying npm_package_store target(s) that back the links is used.

Gathered NpmPackageStoreInfo providers are used downstream as direct dependencies of this npm package when linking with npm_link_package.

exclude_srcs_packages

List of Bazel packages (with glob support) to exclude from output directory.

    Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
    more details on how to use glob patterns:
    https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

    Files and directories in srcs are not copied to the output directory if
    the Bazel package of the file or directory matches one of the patterns specified.

    Forward slashes (`/`) should be used as path separators.

    A `"."` value means exclude srcs that are in the target's package.
    It expands to the target's package path (`ctx.label.package`). This
    will be an empty string `""` if the target is in the root package.

    A `"./**"` value means exclude srcs that are in subpackages of the target's package.
    It expands to the target's package path followed by a slash and a
    globstar (`"{}/**".format(ctx.label.package)`). If the target's package is
    the root package, `"./**"` expands to `["?*", "?*/**"]` instead.

    Files and directories that have do not have matching Bazel packages are subject to subsequent
    filters and transformations to determine if they are copied and what their path in the output
    directory will be.

    Filters and transformations are applied in the following order:
  1. include_external_repositories

  2. include_srcs_packages

  3. exclude_srcs_packages

  4. root_paths

  5. include_srcs_patterns

  6. exclude_srcs_patterns

  7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute.

exclude_srcs_patterns

List of paths (with glob support) to exclude from output directory.

Glob patterns **, * and ? are supported. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

Files and directories in srcs are not copied to the output directory if their output directory path, after applying root_paths, matches one of the patterns specified.

Patterns do not look into files within source directory or generated directory (TreeArtifact) targets since matches are performed in Starlark. To use include_srcs_patterns on files within directories you can use the make_directory_paths helper to specify individual files inside directories in srcs. This restriction may be fixed in a future release by performing matching inside the copy action instead of in Starlark.

Forward slashes (/) should be used as path separators.

Defaults to ["node_modules/", "/node_modules/**"] which excludes all node_modules folders from the output directory.

Files and directories that do not have matching output directory paths are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.

See copy_to_directory_action documentation for list of order of filters and transformations: https://github.com/aspect-build/bazel-lib/blob/main/docs/copy_to_directory.md#copy_to_directory.

include_external_repositories

List of external repository names (with glob support) to include in the output directory.

    Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
    more details on how to use glob patterns:
    https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

    Files from external repositories are only copied into the output directory if
    the external repository they come from matches one of the external repository patterns
    specified.

    When copied from an external repository, the file path in the output directory
    defaults to the file's path within the external repository. The external repository
    name is _not_ included in that path.

    For example, the following copies `@external_repo//path/to:file` to
    `path/to/file` within the output directory.

    ```
    copy_to_directory(
        name = "dir",
        include_external_repositories = ["external_*"],
        srcs = ["@external_repo//path/to:file"],
    )
    ```

    Files and directories that come from matching external are subject to subsequent filters and
    transformations to determine if they are copied and what their path in the output
    directory will be. The external repository name of the file or directory from an external
    repository is not included in the output directory path and is considered in subsequent
    filters and transformations.

    Filters and transformations are applied in the following order:
  1. include_external_repositories

  2. include_srcs_packages

  3. exclude_srcs_packages

  4. root_paths

  5. include_srcs_patterns

  6. exclude_srcs_patterns

  7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute.

include_srcs_packages

List of Bazel packages (with glob support) to include in output directory.

Glob patterns **, * and ? are supported. See glob_match documentation for more details on how to use glob patterns: https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

Files and directories in srcs are only copied to the output directory if the Bazel package of the file or directory matches one of the patterns specified.

Forward slashes (/) should be used as path separators.

A "." value expands to the target's package path (ctx.label.package). A "./**" value expands to the target's package path followed by a slash and a globstar ("{{}}/**".format(ctx.label.package)).

Defaults to [".", "./**"] which includes sources target's package and subpackages.

Files and directories that have matching Bazel packages are subject to subsequent filters and transformations to determine if they are copied and what their path in the output directory will be.

See copy_to_directory_action documentation for list of order of filters and transformations: https://github.com/aspect-build/bazel-lib/blob/main/docs/copy_to_directory.md#copy_to_directory.

include_srcs_patterns

List of paths (with glob support) to include in output directory.

    Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
    more details on how to use glob patterns:
    https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

    Files and directories in srcs are only copied to the output directory if their output
    directory path, after applying `root_paths`, matches one of the patterns specified.

    Patterns do not look into files within source directory or generated directory (TreeArtifact)
    targets since matches are performed in Starlark. To use `include_srcs_patterns` on files
    within directories you can use the `make_directory_paths` helper to specify individual files inside
    directories in `srcs`. This restriction may be fixed in a future release by performing matching
    inside the copy action instead of in Starlark.

    Forward slashes (`/`) should be used as path separators.

    Defaults to ["**"] which includes all sources.

    Files and directories that have matching output directory paths are subject to subsequent
    filters and transformations to determine if they are copied and what their path in the output
    directory will be.

    Filters and transformations are applied in the following order:
  1. include_external_repositories

  2. include_srcs_packages

  3. exclude_srcs_packages

  4. root_paths

  5. include_srcs_patterns

  6. exclude_srcs_patterns

  7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute.

out

Path of the output directory, relative to this package.

If not set, the name of the target is used.

package

The package name. If set, should match the name field in the package.json file for this package.

If set, the package name set here will be used for linking if a npm_link_package does not specify a package name. A npm_link_package that specifies a package name will override the value here when linking.

If unset, a npm_link_package that references this npm_package must define the package name must be for linking.

replace_prefixes

Map of paths prefixes (with glob support) to replace in the output directory path when copying files.

    Glob patterns `**`, `*` and `?` are supported but the pattern must not end with a `**` glob
    expression. See `glob_match` documentation for more details on how to use glob patterns:
    https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

    If the output directory path for a file or directory starts with or fully matches a
    a key in the dict then the matching portion of the output directory path is
    replaced with the dict value for that key. The final path segment
    matched can be a partial match of that segment and only the matching portion will
    be replaced. If there are multiple keys that match, the longest match wins.

    Patterns do not look into files within source directory or generated directory (TreeArtifact)
    targets since matches are performed in Starlark. To use `replace_prefixes` on files
    within directories you can use the `make_directory_paths` helper to specify individual files inside
    directories in `srcs`. This restriction may be fixed in a future release by performing matching
    inside the copy action instead of in Starlark.

    Forward slashes (`/`) should be used as path separators. 

    Replace prefix transformation are the final step in the list of filters and transformations.
    The final output path of a file or directory being copied into the output directory
    is determined at this step.

    Filters and transformations are applied in the following order:
  1. include_external_repositories

  2. include_srcs_packages

  3. exclude_srcs_packages

  4. root_paths

  5. include_srcs_patterns

  6. exclude_srcs_patterns

  7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute.

root_paths

List of paths (with glob support) that are roots in the output directory.

    Glob patterns `**`, `*` and `?` are supported. See `glob_match` documentation for
    more details on how to use glob patterns:
    https://github.com/aspect-build/bazel-lib/blob/main/docs/glob_match.md.

    If any parent directory of a file or directory being copied matches one of the root paths
    patterns specified, the output directory path will be the path relative to the root path
    instead of the path relative to the file's or directory's workspace. If there are multiple
    root paths that match, the longest match wins.

    Matching is done on the parent directory of the output file path so a trailing '**' glob patterm
    will match only up to the last path segment of the dirname and will not include the basename.
    Only complete path segments are matched. Partial matches on the last segment of the root path
    are ignored.

    Forward slashes (`/`) should be used as path separators.

    A "." value expands to the target's package path (`ctx.label.package`).

    Defaults to ["."] which results in the output directory path of files in the
    target's package and and sub-packages are relative to the target's package and
    files outside of that retain their full workspace relative paths.

    Filters and transformations are applied in the following order:
  1. include_external_repositories

  2. include_srcs_packages

  3. exclude_srcs_packages

  4. root_paths

  5. include_srcs_patterns

  6. exclude_srcs_patterns

  7. replace_prefixes

For more information each filters / transformations applied, see the documentation for the specific filter / transformation attribute.

srcs

Files and/or directories or targets that provide DirectoryPathInfo to copy into the output directory.

version

The package version. If set, should match the version field in the package.json file for this package.

If set, a npm_link_package may omit the package version and the package version set here will be used for linking. A npm_link_package that specifies a package version will override the value here when linking.

If unset, a npm_link_package that references this npm_package must define the package version must be for linking.


Macros and Functions

npm_package_lib.implementation

Example usage (generated)

load("@aspect_rules_js//npm:defs.bzl", "npm_package_lib")

npm_package_lib.implementation(
    ctx = None,
)

ctx


stamped_package_json

Convenience wrapper to set the "version" property in package.json with the git tag.

In unstamped builds (typically those without --stamp) the version will be set to 0.0.0. This ensures that actions which use the package.json file can get cache hits.

For more information on stamping, read https://github.com/aspect-build/bazel-lib/blob/main/docs/stamping.md.

Using this rule requires that you register the jq toolchain in your WORKSPACE:

load("@aspect_bazel_lib//lib:repositories.bzl", "register_jq_toolchains")

register_jq_toolchains()

Example usage (generated)

load("@aspect_rules_js//npm:defs.bzl", "stamped_package_json")

stamped_package_json(
    # name of the resulting `jq` target, must be "package"
    name = "",
    # a key from the bazel-out/stable-status.txt or bazel-out/volatile-status.txt files
    stamp_var = None,
)

name

name of the resulting jq target, must be "package"

stamp_var

a key from the bazel-out/stable-status.txt or bazel-out/volatile-status.txt files

kwargs

additional attributes passed to the jq rule, see https://github.com/aspect-build/bazel-lib/blob/main/docs/jq.md