feat: new bun_build and bun_compile, extend bun_install

This commit is contained in:
eric
2026-03-15 00:11:55 +01:00
parent c446f23a35
commit a0bc998bd2
58 changed files with 1845 additions and 191 deletions

View File

@@ -1,4 +1,4 @@
load("//bun:defs.bzl", "bun_bundle")
load("//bun:defs.bzl", "bun_build", "bun_bundle", "bun_compile")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
bun_bundle(
@@ -18,6 +18,32 @@ bun_bundle(
external = ["left-pad"],
)
bun_build(
name = "site_build",
entry_points = ["site/index.html"],
data = [
"site/main.ts",
"site/styles.css",
],
splitting = True,
)
bun_build(
name = "site_build_with_meta",
entry_points = ["site/index.html"],
data = [
"site/main.ts",
"site/styles.css",
],
metafile = True,
metafile_md = True,
)
bun_compile(
name = "compiled_cli",
entry_point = "cli.ts",
)
sh_test(
name = "bundle_output_test",
srcs = ["verify_bundle.sh"],
@@ -57,3 +83,24 @@ sh_test(
"//tests/bundle_test:BUILD.bazel",
],
)
sh_test(
name = "bun_build_site_output_test",
srcs = ["verify_site_build.sh"],
args = ["$(location :site_build)"],
data = [":site_build"],
)
sh_test(
name = "bun_build_site_meta_test",
srcs = ["verify_site_build_meta.sh"],
args = ["$(locations :site_build_with_meta)"],
data = [":site_build_with_meta"],
)
sh_test(
name = "bun_compile_output_test",
srcs = ["run_compiled_binary.sh"],
args = ["$(location :compiled_cli)"],
data = [":compiled_cli"],
)

1
tests/bundle_test/cli.ts Normal file
View File

@@ -0,0 +1 @@
console.log("compiled-cli");

View File

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

View File

@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>rules_bun site build</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<div id="app">rules_bun</div>
<script type="module" src="./main.ts"></script>
</body>
</html>

View File

@@ -0,0 +1 @@
document.getElementById("app")?.setAttribute("data-built", "yes");

View File

@@ -0,0 +1,4 @@
body {
background: #f5efe2;
color: #1f1b14;
}

View File

@@ -3,12 +3,12 @@ set -euo pipefail
bundle="$1"
if [[ ! -f "${bundle}" ]]; then
if [[ ! -f ${bundle} ]]; then
echo "Bundle output not found: ${bundle}" >&2
exit 1
fi
if [[ ! -s "${bundle}" ]]; then
if [[ ! -s ${bundle} ]]; then
echo "Bundle output is empty: ${bundle}" >&2
exit 1
fi

View File

@@ -4,10 +4,10 @@ set -euo pipefail
bundle="$1"
minified="$2"
bundle_size="$(wc -c < "${bundle}")"
minified_size="$(wc -c < "${minified}")"
bundle_size="$(wc -c <"${bundle}")"
minified_size="$(wc -c <"${minified}")"
if (( minified_size >= bundle_size )); then
if ((minified_size >= bundle_size)); then
echo "Expected minified bundle (${minified_size}) to be smaller than regular bundle (${bundle_size})" >&2
exit 1
fi

View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
output_dir="$1"
if [[ ! -d ${output_dir} ]]; then
echo "Expected output directory: ${output_dir}" >&2
exit 1
fi
if ! find -L "${output_dir}" -type f \( -name '*.js' -o -name '*.css' \) | grep -q .; then
echo "Expected Bun build assets in ${output_dir}" >&2
exit 1
fi

View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
set -euo pipefail
output_dir=""
meta_json=""
meta_md=""
for path in "$@"; do
case "${path}" in
*.meta.json) meta_json="${path}" ;;
*.meta.md) meta_md="${path}" ;;
*) output_dir="${path}" ;;
esac
done
if [[ ! -d ${output_dir} ]]; then
echo "Expected directory output, got: ${output_dir}" >&2
exit 1
fi
if [[ ! -f ${meta_json} ]]; then
echo "Expected JSON metafile output" >&2
exit 1
fi
if [[ ! -f ${meta_md} ]]; then
echo "Expected markdown metafile output" >&2
exit 1
fi