aspect_rules_swcAPI docs @2.5.0

@aspect_rules_swc//examples/source_map_support:defs.bzl

Macro wrappers around rules_js's js_binary and js_test that improve the DX of stack traces by automatically
registering source-map-support and removing the runfiles directory prefix.

Use them wherever you would use rules_js's js_binary and js_test.

Functions & Macros

js_binary
Parameters
data
Default: []
chdir
Default: None
node_options
Default: []
kwargs
js_test
Parameters
data
Default: []
chdir
Default: None
node_options
Default: []
kwargs

@aspect_rules_swc//swc:defs.bzl

API for running the SWC cli under Bazel

The simplest usage relies on the swcrc attribute automatically discovering .swcrc:

load("@aspect_rules_swc//swc:defs.bzl", "swc") swc( name = "compile", srcs = ["file.ts"], )

Functions & Macros

swc

Execute the SWC compiler

Parameters
*name

A name for this target

*srcs

List of labels of TypeScript source files.

args

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

Default: []
data

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

Default: []
plugins

List of plugin labels created with swc_plugin.

Default: []
output_dir

Whether to produce a directory output rather than individual files.

If out_dir is set, then that is used as the name of the output directory.
Otherwise, the output directory is named the same as the target.

Default: False
swcrc

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.
See the notes in [/docs/tsconfig.md].

Default: None
source_maps

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.

Default: False
out_dir

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

If output_dir is True, then this is used as the name of the output directory.

Default: None
root_dir

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

Default: None
default_ext

The default extension to use for output files. If not set, the default is ".js".

Default: ".js"
allow_js

If True (default), then .js/.mjs/.cjs input files are transpiled. If False,
they are ignored. This can be used to mimic the behavior of tsc when using ts_project(transpiler).

Default: True
kwargs

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

swc_plugin

Configure an SWC plugin

Parameters
*name

A name for this target

srcs

Plugin files. Either a directory containing a package.json pointing at a wasm file
as the main entrypoint, or a wasm file. Usually a linked npm package target via rules_js.

Default: []
config

Optional configuration dict for the plugin. This is passed as a JSON object into the
jsc.experimental.plugins entry for the plugin.

Default: {}
kwargs

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

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.

AttributeTypeDescription
*namename

A unique name for this target.

*srcslist of labels

source files, typically .ts files in the source tree

argslist of strings

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

Default: []
source_mapsstring

Create source map files for emitted JavaScript files.

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

Default: "false"
source_rootstring

Specify the root path for debuggers to find the reference source code.

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

If not set, then the directory containing the source file is used.

Default: ""
output_dirboolean

Whether to produce a directory output rather than individual files.

If out_dir is also specified, it is used as the name of the output directory.
Otherwise, the directory is named the same as the target.

Default: False
datalist of labels

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

Follows the same semantics as js_library data attribute. See
https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#data for more info.

Default: []
swcrclabel

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

Default: None
pluginslist of labels

swc compilation plugins, created with swc_plugin rule

Default: []
out_dirstring

With output_dir=False, output files will have this directory prefix.

With output_dir=True, this is the name of the output directory.

Default: ""
root_dirstring

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

Default: ""
emit_isolated_dtsboolean

Emit .d.ts files instead of .js for TypeScript sources

EXPERIMENTAL: this API is undocumented, experimental and may change without notice

Default: False
default_extstring

Default extension for output files.

If a source file does not indicate a specific module type, this extension is used.

If unset, extensions will be determined based on the js_outs outputs attribute
or source file extensions.

Default: ""
allow_jsboolean

Allow JavaScript sources to be transpiled.

If False, only TypeScript sources will be transpiled.

Default: True
js_outslist of labels

list of expected JavaScript output files.

There should be one for each entry in srcs.

Default: []
map_outslist 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.

Default: []
dts_outslist of labels

list of expected TypeScript declaration files.

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

Default: []

