Skip to main content
Version: 0.21.x

swc

API for running the SWC cli under Bazel

The simplest usage is a single line. It relies on the swcrc attribute automatically discovering .swcrc and the srcs attribute default of glob(["**/*.ts", "**/*.tsx"]):

load("@aspect_rules_swc//swc:defs.bzl", "swc")

swc(name = "compile")

Rules

swc_compile

Underlying rule for the swc macro.

Most users should use swc instead, as it predicts the output files and has convenient default values.

Use this if you need more control over how the rule is called, for example to set your own output labels for js_outs.

Example usage (generated):

load("@aspect_rules_swc//swc:defs.bzl", "swc_compile")

swc_compile(
# A unique name for this target.
name = "",
# source files, typically .ts files in the source tree
srcs = [],
)

name

Required name.

A unique name for this target.

args

Optional list of strings. Default: []

Additional arguments to pass to swcx cli (NOT swc!).

NB: this is not the same as the CLI arguments for @swc/cli npm package. For performance, rules_swc does not call a Node.js program wrapping the swc rust binding. Instead, we directly spawn the (somewhat experimental) native Rust binary shipped inside the @swc/core npm package, which the swc project calls "swcx" Tracking issue for feature parity: https://github.com/swc-project/swc/issues/4017

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.

js_outs

Optional list of labels.

list of expected JavaScript output files.

There should be one for each entry in srcs.

map_outs

Optional list of labels.

list of expected source map output files.

Can be empty, meaning no source maps should be produced. If non-empty, there should be one for each entry in srcs.

out_dir

Optional string. Default: ""

base directory for output files

output_dir

Optional boolean. Default: False

whether to produce a directory output rather than individual files

root_dir

Optional string. Default: ""

a subdirectory under the input package which should be consider the root directory of all the input files

source_maps

Optional string. Default: "false"

see https://swc.rs/docs/usage/cli#--source-maps--s

srcs

Required list of labels.

source files, typically .ts files in the source tree

swcrc

Optional label. Default: None

label of a configuration file for swc, see https://swc.rs/docs/configuration/swcrc

Macros and Functions

swc

Execute the SWC compiler

Example usage (generated):

load("@aspect_rules_swc//swc:defs.bzl", "swc")

swc(
# A name for this target
name = "",
)

name

Required.

A name for this target

srcs

Optional. Default: None

List of labels of TypeScript source files.

args

Optional. Default: []

Additional options to pass to swcx cli, see https://github.com/swc-project/swc/discussions/3859 Note: we do not run the NodeJS wrapper @swc/cli

data

Optional. Default: []

Files needed at runtime by binaries or tests that transitively depend on this target. See https://bazel.build/reference/be/common-definitions#typical-attributes

output_dir

Optional. Default: False

Whether to produce a directory output rather than individual files

swcrc

Optional. Default: None

Label of a .swcrc configuration file for the SWC cli, see https://swc.rs/docs/configuration/swcrc Instead of a label, you can pass a dictionary matching the JSON schema. If this attribute isn't specified, and a file .swcrc exists in the same folder as this rule, it is used.

Note that some settings in .swcrc also appear in tsconfig.json. We recommend adding an assert_json_matches rule to guarantee these don't accidentally diverge. See an example in examples/paths/BUILD.bazel.

source_maps

Optional. Default: False

If set, the --source-maps argument is passed to the SWC cli with the value. See https://swc.rs/docs/usage/cli#--source-maps--s. True/False are automaticaly converted to "true"/"false" string values the cli expects.

out_dir

Optional. Default: None

The base directory for output files relative to the output directory for this package

root_dir

Optional. Default: None

A subdirectory under the input package which should be considered the root directory of all the input files

kwargs

Optional.

additional keyword arguments passed through to underlying swc_compile, eg. visibility, tags