Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ddd0b77cc | ||
|
|
5fe05921d4 | ||
|
|
a6dedca6f5 | ||
|
|
372a62fa9c |
@@ -1,6 +1,6 @@
|
|||||||
module(
|
module(
|
||||||
name = "rules_bun",
|
name = "rules_bun",
|
||||||
version = "0.0.4",
|
version = "0.0.6",
|
||||||
)
|
)
|
||||||
|
|
||||||
bazel_dep(name = "platforms", version = "1.0.0")
|
bazel_dep(name = "platforms", version = "1.0.0")
|
||||||
|
|||||||
2
MODULE.bazel.lock
generated
2
MODULE.bazel.lock
generated
@@ -190,7 +190,7 @@
|
|||||||
"//bun:extensions.bzl%bun": {
|
"//bun:extensions.bzl%bun": {
|
||||||
"general": {
|
"general": {
|
||||||
"bzlTransitiveDigest": "Q0uQOwFAgAU+etePCZ4TUDO+adLX7Z0EmRLaEsKgncw=",
|
"bzlTransitiveDigest": "Q0uQOwFAgAU+etePCZ4TUDO+adLX7Z0EmRLaEsKgncw=",
|
||||||
"usagesDigest": "qk1PDh3WICa0VONYKXJLsmWCesNJxz3Jkb/aH/voIeI=",
|
"usagesDigest": "UC4zk8kEwWRiDG5FVQOCFysXcrZ757Jehf3sZgG893w=",
|
||||||
"recordedInputs": [
|
"recordedInputs": [
|
||||||
"REPO_MAPPING:,bazel_tools bazel_tools"
|
"REPO_MAPPING:,bazel_tools bazel_tools"
|
||||||
],
|
],
|
||||||
|
|||||||
51
README.md
51
README.md
@@ -11,24 +11,24 @@ These steps show how to consume a tagged release of `rules_bun` in a separate Ba
|
|||||||
In your project's `MODULE.bazel`, add:
|
In your project's `MODULE.bazel`, add:
|
||||||
|
|
||||||
```starlark
|
```starlark
|
||||||
bazel_dep(name = "rules_bun", version = "0.0.3")
|
bazel_dep(name = "rules_bun", version = "0.0.6")
|
||||||
|
|
||||||
archive_override(
|
archive_override(
|
||||||
module_name = "rules_bun",
|
module_name = "rules_bun",
|
||||||
urls = ["https://github.com/Eriyc/rules_bun/archive/refs/tags/v0.0.3.tar.gz"],
|
urls = ["https://github.com/Eriyc/rules_bun/archiv0.0.5.tar.gz"],
|
||||||
strip_prefix = "rules_bun-v0.0.3",
|
strip_prefix = "rules_bun-v0.0.6",
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
For channel/pre-release tags (for example `v0.0.3-rc.1`), use the matching folder prefix:
|
For channel/pre-release tags (for example `v0.0.6-rc.1`), use the matching folder prefix:
|
||||||
|
|
||||||
```starlark
|
```starlark
|
||||||
bazel_dep(name = "rules_bun", version = "0.0.3-rc.1")
|
bazel_dep(name = "rules_bun", version = "0.0.6-rc.1")
|
||||||
|
|
||||||
archive_override(
|
archive_override(
|
||||||
module_name = "rules_bun",
|
module_name = "rules_bun",
|
||||||
urls = ["https://github.com/Eriyc/rules_bun/archive/refs/tags/v0.0.3-rc.1.tar.gz"],
|
urls = ["https://github.com/Eriyc/rules_bun/archiv0.0.5-rc.1.tar.gz"],
|
||||||
strip_prefix = "rules_bun-v0.0.3-rc.1",
|
strip_prefix = "rules_bun-v0.0.6-rc.1",
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -72,6 +72,7 @@ load(
|
|||||||
"@rules_bun//bun:defs.bzl",
|
"@rules_bun//bun:defs.bzl",
|
||||||
"bun_binary",
|
"bun_binary",
|
||||||
"bun_bundle",
|
"bun_bundle",
|
||||||
|
"bun_dev",
|
||||||
"bun_test",
|
"bun_test",
|
||||||
"js_library",
|
"js_library",
|
||||||
"ts_library",
|
"ts_library",
|
||||||
@@ -101,3 +102,39 @@ Run one of your bun-backed targets, for example:
|
|||||||
```bash
|
```bash
|
||||||
bazel test //path/to:your_bun_test
|
bazel test //path/to:your_bun_test
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Development mode (`bun_dev`)
|
||||||
|
|
||||||
|
Use `bun_dev` for long-running local development with Bun watch mode.
|
||||||
|
|
||||||
|
```starlark
|
||||||
|
load("@rules_bun//bun:defs.bzl", "bun_dev")
|
||||||
|
|
||||||
|
bun_dev(
|
||||||
|
name = "web_dev",
|
||||||
|
entry_point = "src/main.ts",
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
Run it with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bazel run //path/to:web_dev
|
||||||
|
```
|
||||||
|
|
||||||
|
`bun_dev` supports:
|
||||||
|
|
||||||
|
- `watch_mode = "watch"` (default) for `bun --watch`
|
||||||
|
- `watch_mode = "hot"` for `bun --hot`
|
||||||
|
- `restart_on = [...]` to force full process restarts when specific files change
|
||||||
|
|
||||||
|
### Hybrid Go + Bun + protobuf workflow
|
||||||
|
|
||||||
|
For monorepos that mix Go and Bun (including FFI):
|
||||||
|
|
||||||
|
1. Run Bun app with native watch/HMR via `bun_dev`.
|
||||||
|
2. Put generated artifacts or bridge files in `restart_on` (for example generated JS/TS files from proto/go steps).
|
||||||
|
3. Rebuild Go/proto artifacts separately (for example with `ibazel build`) so their output files change.
|
||||||
|
4. `bun_dev` detects those `restart_on` changes and restarts Bun, while ordinary JS edits continue to use Bun watch/HMR without full Bazel restarts.
|
||||||
|
|
||||||
|
This keeps the fast Bun JS loop while still supporting full restarts when non-JS dependencies change.
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
"""Public API surface for Bun Bazel rules."""
|
||||||
load("//internal:bun_binary.bzl", _bun_binary = "bun_binary")
|
load("//internal:bun_binary.bzl", _bun_binary = "bun_binary")
|
||||||
load("//internal:bun_bundle.bzl", _bun_bundle = "bun_bundle")
|
load("//internal:bun_bundle.bzl", _bun_bundle = "bun_bundle")
|
||||||
|
load("//internal:bun_dev.bzl", _bun_dev = "bun_dev")
|
||||||
load("//internal:bun_test.bzl", _bun_test = "bun_test")
|
load("//internal:bun_test.bzl", _bun_test = "bun_test")
|
||||||
load("//internal:js_library.bzl", _js_library = "js_library", _ts_library = "ts_library")
|
load("//internal:js_library.bzl", _js_library = "js_library", _ts_library = "ts_library")
|
||||||
load(":toolchain.bzl", _BunToolchainInfo = "BunToolchainInfo", _bun_toolchain = "bun_toolchain")
|
load(":toolchain.bzl", _BunToolchainInfo = "BunToolchainInfo", _bun_toolchain = "bun_toolchain")
|
||||||
@@ -8,6 +10,7 @@ visibility("public")
|
|||||||
|
|
||||||
bun_binary = _bun_binary
|
bun_binary = _bun_binary
|
||||||
bun_bundle = _bun_bundle
|
bun_bundle = _bun_bundle
|
||||||
|
bun_dev = _bun_dev
|
||||||
bun_test = _bun_test
|
bun_test = _bun_test
|
||||||
js_library = _js_library
|
js_library = _js_library
|
||||||
ts_library = _ts_library
|
ts_library = _ts_library
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
|
load("//bun:defs.bzl", "bun_dev")
|
||||||
|
|
||||||
package(default_visibility = ["//visibility:public"])
|
package(default_visibility = ["//visibility:public"])
|
||||||
|
|
||||||
exports_files([
|
exports_files([
|
||||||
"README.md",
|
"README.md",
|
||||||
|
"main.ts",
|
||||||
])
|
])
|
||||||
|
|
||||||
|
bun_dev(
|
||||||
|
name = "web_dev",
|
||||||
|
entry_point = "main.ts",
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,3 +1,11 @@
|
|||||||
# basic example
|
# basic example
|
||||||
|
|
||||||
Placeholder for end-to-end bun rules example.
|
Minimal `bun_dev` example.
|
||||||
|
|
||||||
|
Run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bazel run //examples/basic:web_dev
|
||||||
|
```
|
||||||
|
|
||||||
|
This starts Bun in watch mode for `main.ts`.
|
||||||
|
|||||||
1
examples/basic/main.ts
Normal file
1
examples/basic/main.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
console.log("rules_bun bun_dev example");
|
||||||
30
flake.nix
30
flake.nix
@@ -136,6 +136,36 @@
|
|||||||
sed -E -i 's#^([[:space:]]*version[[:space:]]*=[[:space:]]*")[^"]*(",)$#\1'"$FULL_VERSION"'\2#' "$ROOT_DIR/MODULE.bazel"
|
sed -E -i 's#^([[:space:]]*version[[:space:]]*=[[:space:]]*")[^"]*(",)$#\1'"$FULL_VERSION"'\2#' "$ROOT_DIR/MODULE.bazel"
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
run = ''
|
||||||
|
README="$ROOT_DIR/README.md"
|
||||||
|
TMP="$README.tmp"
|
||||||
|
|
||||||
|
awk -v stable="$BASE_VERSION" -v prerelease="$BASE_VERSION-rc.1" '
|
||||||
|
{
|
||||||
|
line = $0
|
||||||
|
|
||||||
|
if (line ~ /bazel_dep\(name = "rules_bun", version = "/ && line !~ /-rc\.1/) {
|
||||||
|
sub(/version = "[^"]+"/, "version = \"" stable "\"", line)
|
||||||
|
} else if (line ~ /bazel_dep\(name = "rules_bun", version = "/ && line ~ /-rc\.1/) {
|
||||||
|
sub(/version = "[^"]+"/, "version = \"" prerelease "\"", line)
|
||||||
|
} else if (line ~ /archive\/refs\/tags\/v/ && line !~ /-rc\.1/) {
|
||||||
|
sub(/v[^"]+\.tar\.gz/, "v" stable ".tar.gz", line)
|
||||||
|
} else if (line ~ /archive\/refs\/tags\/v/ && line ~ /-rc\.1/) {
|
||||||
|
sub(/v[^"]+\.tar\.gz/, "v" prerelease ".tar.gz", line)
|
||||||
|
} else if (line ~ /strip_prefix = "rules_bun-v/ && line !~ /-rc\.1/) {
|
||||||
|
sub(/rules_bun-v[^"]+/, "rules_bun-v" stable, line)
|
||||||
|
} else if (line ~ /strip_prefix = "rules_bun-v/ && line ~ /-rc\.1/) {
|
||||||
|
sub(/rules_bun-v[^"]+/, "rules_bun-v" prerelease, line)
|
||||||
|
} else if (line ~ /For channel\/pre-release tags \(for example `v.*-rc\.1`\), use the matching folder prefix:/) {
|
||||||
|
sub(/`v[^`]+`/, "`v" prerelease "`", line)
|
||||||
|
}
|
||||||
|
|
||||||
|
print line
|
||||||
|
}
|
||||||
|
' "$README" > "$TMP" && mv "$TMP" "$README"
|
||||||
|
'';
|
||||||
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
postVersion = ''
|
postVersion = ''
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"])
|
|||||||
exports_files([
|
exports_files([
|
||||||
"bun_binary.bzl",
|
"bun_binary.bzl",
|
||||||
"bun_bundle.bzl",
|
"bun_bundle.bzl",
|
||||||
|
"bun_dev.bzl",
|
||||||
"bun_install.bzl",
|
"bun_install.bzl",
|
||||||
"bun_test.bzl",
|
"bun_test.bzl",
|
||||||
"js_library.bzl",
|
"js_library.bzl",
|
||||||
|
|||||||
142
internal/bun_dev.bzl
Normal file
142
internal/bun_dev.bzl
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
"""Rule for running JS/TS scripts with Bun in watch mode for development."""
|
||||||
|
|
||||||
|
|
||||||
|
def _bun_dev_impl(ctx):
|
||||||
|
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||||
|
bun_bin = toolchain.bun.bun_bin
|
||||||
|
entry_point = ctx.file.entry_point
|
||||||
|
|
||||||
|
restart_watch_paths = "\n".join([path.short_path for path in ctx.files.restart_on])
|
||||||
|
|
||||||
|
launcher = ctx.actions.declare_file(ctx.label.name)
|
||||||
|
ctx.actions.write(
|
||||||
|
output = launcher,
|
||||||
|
is_executable = True,
|
||||||
|
content = """#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
runfiles_dir="${{RUNFILES_DIR:-$0.runfiles}}"
|
||||||
|
bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
|
||||||
|
entry_point="${{runfiles_dir}}/_main/{entry_short_path}"
|
||||||
|
cd "${{runfiles_dir}}/_main"
|
||||||
|
|
||||||
|
watch_mode="{watch_mode}"
|
||||||
|
if [[ "${{watch_mode}}" == "hot" ]]; then
|
||||||
|
dev_flag="--hot"
|
||||||
|
else
|
||||||
|
dev_flag="--watch"
|
||||||
|
fi
|
||||||
|
|
||||||
|
run_dev() {{
|
||||||
|
exec "${{bun_bin}}" "${{dev_flag}}" run "${{entry_point}}" "$@"
|
||||||
|
}}
|
||||||
|
|
||||||
|
if [[ {restart_count} -eq 0 ]]; then
|
||||||
|
run_dev "$@"
|
||||||
|
fi
|
||||||
|
|
||||||
|
readarray -t restart_paths <<'EOF_RESTART_PATHS'
|
||||||
|
{restart_watch_paths}
|
||||||
|
EOF_RESTART_PATHS
|
||||||
|
|
||||||
|
file_mtime() {{
|
||||||
|
local p="$1"
|
||||||
|
if stat -f '%m' "${{p}}" >/dev/null 2>&1; then
|
||||||
|
stat -f '%m' "${{p}}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
stat -c '%Y' "${{p}}"
|
||||||
|
}}
|
||||||
|
|
||||||
|
declare -A mtimes
|
||||||
|
for rel in "${{restart_paths[@]}}"; do
|
||||||
|
if [[ -e "${{rel}}" ]]; then
|
||||||
|
mtimes["${{rel}}"]="$(file_mtime "${{rel}}")"
|
||||||
|
else
|
||||||
|
mtimes["${{rel}}"]="missing"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
child_pid=""
|
||||||
|
restart_child() {{
|
||||||
|
if [[ -n "${{child_pid}}" ]] && kill -0 "${{child_pid}}" 2>/dev/null; then
|
||||||
|
kill "${{child_pid}}"
|
||||||
|
wait "${{child_pid}}" || true
|
||||||
|
fi
|
||||||
|
"${{bun_bin}}" "${{dev_flag}}" run "${{entry_point}}" "$@" &
|
||||||
|
child_pid=$!
|
||||||
|
}}
|
||||||
|
|
||||||
|
cleanup() {{
|
||||||
|
if [[ -n "${{child_pid}}" ]] && kill -0 "${{child_pid}}" 2>/dev/null; then
|
||||||
|
kill "${{child_pid}}"
|
||||||
|
wait "${{child_pid}}" || true
|
||||||
|
fi
|
||||||
|
}}
|
||||||
|
|
||||||
|
trap cleanup EXIT INT TERM
|
||||||
|
|
||||||
|
restart_child "$@"
|
||||||
|
|
||||||
|
while true; do
|
||||||
|
sleep 1
|
||||||
|
changed=0
|
||||||
|
for rel in "${{restart_paths[@]}}"; do
|
||||||
|
if [[ -e "${{rel}}" ]]; then
|
||||||
|
current="$(file_mtime "${{rel}}")"
|
||||||
|
else
|
||||||
|
current="missing"
|
||||||
|
fi
|
||||||
|
if [[ "${{current}}" != "${{mtimes[${{rel}}]}}" ]]; then
|
||||||
|
mtimes["${{rel}}"]="${{current}}"
|
||||||
|
changed=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [[ "${{changed}}" -eq 1 ]]; then
|
||||||
|
restart_child "$@"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
""".format(
|
||||||
|
bun_short_path = bun_bin.short_path,
|
||||||
|
entry_short_path = entry_point.short_path,
|
||||||
|
watch_mode = ctx.attr.watch_mode,
|
||||||
|
restart_count = len(ctx.files.restart_on),
|
||||||
|
restart_watch_paths = restart_watch_paths,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
transitive_files = []
|
||||||
|
if ctx.attr.node_modules:
|
||||||
|
transitive_files.append(ctx.attr.node_modules[DefaultInfo].files)
|
||||||
|
|
||||||
|
runfiles = ctx.runfiles(
|
||||||
|
files = [bun_bin, entry_point] + ctx.files.data + ctx.files.restart_on,
|
||||||
|
transitive_files = depset(transitive = transitive_files),
|
||||||
|
)
|
||||||
|
|
||||||
|
return [
|
||||||
|
DefaultInfo(
|
||||||
|
executable = launcher,
|
||||||
|
runfiles = runfiles,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
bun_dev = rule(
|
||||||
|
implementation = _bun_dev_impl,
|
||||||
|
attrs = {
|
||||||
|
"entry_point": attr.label(
|
||||||
|
mandatory = True,
|
||||||
|
allow_single_file = [".js", ".ts", ".jsx", ".tsx", ".mjs", ".cjs"],
|
||||||
|
),
|
||||||
|
"watch_mode": attr.string(
|
||||||
|
default = "watch",
|
||||||
|
values = ["watch", "hot"],
|
||||||
|
),
|
||||||
|
"restart_on": attr.label_list(allow_files = True),
|
||||||
|
"node_modules": attr.label(),
|
||||||
|
"data": attr.label_list(allow_files = True),
|
||||||
|
},
|
||||||
|
executable = True,
|
||||||
|
toolchains = ["//bun:toolchain_type"],
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user