@aspect_rules_swc//swc:dependencies.bzl

Starlark helper to fetch rules_swc dependencies.

These are needed for local dev, and users must install them as well.
See https://docs.bazel.build/versions/main/skylark/deploying.html#dependencies

Replaced by bzlmod for users of Bazel 6.0 and above.

Functions & Macros

http_archive
Parameters
kwargs
rules_swc_dependencies

@aspect_rules_swc//swc:providers.bzl

Providers for building derivative rules

Providers

SwcPluginConfigInfo

Provides a configuration for an SWC plugin

Fields
label

the label of the target that created this provider

config

the plugin configuration string, encoded from a JSON object

@aspect_rules_swc//swc:repositories.bzl

Repository rules for fetching the swc toolchain.

For typical usage, see the snippets provided in the rules_swc release notes.

Version matching

To keep the swc version in sync with non-Bazel tooling, use swc_version_from.

Currently this only works when a single, pinned version appears, see:
https://github.com/aspect-build/rules_ts/issues/308

For example, package.json:

{ "devDependencies": { "@swc/core": "1.3.37" } }

Allows this in WORKSPACE:

swc_register_toolchains( name = "swc", swc_version_from = "//:package.json", )

Functions & Macros

swc_register_toolchains

Convenience macro for users which does typical setup.

  • create a repository for each built-in platform like "swc_linux_amd64"
  • create a repository exposing toolchains for each platform like "swc_platforms"
  • register a toolchain pointing at each platform
    Users can avoid this macro and do these steps themselves, if they want more control.
Parameters
*name

base name for all created repos; we recommend swc

swc_version

version of the swc project, from https://github.com/swc-project/swc/releases
Exactly one of swc_version or swc_version_from must be set.

Default: None
swc_version_from

label of a json file which declares an @swc/core version.

This may be a package.json file, with "@swc/core" in the dependencies or
devDependencies property, and the version exactly specified.

With rules_js v1.32.0 or greater, it may also be a resolved.json file
produced by npm_translate_lock, such as
@npm//path/to/linked:@swc/core/resolved.json

Exactly one of swc_version or swc_version_from must be set.

Default: None
platforms

list of platforms (must be a key in PLATFORMS) to register toolchains for.
Defaults to all platforms.

Default: ["darwin-arm64", "darwin-x64", "linux-arm64-gnu", "linux-arm64-musl", "linux-x64-gnu", "linux-x64-musl", "win32-arm64-msvc", "win32-ia32-msvc", "win32-x64-msvc"]
register

whether to call through to native.register_toolchains.
Should be True for WORKSPACE users, but false when used under bzlmod extension

Default: True
kwargs

passed to each swc_repositories call

Repository Rules

swc_repositories

Fetch external dependencies needed to run the SWC cli

AttributeTypeDescription
*namename

A unique name for this repository.

repo_mappingdictionary: String → String

In WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).

swc_versionstring

Explicit version. If provided, the package.json is not read.

Default: ""
swc_version_fromlabel

Location of package.json which has a version for @swc/core.

Default: None
*platformstring
integrity_hashesdictionary: String → String

A mapping from platform to integrity hash.

Default: {}

@aspect_rules_swc//swc:toolchain.bzl

This module implements the language-specific toolchain rule.

Rules

swc_toolchain

Defines a swc compiler/runtime toolchain.

For usage see https://docs.bazel.build/versions/main/toolchains.html#defining-toolchains.

AttributeTypeDescription
*namename

A unique name for this target.

target_toollabel

A hermetically downloaded 'swcx' cli for the target platform.

Default: None
target_tool_pathstring

Path to an existing 'swcx' cli for the target platform.

Default: ""

Providers

SwcInfo

Information about how to invoke the tool executable.

Fields
swc_binary

Path to the rust-native 'swcx' cli for the target platform.

tool_files

Files required in runfiles to make the tool executable available.

May be empty if the target_tool_path points to a locally installed tool binary.