feat: implement phase 5 bun_bundle bootstrap

This commit is contained in:
Eriyc
2026-03-04 03:30:07 +00:00
committed by copilot-swe-agent[bot]
parent e36d7a982b
commit b664b15916
7 changed files with 164 additions and 2 deletions

View File

@@ -28,3 +28,9 @@ Phase 4 bootstrap is in place:
- Test rule `bun_test` (`/internal/bun_test.bzl`) - Test rule `bun_test` (`/internal/bun_test.bzl`)
- Public export via `bun/defs.bzl` - Public export via `bun/defs.bzl`
- Focused passing/failing test targets (`//tests/bun_test_test:all`) - Focused passing/failing test targets (`//tests/bun_test_test:all`)
Phase 5 bootstrap is in place:
- Bundle rule `bun_bundle` (`/internal/bun_bundle.bzl`)
- Public export via `bun/defs.bzl`
- Focused output/minify tests (`//tests/bundle_test:all`)

View File

@@ -1,4 +1,5 @@
load("//internal:bun_binary.bzl", "bun_binary") load("//internal:bun_binary.bzl", "bun_binary")
load("//internal:bun_bundle.bzl", "bun_bundle")
load("//internal:bun_install.bzl", "bun_install") load("//internal:bun_install.bzl", "bun_install")
load("//internal:bun_test.bzl", "bun_test") load("//internal:bun_test.bzl", "bun_test")
load(":repositories.bzl", "bun_register_toolchains", "bun_repositories") load(":repositories.bzl", "bun_register_toolchains", "bun_repositories")
@@ -7,6 +8,7 @@ load(":toolchain.bzl", "BunToolchainInfo", "bun_toolchain")
__all__ = [ __all__ = [
"BunToolchainInfo", "BunToolchainInfo",
"bun_binary", "bun_binary",
"bun_bundle",
"bun_install", "bun_install",
"bun_test", "bun_test",
"bun_register_toolchains", "bun_register_toolchains",

View File

@@ -1,2 +1,76 @@
def bun_bundle(**_kwargs): """Rule for bundling JS/TS sources with Bun."""
fail("bun_bundle is not implemented yet")
def _output_name(target_name, entry):
stem = entry.basename.rsplit(".", 1)[0]
return "{}__{}.js".format(target_name, stem)
def _bun_bundle_impl(ctx):
toolchain = ctx.toolchains["//bun:toolchain_type"]
bun_bin = toolchain.bun.bun_bin
transitive_inputs = []
if ctx.attr.node_modules:
transitive_inputs.append(ctx.attr.node_modules[DefaultInfo].files)
outputs = []
for entry in ctx.files.entry_points:
output = ctx.actions.declare_file(_output_name(ctx.label.name, entry))
outputs.append(output)
args = ctx.actions.args()
args.add("build")
args.add(entry.path)
args.add("--outfile")
args.add(output.path)
args.add("--target")
args.add(ctx.attr.target)
args.add("--format")
args.add(ctx.attr.format)
if ctx.attr.minify:
args.add("--minify")
if ctx.attr.sourcemap:
args.add("--sourcemap")
for package in ctx.attr.external:
args.add("--external")
args.add(package)
ctx.actions.run(
executable = bun_bin,
arguments = [args],
inputs = depset(
direct = [entry] + ctx.files.data,
transitive = transitive_inputs,
),
outputs = [output],
mnemonic = "BunBundle",
progress_message = "Bundling {} with Bun".format(entry.short_path),
)
return [DefaultInfo(files = depset(outputs))]
bun_bundle = rule(
implementation = _bun_bundle_impl,
attrs = {
"entry_points": attr.label_list(
mandatory = True,
allow_files = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"],
),
"node_modules": attr.label(),
"data": attr.label_list(allow_files = True),
"target": attr.string(
default = "browser",
values = ["browser", "node", "bun"],
),
"format": attr.string(
default = "esm",
values = ["esm", "cjs", "iife"],
),
"minify": attr.bool(default = False),
"sourcemap": attr.bool(default = False),
"external": attr.string_list(),
},
toolchains = ["//bun:toolchain_type"],
)

View File

@@ -0,0 +1,48 @@
load("//bun:defs.bzl", "bun_bundle")
bun_bundle(
name = "simple_bundle",
entry_points = ["main.ts"],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
)
bun_bundle(
name = "minified_bundle",
entry_points = ["main.ts"],
minify = True,
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
)
sh_test(
name = "bundle_output_test",
srcs = ["verify_bundle.sh"],
args = ["$(location :simple_bundle)"],
data = [":simple_bundle"],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
)
sh_test(
name = "bundle_minify_test",
srcs = ["verify_minify.sh"],
args = [
"$(location :simple_bundle)",
"$(location :minified_bundle)",
],
data = [
":simple_bundle",
":minified_bundle",
],
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
)

View File

@@ -0,0 +1,5 @@
export function greet(name: string): string {
return `Hello ${name}`;
}
console.log(greet("bundle"));

View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
bundle="$1"
if [[ ! -f "${bundle}" ]]; then
echo "Bundle output not found: ${bundle}" >&2
exit 1
fi
if [[ ! -s "${bundle}" ]]; then
echo "Bundle output is empty: ${bundle}" >&2
exit 1
fi

View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -euo pipefail
bundle="$1"
minified="$2"
bundle_size="$(wc -c < "${bundle}")"
minified_size="$(wc -c < "${minified}")"
if (( minified_size >= bundle_size )); then
echo "Expected minified bundle (${minified_size}) to be smaller than regular bundle (${bundle_size})" >&2
exit 1
fi