Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run bare rust CLI binary, no node #57

Merged
merged 10 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
# Settings that apply only to CI are in .github/workflows/ci.bazelrc

build --enable_runfiles
# Workaround for https://github.com/swc-project/swc/issues/4057
# Turn off sandboxing so SWC doesn't observe symlinked inputs.
build --modify_execution_info=SWCTranspile=+no-sandbox

# Load any settings specific to the current user.
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Bazel rules for swc

SWC (<https://swc.rs/>) is a fast JS compiler, written in Rust.
SWC (<https://swc.rs/>) is a fast JavaScript compiler, written in Rust.
It performs the same work as Babel among other things, but is 20x faster.

SWC is a natural fit with Bazel.
Expand All @@ -20,7 +20,7 @@ _Need help?_ This ruleset has support provided by https://aspect.dev.

## Features

These rules provide a hermetic toolchain that runs `@swc/cli`, so it doesn't matter what is
These rules provide a hermetic toolchain that runs the SWC cli, so it doesn't matter what is
already installed on a developer's machine, they're guaranteed to get the same result.
It caches all the tools using Bazel's downloader.
This means that even when Bazel determines that a repository is invalidated and re-runs the setup
Expand Down Expand Up @@ -52,7 +52,7 @@ copy the WORKSPACE snippet into your `WORKSPACE` file.

### From a BUILD file

The simplest usage is with the [swc rule](/docs/swc.md), used to transpile TypeScript code to JavaScript in a tight developer loop. Each `.ts` or `.tsx` file is compiled to `bazel-bin/path/to/file.js` and available to downstream
The simplest usage is with the [swc rule](/docs/swc.md), used to compile TypeScript code to JavaScript in a tight developer loop. Each `.ts` or `.tsx` file is compiled to `bazel-bin/path/to/file.js` and available to downstream
tools such as bundlers, which are in their own Bazel rules.

See the example in /examples/simple.
Expand All @@ -75,13 +75,11 @@ You'll basically follow the example of /swc/private/swc.bzl in this repo, by usi
the `ctx.actions.run` Starlark API.

- Use `@aspect_rules_swc//swc:cli` as the binary tool to execute
- To get the swc nodejs bindings for Rust, `env` should include
`"SWC_BINARY_PATH": ctx.toolchains["@aspect_rules_swc//swc:toolchain_type"].swcinfo.binding`
- To pass the relevant files to the action, `inputs` should include
`ctx.toolchains["@aspect_rules_swc//swc:toolchain_type"].swcinfo.tool_files`

You can load helper functions from the private API like our implementation does,
but note that this may have breaking changes between major releases.

Alternatively you can write a rule from scratch, using the toolchains and
swc cli binary provided in aspect_rules_swc.
SWC cli provided in aspect_rules_swc.
20 changes: 15 additions & 5 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,29 @@ load("//swc:repositories.bzl", "swc_register_toolchains")

swc_register_toolchains(
name = "default_swc",
node_repository = "node16",
swc_version = "v1.2.168",
# Demonstrates how users can choose ANY swc version, not just the ones we mirrored
integrity_hashes = {
"darwin-arm64": "sha384-4TRcW8Nb2g/3zQ0T4aS+NSjMbUaIHaGxofkA67CcAQZbeK1uDmjXFh5uJw69ZV07",
"darwin-x64": "sha384-uh5Pt06OgF/bhQaq09/SO0hNa9x/isZ3O9R+3uwseSdbL7OentceFHZ/zTr9Y+xL",
"linux-arm64-gnu": "sha384-2uYEohzUAAalEGiD0iEPEDWlR1BCM4q+DhsnIe5saa8B2+igdLzsQrbREimhu5TJ",
"linux-x64-gnu": "sha384-c46sYs/jCA4OjV7LeWT201dYQDTO76V6sZzdtOiGt+3Gls9n6QG+K4hkpIE+wy0k",
},
swc_version = "v1.3.25",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")

aspect_bazel_lib_dependencies(override_local_config_platform = True)

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")
load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")

rules_js_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "node16",
node_version = "16.9.0",
name = "nodejs",
node_version = DEFAULT_NODE_VERSION,
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
Expand Down
65 changes: 29 additions & 36 deletions docs/swc.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions e2e/workspace/BUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@aspect_rules_swc//swc:defs.bzl", "swc")

swc(name = "transpile")
swc(name = "compile")

build_test(
name = "test",
targets = [":transpile"],
targets = [":compile"],
)
13 changes: 2 additions & 11 deletions e2e/workspace/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ load("@aspect_rules_swc//swc:dependencies.bzl", "rules_swc_dependencies")

rules_swc_dependencies()

# Fetches a pre-built Rust-node binding from
# https://github.com/swc-project/swc/releases.
# Fetches a SWC cli from
# https://github.com/swc-project/swc/releases
# If you'd rather compile it from source, you can use rules_rust, fetch the project,
# then register the toolchain yourself. (Note, this is not yet documented)
load("@aspect_rules_swc//swc:repositories.bzl", "LATEST_VERSION", "swc_register_toolchains")
Expand All @@ -29,12 +29,3 @@ swc_register_toolchains(
name = "swc",
swc_version = LATEST_VERSION,
)

# Fetches a NodeJS interpreter, needed to run the swc CLI.
# You can skip this if you already register a nodejs toolchain.
load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "nodejs",
node_version = DEFAULT_NODE_VERSION,
)
40 changes: 33 additions & 7 deletions examples/custom_outs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
"""Demonstrate that js_outs can go to whatever file extensions you like."""

load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@aspect_rules_swc//swc:defs.bzl", "swc_transpiler")
load("@aspect_rules_swc//swc:defs.bzl", "swc_compile")
load("@bazel_skylib//rules:write_file.bzl", "write_file")

# Note, --config is documented on swcx CLI but isn't functional. See
# https://github.com/swc-project/swc/issues/4017#issuecomment-1374141572
# In the call to swc_compile below, we ought to be able to use
# `args = ["--config", "module.format=" + format]`
# As a workaround, write our config to an rc file. Can be changed back when swcx supports --config.
[
write_file(
name = "_write_swcrc_" + format,
out = "swcrc_" + format,
content = [json.encode({
"jsc": {
"parser": {
"syntax": "typescript",
},
},
"module": {
"type": format,
},
})],
)
for format in [
"commonjs",
"es6",
]
]

[
swc_transpiler(
name = "transpile_" + format,
swc_compile(
name = "compile_" + format,
srcs = ["a.ts"],
args = [
"--config",
"module.type=" + format,
],
# The extension of the outputs can be modified using js_outs
js_outs = [format + "/a." + ("cjs" if format == "commonjs" else "js")],
out_dir = format,
swcrc = "swcrc_" + format,
)
for format in [
"commonjs",
Expand Down
11 changes: 6 additions & 5 deletions examples/custom_outs/expected.cjs
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.a = void 0;
Object.defineProperty(exports, "a", {
enumerable: true,
get: function() {
return a;
}
});
var a = "a";
exports.a = a;


//# sourceMappingURL=a.cjs.map
3 changes: 0 additions & 3 deletions examples/custom_outs/expected.js
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
export var a = "a";


//# sourceMappingURL=a.js.map