@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 Default: [] |
data | Files needed at runtime by binaries or tests that transitively depend on this target. Default: [] |
plugins | List of plugin labels created with Default: [] |
output_dir | Whether to produce a directory output rather than individual files. If Default: False |
swcrc | Label of a .swcrc configuration file for the SWC cli, see https://swc.rs/docs/configuration/swcrc Note that some settings in Default: None |
source_maps | If set, the --source-maps argument is passed to the SWC cli with the value. 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 Default: True |
kwargs | additional keyword arguments passed through to underlying |
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 Default: [] |
config | Optional configuration dict for the plugin. This is passed as a JSON object into the Default: {} |
kwargs | additional keyword arguments passed through to underlying rule, eg. |
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
.
Attribute | Type | Description |
---|---|---|
*name | name | A unique name for this target. |
*srcs | list of labels | source files, typically .ts files in the source tree |
args | list 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. Default: [] |
source_maps | string | Create source map files for emitted JavaScript files. see https://swc.rs/docs/usage/cli#--source-maps--s Default: "false" |
source_root | string | 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_dir | boolean | 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. Default: False |
data | list of labels | Runtime dependencies to include in binaries/tests that depend on this target. Follows the same semantics as Default: [] |
swcrc | label | label of a configuration file for swc, see https://swc.rs/docs/configuration/swcrc Default: None |
plugins | list of labels | swc compilation plugins, created with swc_plugin rule Default: [] |
out_dir | string | 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_dir | string | a subdirectory under the input package which should be consider the root directory of all the input files Default: "" |
emit_isolated_dts | boolean | Emit .d.ts files instead of .js for TypeScript sources EXPERIMENTAL: this API is undocumented, experimental and may change without notice Default: False |
default_ext | string | 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 Default: "" |
allow_js | boolean | Allow JavaScript sources to be transpiled. If False, only TypeScript sources will be transpiled. Default: True |
js_outs | list of labels | list of expected JavaScript output files. There should be one for each entry in srcs. Default: [] |
map_outs | list of labels | list of expected source map output files. Can be empty, meaning no source maps should be produced. Default: [] |
dts_outs | list of labels | list of expected TypeScript declaration files. Can be empty, meaning no dts files should be produced. 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_version | version of the swc project, from https://github.com/swc-project/swc/releases Default: None |
swc_version_from | label of a json file which declares an This may be a With rules_js v1.32.0 or greater, it may also be a Exactly one of Default: None |
platforms | list of platforms (must be a key in PLATFORMS) to register toolchains for. 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. Default: True |
kwargs | passed to each swc_repositories call |
Repository Rules
swc_repositories
Fetch external dependencies needed to run the SWC cli
Attribute | Type | Description |
---|---|---|
*name | name | A unique name for this repository. |
repo_mapping | dictionary: String → String | In For example, an entry This attribute is not supported in |
swc_version | string | Explicit version. If provided, the package.json is not read. Default: "" |
swc_version_from | label | Location of package.json which has a version for @swc/core. Default: None |
*platform | string | |
integrity_hashes | dictionary: 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.
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. |