feat: improve rules_js parity

This commit is contained in:
eric
2026-03-14 23:50:26 +01:00
parent d7a6d6b0ba
commit c446f23a35
36 changed files with 1683 additions and 639 deletions

View File

@@ -6,3 +6,10 @@ sh_test(
args = ["$(location //bun:extensions.bzl)"],
data = ["//bun:extensions.bzl"],
)
sh_test(
name = "npm_translate_lock_extension_shape_test",
srcs = ["npm_extension_shape_test.sh"],
args = ["$(location //npm:extensions.bzl)"],
data = ["//npm:extensions.bzl"],
)

View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail
extension_file="$1"
grep -Eq 'npm_translate_lock[[:space:]]*=[[:space:]]*module_extension\(' "${extension_file}"
grep -Eq 'tag_classes[[:space:]]*=[[:space:]]*\{"translate":[[:space:]]*_translate\}' "${extension_file}"
grep -Eq '"name":[[:space:]]*attr\.string\(mandatory[[:space:]]*=[[:space:]]*True\)' "${extension_file}"
grep -Eq '"package_json":[[:space:]]*attr\.label\(mandatory[[:space:]]*=[[:space:]]*True\)' "${extension_file}"
grep -Eq '"lockfile":[[:space:]]*attr\.label\(mandatory[[:space:]]*=[[:space:]]*True\)' "${extension_file}"

View File

@@ -0,0 +1,41 @@
load("//js:defs.bzl", "js_binary", "js_run_devserver", "js_test", "ts_library")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
ts_library(
name = "helper_lib",
srcs = ["helper.ts"],
data = ["payload.txt"],
)
js_binary(
name = "compat_bin",
entry_point = "main.ts",
deps = [":helper_lib"],
args = ["compat-mode"],
)
sh_test(
name = "js_binary_compat_test",
srcs = ["run_binary.sh"],
args = ["$(location :compat_bin)"],
data = [":compat_bin"],
)
js_test(
name = "compat_suite",
entry_point = "app.test.ts",
deps = [":helper_lib"],
)
js_run_devserver(
name = "compat_devserver",
tool = ":compat_bin",
args = ["devserver-mode"],
)
sh_test(
name = "js_run_devserver_compat_test",
srcs = ["run_devserver.sh"],
args = ["$(location :compat_devserver)"],
data = [":compat_devserver"],
)

View File

@@ -0,0 +1,7 @@
import { expect, test } from "bun:test";
import { helperMessage } from "./helper.ts";
test("js_test compatibility layer propagates deps and data", () => {
expect(helperMessage()).toBe("helper:payload-from-lib");
});

View File

@@ -0,0 +1,6 @@
import { readFileSync } from "node:fs";
export function helperMessage(): string {
const payload = readFileSync(new URL("./payload.txt", import.meta.url), "utf8").trim();
return `helper:${payload}`;
}

View File

@@ -0,0 +1,3 @@
import { helperMessage } from "./helper.ts";
console.log(`${helperMessage()} ${Bun.argv.slice(2).join(" ")}`.trim());

View File

@@ -0,0 +1 @@
payload-from-lib

View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail
binary="$1"
output="$("${binary}")"
if [[ ${output} != "helper:payload-from-lib compat-mode" ]]; then
echo "unexpected output: ${output}" >&2
exit 1
fi

View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail
binary="$1"
output="$("${binary}")"
if [[ ${output} != "helper:payload-from-lib compat-mode devserver-mode" ]]; then
echo "unexpected output: ${output}" >&2
exit 1
fi

View File

@@ -0,0 +1,119 @@
#!/usr/bin/env bash
set -euo pipefail
nix_cmd="${NIX:-/nix/var/nix/profiles/default/bin/nix}"
if [[ ! -x ${nix_cmd} ]]; then
nix_cmd="$(command -v nix || true)"
fi
if [[ -z ${nix_cmd} || ! -x ${nix_cmd} ]]; then
echo "nix is required to launch bazel from the repo dev shell" >&2
exit 1
fi
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
rules_bun_root="$(cd "${script_dir}/../.." && pwd -P)"
workdir="$(mktemp -d)"
trap 'rm -rf "${workdir}"' EXIT
fixture_dir="${workdir}/fixture"
mkdir -p "${fixture_dir}"
cat >"${fixture_dir}/package.json" <<'JSON'
{
"name": "npm-compat-test",
"type": "module",
"dependencies": {
"is-number": "7.0.0"
}
}
JSON
cat >"${fixture_dir}/main.js" <<'JS'
import isNumber from "is-number";
console.log(`compat:${isNumber(42)}`);
JS
(
cd "${rules_bun_root}" &&
"${nix_cmd}" develop -c bash -lc 'bun install --cwd "$1" >/dev/null' bash "${fixture_dir}"
)
rm -rf "${fixture_dir}/node_modules"
cat >"${fixture_dir}/MODULE.bazel" <<EOF
module(
name = "npm_compat_test",
)
bazel_dep(name = "rules_bun", version = "0.2.2")
local_path_override(
module_name = "rules_bun",
path = "${rules_bun_root}",
)
bun_ext = use_extension("@rules_bun//bun:extensions.bzl", "bun")
use_repo(
bun_ext,
"bun_darwin_aarch64",
"bun_darwin_x64",
"bun_linux_aarch64",
"bun_linux_x64",
"bun_windows_x64",
)
npm_ext = use_extension("@rules_bun//npm:extensions.bzl", "npm_translate_lock")
npm_ext.translate(
name = "npm",
package_json = "//:package.json",
lockfile = "//:bun.lock",
)
use_repo(npm_ext, "npm")
register_toolchains(
"@rules_bun//bun:darwin_aarch64_toolchain",
"@rules_bun//bun:darwin_x64_toolchain",
"@rules_bun//bun:linux_aarch64_toolchain",
"@rules_bun//bun:linux_x64_toolchain",
"@rules_bun//bun:windows_x64_toolchain",
)
EOF
cat >"${fixture_dir}/BUILD.bazel" <<'EOF'
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@rules_bun//js:defs.bzl", "js_binary")
exports_files([
"bun.lock",
"main.js",
"package.json",
])
npm_link_all_packages()
js_binary(
name = "app",
entry_point = "main.js",
node_modules = ":node_modules",
)
EOF
output="$(
cd "${rules_bun_root}" &&
"${nix_cmd}" develop -c bash -lc 'cd "$1" && bazel run //:app' bash "${fixture_dir}"
)"
if [[ ${output} != *"compat:true"* ]]; then
echo "unexpected output: ${output}" >&2
exit 1
fi
query_output="$(
cd "${rules_bun_root}" &&
"${nix_cmd}" develop -c bash -lc 'cd "$1" && bazel query //:npm__is_number' bash "${fixture_dir}"
)"
if ! grep -Fxq "//:npm__is_number" <<<"${query_output}"; then
echo "expected npm_link_all_packages to create //:npm__is_number" >&2
exit 1
fi