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 TreeArtifact or forwards a tree artifact and provides a NpmPackageInfo.

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

A DeclarationInfo is also provided so that the target can be used as an input to rules that expect one such as ts_project.

Example usage (generated)

load("@aspect_rules_js//npm/private:npm_package.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.

exclude_prefixes

List of path prefixes to exclude from output directory.

If the output directory path for a file or directory starts with or is equal to a path in the list then that file is not copied to the output directory.

Exclude prefixes are matched before replace_prefixes are applied.

include_external_repositories

List of external repository names to include in the output directory.

Files from external repositories are not copied into the output directory unless the external repository they come from is listed here.

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_repo"],
    srcs = ["@external_repo//path/to:file"],
)

Files from external repositories are subject to root_paths, exclude_prefixes and replace_prefixes in the same way as files form the main repository.

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 target that specifies a package name will override the value here when linking.

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

replace_prefixes

Map of paths prefixes to replace in the output directory path when copying files.

If the output directory path for a file or directory starts with or is equal to a key in the dict then the matching portion of the output directory path is replaced with the dict value for that key.

Forward slashes (/) should be used as path separators. The final path segment of the key can be a partial match in the corresponding segment of the output directory path.

If there are multiple keys that match, the longest match wins.

root_paths

List of paths that are roots in the output directory.

"." values indicate the targets package path.

If a file or directory being copied is in one of the listed paths or one of its subpaths, the output directory path is the path relative to the root path instead of the path relative to the file's workspace.

Forward slashes (/) should be used as path separators. Partial matches on the final path segment of a root path against the corresponding segment in the full workspace relative path of a file are not matched.

If there are multiple root paths that match, the longest match wins.

Defaults to [package_name()] so that 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.

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 target that specifies a package version will override the value here when linking.

If unset, a npm_link_package target 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/private:npm_package.bzl", "npm_package_lib")

npm_package_lib.implementation(
    ctx = None,
)

ctx


Providers

NpmPackageInfo

A provider that carries the output directory (a TreeArtifact) of an npm package which contains the packages sources along with the package name and version