Skip to main content
Version: 1.17.x

js_library

js_library groups together JS sources and arranges them and their transitive and npm dependencies into a provided JsInfo. There are no Bazel actions to run.

For example, this BUILD file groups a pair of .js/.d.ts files along with the package.json. The latter is needed because it contains a typings key that allows downstream users of this library to resolve the one.d.ts file. The main key is another commonly used field in package.json which would require including it in the library.

load("@aspect_rules_js//js:defs.bzl", "js_library")

js_library(
name = "one",
srcs = [
"one.d.ts",
"one.js",
"package.json",
],
)

| This is similar to py_library which depends on | Python sources and provides a PyInfo.

Rules

js_library

A library of JavaScript sources. Provides JsInfo, the primary provider used in rules_js and derivative rule sets.

Declaration files are handled separately from sources since they are generally not needed at runtime and build rules, such as ts_project, are optimal in their build graph if they only depend on declarations from deps since these they don't need the JavaScript source files from deps to typecheck.

Linked npm dependences are also handled separately from sources since not all rules require them and it is optimal for these rules to not depend on them in the build graph.

NB: js_library copies all source files to the output tree before providing them in JsInfo. See https://github.com/aspect-build/rules_js/tree/dbb5af0d2a9a2bb50e4cf4a96dbc582b27567155/docs#javascript for more context on why we do this.

Example usage (generated):

load("@aspect_rules_js//js:defs.bzl", "js_library")

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

name

Required name.

A unique name for this target.

data

Optional list of labels. Default: []

Runtime dependencies to include in binaries/tests that depend on this target.

The transitive npm dependencies, transitive sources, default outputs and runfiles of targets in the data attribute are added to the runfiles of this target. They should appear in the '*.runfiles' area of any executable which has a runtime dependency on this target.

If this list contains linked npm packages, npm package store targets or other targets that provide JsInfo, NpmPackageStoreInfo providers are gathered from 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 when linking a downstream npm_package target with npm_link_package.

declarations

Optional list of labels. Default: []

Same as srcs except all files are also provided as "declarations" available to downstream rules for type checking.

For example, a js_library with only .js files that are intended to be imported as .js files by downstream type checking rules such as ts_project would list those files in declarations:

js_library(
name = "js_lib",
declarations = ["index.js"],
)

deps

Optional list of labels. Default: []

[Must provide JsInfo]

Dependencies of this target.

This may include other js_library targets or other targets that provide JsInfo

The transitive npm dependencies, transitive sources & runfiles of targets in the deps attribute are added to the runfiles of this target. They should appear in the '*.runfiles' area of any executable which is output by or has a runtime dependency on this target.

srcs

Optional list of labels. Default: []

Source files that are included in this library.

This includes all your checked-in code and any generated source files.

The transitive npm dependencies, transitive sources & runfiles of targets in the srcs attribute are added to the runfiles of this target. They should appear in the '*.runfiles' area of any executable which is output by or has a runtime dependency on this target.

Source files that are JSON files, declaration files or directory artifacts will be automatically provided as "declarations" available to downstream rules for type checking. To explicitly provide source files as "declarations" available to downstream rules for type checking that do not match these criteria, move those files to the declarations attribute instead.

Macros and Functions

js_library_lib.implementation

Example usage (generated):

load("@aspect_rules_js//js:defs.bzl", "js_library_lib")

js_library_lib.implementation(
ctx = None,
)

ctx

Required.