Compare commits
2 Commits
v0.4.0
...
4f8e27cd74
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f8e27cd74 | ||
|
|
31c42a8638 |
@@ -1,6 +1,6 @@
|
||||
module(
|
||||
name = "rules_bun",
|
||||
version = "0.4.0",
|
||||
version = "0.5.0",
|
||||
)
|
||||
|
||||
# Core ruleset dependencies.
|
||||
|
||||
4
MODULE.bazel.lock
generated
4
MODULE.bazel.lock
generated
@@ -193,7 +193,7 @@
|
||||
"//bun:extensions.bzl%bun": {
|
||||
"general": {
|
||||
"bzlTransitiveDigest": "lzOUyaXDbkH922ruNkkwEF2cnI4m0XpzrOti0qypwtA=",
|
||||
"usagesDigest": "3e3c2oTwajnFd8qnwby/r5XJjkCbXcs0Y4hgl0tOBes=",
|
||||
"usagesDigest": "iOEuN6Lhr5Bejh9GRDT3yaCI7umBsNZo5CLQSzt+tNQ=",
|
||||
"recordedInputs": [
|
||||
"REPO_MAPPING:,bazel_tools bazel_tools"
|
||||
],
|
||||
@@ -284,7 +284,7 @@
|
||||
"//bun:extensions.bzl%bun_install": {
|
||||
"general": {
|
||||
"bzlTransitiveDigest": "lzOUyaXDbkH922ruNkkwEF2cnI4m0XpzrOti0qypwtA=",
|
||||
"usagesDigest": "Rpb8CFAx+vMvmPf6ZPeHR/gweyP8QXMGdOfSou7lKNg=",
|
||||
"usagesDigest": "eD/dECSVWIFS/qMgEXZ6501CimKmsHt7yTIFPX4XdLo=",
|
||||
"recordedInputs": [
|
||||
"REPO_MAPPING:,bazel_tools bazel_tools"
|
||||
],
|
||||
|
||||
@@ -60,7 +60,7 @@ Release announcements should provide a copy-pasteable module snippet in the
|
||||
standard ruleset form:
|
||||
|
||||
```starlark
|
||||
bazel_dep(name = "rules_bun", version = "0.4.0")
|
||||
bazel_dep(name = "rules_bun", version = "0.5.0")
|
||||
```
|
||||
|
||||
Then add the Bun repositories and register the toolchains in `MODULE.bazel`:
|
||||
@@ -225,7 +225,9 @@ bun_compile(
|
||||
|
||||
`bun_build` exposes a directory output so Bun can emit HTML, CSS, assets, and
|
||||
split chunks. `bun_compile` produces a single executable artifact and supports
|
||||
explicit cross-compilation via `compile_executable`.
|
||||
explicit cross-compilation via `compile_executable`. When `root` is omitted,
|
||||
`bun_build` derives a stable default from the entry point parent directory so
|
||||
HTML and asset output stays inside Bazel's declared output tree.
|
||||
|
||||
### `bun_dev` for local development
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ may be requested with `metafile` and `metafile_md`.
|
||||
| <a id="bun_build-production"></a>production | If true, sets `NODE_ENV=production` and enables Bun production mode. | Boolean | optional | `False` |
|
||||
| <a id="bun_build-public_path"></a>public_path | Optional public path prefix for emitted imports. | String | optional | `""` |
|
||||
| <a id="bun_build-react_fast_refresh"></a>react_fast_refresh | If true, enables Bun's React fast refresh transform. | Boolean | optional | `False` |
|
||||
| <a id="bun_build-root"></a>root | Optional root directory for multiple entry points. | String | optional | `""` |
|
||||
| <a id="bun_build-root"></a>root | Optional root directory for multiple entry points. When omitted, `bun_build` derives one from the entry point parent directories to keep emitted files inside the declared output tree. | String | optional | `""` |
|
||||
| <a id="bun_build-sourcemap"></a>sourcemap | Sourcemap emission mode. | String | optional | `"none"` |
|
||||
| <a id="bun_build-splitting"></a>splitting | If true, enables code splitting. | Boolean | optional | `False` |
|
||||
| <a id="bun_build-target"></a>target | Bun build target environment. | String | optional | `"browser"` |
|
||||
@@ -481,4 +481,3 @@ js_test(<a href="#js_test-name">name</a>, <a href="#js_test-entry_point">entry_p
|
||||
| <a id="js_test-srcs"></a>srcs | <p align="center"> - </p> | `None` |
|
||||
| <a id="js_test-kwargs"></a>kwargs | <p align="center"> - </p> | none |
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,26 @@
|
||||
load("//internal:bun_command.bzl", "add_flag", "add_flag_value", "add_flag_values", "add_install_mode", "add_raw_flags")
|
||||
load("//internal:js_library.bzl", "collect_js_sources")
|
||||
|
||||
def infer_entry_point_root(entries):
|
||||
if not entries:
|
||||
return None
|
||||
|
||||
common_segments = entries[0].path.split("/")[:-1]
|
||||
for entry in entries[1:]:
|
||||
entry_segments = entry.path.split("/")[:-1]
|
||||
common_length = min(len(common_segments), len(entry_segments))
|
||||
idx = common_length
|
||||
for segment_idx in range(common_length):
|
||||
if common_segments[segment_idx] != entry_segments[segment_idx]:
|
||||
idx = segment_idx
|
||||
break
|
||||
common_segments = common_segments[:idx]
|
||||
|
||||
if not common_segments:
|
||||
return "."
|
||||
|
||||
return "/".join(common_segments)
|
||||
|
||||
def bun_build_transitive_inputs(ctx):
|
||||
transitive_inputs = []
|
||||
if getattr(ctx.attr, "node_modules", None):
|
||||
@@ -11,13 +31,17 @@ def bun_build_transitive_inputs(ctx):
|
||||
transitive_inputs.append(collect_js_sources(dep))
|
||||
return transitive_inputs
|
||||
|
||||
def add_bun_build_common_flags(args, attr, metafile = None, metafile_md = None):
|
||||
def add_bun_build_common_flags(args, attr, metafile = None, metafile_md = None, root = None):
|
||||
build_root = root
|
||||
if build_root == None:
|
||||
build_root = getattr(attr, "root", None)
|
||||
|
||||
add_install_mode(args, getattr(attr, "install_mode", "disable"))
|
||||
add_flag_value(args, "--target", getattr(attr, "target", None))
|
||||
add_flag_value(args, "--format", getattr(attr, "format", None))
|
||||
add_flag(args, "--production", getattr(attr, "production", False))
|
||||
add_flag(args, "--splitting", getattr(attr, "splitting", False))
|
||||
add_flag_value(args, "--root", getattr(attr, "root", None))
|
||||
add_flag_value(args, "--root", build_root)
|
||||
|
||||
sourcemap = getattr(attr, "sourcemap", None)
|
||||
if sourcemap == True:
|
||||
|
||||
@@ -1,18 +1,34 @@
|
||||
"""Rules for Bun build outputs and standalone executables."""
|
||||
|
||||
load("//internal:bun_build_support.bzl", "add_bun_build_common_flags", "add_bun_compile_flags", "bun_build_transitive_inputs")
|
||||
load("//internal:bun_build_support.bzl", "add_bun_build_common_flags", "add_bun_compile_flags", "bun_build_transitive_inputs", "infer_entry_point_root")
|
||||
|
||||
def _bun_build_impl(ctx):
|
||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||
bun_bin = toolchain.bun.bun_bin
|
||||
output_dir = ctx.actions.declare_directory(ctx.label.name)
|
||||
metafile = ctx.actions.declare_file(ctx.label.name + ".meta.json") if ctx.attr.metafile else None
|
||||
metafile_md = ctx.actions.declare_file(ctx.label.name + ".meta.md") if ctx.attr.metafile_md else None
|
||||
metafile = None
|
||||
if ctx.attr.metafile:
|
||||
metafile = ctx.actions.declare_file(ctx.label.name + ".meta.json")
|
||||
metafile_md = None
|
||||
if ctx.attr.metafile_md:
|
||||
metafile_md = ctx.actions.declare_file(ctx.label.name + ".meta.md")
|
||||
build_root = ctx.attr.root
|
||||
if not build_root:
|
||||
build_root = infer_entry_point_root(ctx.files.entry_points)
|
||||
transitive_inputs = bun_build_transitive_inputs(ctx)
|
||||
build_inputs = depset(
|
||||
direct = ctx.files.entry_points + ctx.files.data,
|
||||
transitive = transitive_inputs,
|
||||
)
|
||||
input_manifest = ctx.actions.declare_file(ctx.label.name + ".inputs")
|
||||
runner = ctx.actions.declare_file(ctx.label.name + "_runner.sh")
|
||||
|
||||
args = ctx.actions.args()
|
||||
args.add(input_manifest.path)
|
||||
args.add(bun_bin.path)
|
||||
args.add("--bun")
|
||||
args.add("build")
|
||||
add_bun_build_common_flags(args, ctx.attr, metafile = metafile, metafile_md = metafile_md)
|
||||
add_bun_build_common_flags(args, ctx.attr, metafile = metafile, metafile_md = metafile_md, root = build_root)
|
||||
args.add("--outdir")
|
||||
args.add(output_dir.path)
|
||||
args.add_all(ctx.files.entry_points)
|
||||
@@ -23,12 +39,74 @@ def _bun_build_impl(ctx):
|
||||
if metafile_md:
|
||||
outputs.append(metafile_md)
|
||||
|
||||
ctx.actions.write(
|
||||
output = input_manifest,
|
||||
content = "".join([file.path + "\n" for file in build_inputs.to_list()]),
|
||||
)
|
||||
|
||||
ctx.actions.write(
|
||||
output = runner,
|
||||
is_executable = True,
|
||||
content = """#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
manifest="$1"
|
||||
execroot="$(pwd -P)"
|
||||
bun_bin="$2"
|
||||
if [[ "${bun_bin}" != /* ]]; then
|
||||
bun_bin="${execroot}/${bun_bin}"
|
||||
fi
|
||||
shift 2
|
||||
|
||||
stage_dir="$(mktemp -d "${TMPDIR:-/tmp}/rules_bun_build.XXXXXX")"
|
||||
cleanup() {
|
||||
rm -rf "${stage_dir}"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
while IFS= read -r relpath; do
|
||||
if [[ -z "${relpath}" ]]; then
|
||||
continue
|
||||
fi
|
||||
src="${execroot}/${relpath}"
|
||||
dest="${stage_dir}/${relpath}"
|
||||
mkdir -p "$(dirname "${dest}")"
|
||||
cp -L "${src}" "${dest}"
|
||||
done < "${manifest}"
|
||||
|
||||
forwarded_args=()
|
||||
while (($#)); do
|
||||
case "$1" in
|
||||
--outdir)
|
||||
forwarded_args+=("$1" "${execroot}/$2")
|
||||
shift 2
|
||||
;;
|
||||
--metafile=*)
|
||||
forwarded_args+=("--metafile=${execroot}/${1#--metafile=}")
|
||||
shift
|
||||
;;
|
||||
--metafile-md=*)
|
||||
forwarded_args+=("--metafile-md=${execroot}/${1#--metafile-md=}")
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
forwarded_args+=("$1")
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
cd "${stage_dir}"
|
||||
exec "${bun_bin}" "${forwarded_args[@]}"
|
||||
""",
|
||||
)
|
||||
|
||||
ctx.actions.run(
|
||||
executable = bun_bin,
|
||||
executable = runner,
|
||||
arguments = [args],
|
||||
inputs = depset(
|
||||
direct = ctx.files.entry_points + ctx.files.data,
|
||||
transitive = bun_build_transitive_inputs(ctx),
|
||||
direct = [input_manifest, bun_bin],
|
||||
transitive = [build_inputs],
|
||||
),
|
||||
outputs = outputs,
|
||||
mnemonic = "BunBuild",
|
||||
|
||||
@@ -132,6 +132,14 @@ for expected in \
|
||||
expect_line "${build_output}" "${expected}"
|
||||
done
|
||||
|
||||
default_root_output="$(run_aquery "BunBuild" "//tests/bundle_test:site_build_with_meta")"
|
||||
|
||||
for expected in \
|
||||
'arguments: "--root"' \
|
||||
'arguments: "tests/bundle_test/site"'; do
|
||||
expect_line "${default_root_output}" "${expected}"
|
||||
done
|
||||
|
||||
compile_output="$(run_aquery "BunCompile" "//tests/bundle_test:compiled_cli_with_flags")"
|
||||
|
||||
for expected in \
|
||||
|
||||
Reference in New Issue
Block a user