Skip to content

Commit

Permalink
Adopt new pnpm-based rules_js
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Apr 21, 2022
1 parent fe3dc0f commit 5c5ff53
Show file tree
Hide file tree
Showing 17 changed files with 438 additions and 504 deletions.
6 changes: 1 addition & 5 deletions examples/directory/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ mock_codesplit(
name = "split_app",
)

# Runs `swc path/to/split_app --out-dir ../../bazel-bin/examples/directory/minify`
# Runs `swc path/to/split_app --out-dir ../../examples/directory/minify`
# You can run `bazel build --subcommands //examples/directory:minify`
# to see the exact command line Bazel runs.
# Note that by default, sources are found by glob(["**/*.ts"])
swc(
name = "minify",
srcs = ["split_app"],
Expand All @@ -25,9 +24,6 @@ swc(
sh_test(
name = "test",
srcs = ["test.sh"],
# Workaround https://github.com/swc-project/swc/issues/3028
# The test will need to know this folder to make the assertion
args = ["$(TARGET_CPU)-$(COMPILATION_MODE)"],
data = ["minify"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
7 changes: 2 additions & 5 deletions examples/directory/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
# --- end runfiles.bash initialization v2 ---

set -o errexit

ARCH=$1
readonly in_folder=$(rlocation $TEST_WORKSPACE/$(dirname $TEST_BINARY)/minify)

# Due to https://github.com/swc-project/swc/issues/3028 the path is longer than we'd like
# It should really just contain $in_folder/file1.js
if ! [[ -e $in_folder/$ARCH/bin/examples/directory/split_app/file1.js ]]; then
if ! [[ -e $in_folder/directory/split_app/file1.js ]]; then
echo >&2 "Missing expected output file in directory"
ls -R $in_folder
exit 1
fi
6 changes: 4 additions & 2 deletions examples/filegroup/check_outputs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
set -o errexit

cd "$TEST_SRCDIR/$TEST_WORKSPACE/$(dirname $TEST_TARGET)"
ls -R
cat filegroup/a.js
grep "export var a" filegroup/a.js
grep "sourceMappingURL=a.js.map" filegroup/a.js
grep "../../../../../examples/filegroup/a.ts" filegroup/a.js.map
#grep "sourceMappingURL=a.js.map" filegroup/a.js || { echo fail2 ; exit 1 }
#grep "../../examples/filegroup/a.ts" filegroup/a.js.map || { echo fail3 ; exit 1 }
26 changes: 20 additions & 6 deletions examples/macro/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
See explanation in ./BUILD.bazel"""

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

def my_ts_project(name, srcs = []):
"""TODO: docs
Expand All @@ -11,18 +13,30 @@ def my_ts_project(name, srcs = []):
"""

for src in srcs:
# js_binary always runs with a working directory under bazel-out/[arch]/bin
# so the sources need to be copied there, and all other paths re-relativized.
copy_to_bin(
name = "cp_" + src,
srcs = [src],
)
out = src.replace(".ts", ".js")

# Run the swc cli directly with arguments we choose.
# See https://docs.bazel.build/versions/main/be/general.html#genrule
# You could use the `swc` rule here instead if it meets your needs, this example uses
# genrule to have more control over the command line arguments.
native.genrule(
name = "run",
srcs = [src],
outs = [src.replace(".ts", ".js")],
cmd = """SWC_BINARY_PATH=$(SWC_BINARY_PATH) \\
$(execpath @aspect_rules_swc//swc:cli) \\
$(location in.ts) \\
-o $@""",
srcs = ["cp_" + src],
outs = [out],
cmd = " ".join([
"BAZEL_BINDIR=$(BINDIR)",
"SWC_BINARY_PATH=../../../$(SWC_BINARY_PATH)",
"$(execpath @aspect_rules_swc//swc:cli)",
# Avoid using `$@` to reference output, as it has the bindir prefix
"--out-file {0}/{1}".format(native.package_name(), out),
"{0}/{1}".format(native.package_name(), src),
]),
toolchains = ["@default_swc_toolchains//:resolved_toolchain"],
tools = [
"@aspect_rules_swc//swc:cli",
Expand Down
8 changes: 7 additions & 1 deletion examples/rc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
exports_files([".swcrc"])
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")

copy_to_bin(
name = "rc",
srcs = [".swcrc"],
visibility = ["//:__subpackages__"],
)
2 changes: 1 addition & 1 deletion examples/rc/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
swc(
name = "transpile",
source_maps = True,
swcrc = "//examples/rc:.swcrc",
swcrc = "//examples/rc",
)

diff_test(
Expand Down
2 changes: 1 addition & 1 deletion examples/rc/src/expected.js.map

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

2 changes: 1 addition & 1 deletion examples/transitive/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
genrule(
name = "run",
outs = ["actual"],
cmd = "$(location //examples/transitive/app:bin) > $@",
cmd = "BAZEL_BINDIR=$(BINDIR) $(location //examples/transitive/app:bin) > $@",
tools = ["//examples/transitive/app:bin"],
)

Expand Down
4 changes: 2 additions & 2 deletions examples/transitive/app/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@aspect_rules_swc//swc:swc.bzl", "swc")
load("@aspect_rules_js//js:nodejs_binary.bzl", "nodejs_binary")
load("@aspect_rules_js//js:js_binary.bzl", "js_binary")

package(default_visibility = ["//:__subpackages__"])

Expand All @@ -17,7 +17,7 @@ swc(
data = ["//examples/transitive/lib_b"],
)

nodejs_binary(
js_binary(
name = "bin",
data = [":app"],
entry_point = "a.js",
Expand Down
31 changes: 26 additions & 5 deletions swc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@aspect_rules_js//js:nodejs_binary.bzl", "nodejs_binary")
load("@aspect_rules_js//js:js_binary.bzl", "js_binary")
load("@swc_cli//:package.bzl", "package")
load("@swc_cli//:node_modules.bzl", "node_modules")
load("@npm__at_swc_core_1.2.119//:node_package.bzl", swc_core_package = "node_package")
load("@npm__at_node-rs_helper_1.2.1//:node_package.bzl", rs_helper_package = "node_package")
load("@npm__at_napi-rs_triples_1.1.0//:node_package.bzl", rs_triples_package = "node_package")
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")

write_source_files(
name = "write_cli_repos",
files = {
"cli_repositories.bzl": "@swc_cli//:repositories.bzl",
},
)

swc_core_package()

rs_helper_package()

rs_triples_package()

node_modules()

# For stardoc to reference the files
exports_files(["swc.bzl"])
Expand All @@ -25,22 +46,22 @@ bzl_library(
],
)

# Workaround: nodejs_binary doesn't support entry points under external yet
# Workaround: js_binary doesn't support entry points under external yet
write_file(
name = "gen_main",
out = "main.cjs",
content = ["require('@swc/cli')"],
)

nodejs_binary(
js_binary(
name = "cli",
data = [
# Don't use the shorter syntax here because the user or bzlmod
# might use repository mapping to give a different name
# to the repository, e.g.
# @aspect_rules_swc.0.2.0.swc.npm__swc_core-1.2.119
"@npm__swc_cli-0.1.52//:pkg",
"@npm__swc_core-1.2.119//:pkg",
package("@swc/cli"),
"//swc:npm__@swc+core",
],
entry_point = "main.cjs",
visibility = ["//visibility:public"],
Expand Down
Loading

0 comments on commit 5c5ff53

Please sign in to comment.