Skip to main content

nodejs_library

A Node.js library that packages the artifacts it is given as-is (no compilation): whatever produced them (a TypeScript build step emitting JavaScript, plain JavaScript sources, generated files) feeds this rule, and the result must be loadable by Node as a regular node_modules package without custom loaders. Each entry of srcs is staged under dist/<short_path> inside the package (repeating the same source is allowed; distinct sources mapping to the same staged path fail, and staged paths must not nest, e.g. dist/a and dist/a/b fail). Each entry of resources is staged the same way, for non-code files (including generated artifacts and directory artifacts from other rules) that ship with the package. deps must be nodejs_library targets; use resources for non-code artifacts. Staging copies files so the package runs under Node without --preserve-symlinks. package_json, when supplied, is staged at the package root as package.json. Its name is not checked against package_name; keep the two in sync. Without it, importing by package name does not resolve in Node; for packages that must be imported by name, supply package_json whose exports (or main) entry points at the staged file, e.g. {"exports": {".": "./dist/<file>"}}. package_name (default: target name): a valid npm package name of at most 214 characters, name or @scope/name, where each segment contains only lowercase letters, digits, -, ., _, ~ and does not start with . or _. The unscoped names node_modules and favicon.ico are reserved and rejected, as is @scope/node_modules; either may be used as the scope. The default output is the package's own directory (package_dir). The [node_modules] sub-target is the merged node_modules tree for this package and its transitive deps, laid out by package_name. A duplicate package_name in the transitive closure with a different package output fails the build.

Function Signature​

def nodejs_library(
*,
name: str,
default_target_platform: None | str = None,
target_compatible_with: list[str] = [],
compatible_with: list[str] = [],
exec_compatible_with: list[str] = [],
visibility: list[str | target_name_glob] = [],
within_view: list[str | target_name_glob] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
_apple_platforms: dict[str, str] = {},
contacts: list[str] = [],
deps: list[str] = [],
labels: list[str] = [],
package_json: None | str = None,
package_name: None | str = None,
resources: list[str] = [],
srcs: list[str] = [],
) -> None

Parameters​

  • name: (required)

    name of the target

  • default_target_platform: (defaults to: None)

    specifies the default target platform, used when no platforms are specified on the command line

  • target_compatible_with: (defaults to: [])

    a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • compatible_with: (defaults to: [])

    a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • exec_compatible_with: (defaults to: [])

    a list of constraints that are required to be satisfied for this target to be compatible with an execution platform

  • visibility: (defaults to: [])

    a list of visibility patterns restricting what targets can depend on this one

  • within_view: (defaults to: ["PUBLIC"])

    a list of visibility patterns restricting what this target can depend on

  • metadata: (defaults to: {})

    a key-value map of metadata associated with this target

  • tests: (defaults to: [])

    a list of targets that provide tests for this one

  • modifiers: (defaults to: [])

    an array of modifiers associated with this target

  • contacts: (defaults to: [])

    A list of organizational contacts for this rule. These could be individuals who you would contact in the event of a failure or other issue with the rule.

    contacts = [ 'Joe Sixpack', 'Erika Mustermann' ]
  • labels: (defaults to: [])

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query().