Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from rules_nodejs to rules_js #515

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
embeddy/node_modules
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Want to help add features or fix bugs? Awesome! vbs is build using bazel.
grab the source code from github
`bazel run vbs` to compile and run the locally compiled version

### Running bazel managed version of pnpm for nextjs updates

bazel run -- @pnpm//:pnpm --dir $PWD

### Testing release process

To run goreleaser locally to test changes to the release process configuration:
Expand Down
43 changes: 22 additions & 21 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,35 @@ load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "6f15d75f9e99c19d9291ff8e64e4eb594a6b7d25517760a75ad3621a7a48c2df",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.7.0/rules_nodejs-4.7.0.tar.gz"],
name = "aspect_rules_js",
sha256 = "b9fde0f20de6324ad443500ae738bda00facbd73900a12b417ce794856e01407",
strip_prefix = "rules_js-1.5.0",
url = "https://github.com/aspect-build/rules_js/archive/refs/tags/v1.5.0.tar.gz",
)

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")

load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install")
# NOTE: this rule installs nodejs, npm, and yarn, but does NOT install
# your npm dependencies into your node_modules folder.
# You must still run the package manager to do this.
# M1 Macs require Node 16+
node_repositories(
package_json = ["//embeddy:package.json"],
node_version = "16.13.0",
rules_js_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "nodejs",
node_version = DEFAULT_NODE_VERSION,
)

# Setup Bazel managed npm dependencies with the `yarn_install` rule.
# The name of this rule should be set to `npm` so that `ts_library` and `ts_web_test_suite`
# can find your npm dependencies by default in the `@npm` workspace. You may
# also use the `npm_install` rule with a `package-lock.json` file if you prefer.
# See https://github.com/bazelbuild/rules_nodejs#dependencies for more info.
yarn_install(
name = "npm",
package_json = "//embeddy:package.json",
quiet = False,
yarn_lock = "//embeddy:yarn.lock",
load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock")

npm_translate_lock(
name = "npm",
pnpm_lock = "//embeddy:pnpm-lock.yaml",
verify_node_modules_ignored = "//:.bazelignore",
)

load("@npm//:repositories.bzl", "npm_repositories")

npm_repositories()

http_archive(
name = "io_bazel_rules_docker",
sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf",
Expand Down
63 changes: 54 additions & 9 deletions embeddy/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//:glue.bzl", "embed_nextjs")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
load("//:glue.bzl", "static_site_embedder")
load("@npm//embeddy:next/package_json.bzl", next_bin = "bin")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_js//js:defs.bzl", "js_run_binary", "js_binary")
npm_link_all_packages(name = "node_modules")

# helpful example of how to use rules_js with next.js is here:
# https://github.com/aspect-build/bazel-examples/blob/main/next.js/BUILD.bazel
# TODO: set up typescript compilation of pages folder

filegroup(
name = "source_files",
Expand All @@ -8,19 +17,43 @@ filegroup(
"pages/*",
"styles/*",
]) + [
#"tsconfig.json",
#"next.config.js",
#"next-env.d.ts",
"package.json",
"yarn.lock",
#"postcss.config.js",
#"tailwind.config.js",
],
)

embed_nextjs(
name = "embedder",
copy_to_bin(
name = "copy_source_files",
srcs = [":source_files"],
visibility = ["//visibility:private"],
)

js_run_binary(
name = "build",
srcs = [
":node_modules",
":source_files",
],
outs = [".next/build-manifest.json"],
args = ["build"],
tool = ":next_bin",
chdir = package_name(),
)

next_bin.next(
name = "next_export",
srcs = [
":source_files",
":.next/build-manifest.json",
":node_modules",
],
outs = ["dist"],
chdir = package_name(),
args = ["export", "-o", "dist"],
)

static_site_embedder(
name = "embedder",
srcs = [":dist"],
)

# keep
Expand All @@ -32,3 +65,15 @@ go_library(
visibility = ["//visibility:public"],
deps = ["//vendor/github.com/labstack/echo/v5:go_default_library"],
)

# FROM https://github.com/aspect-build/bazel-examples/blob/main/next.js/BUILD.bazel
# This custom next js_binary is needed since next is very sensitive to it being found in two
# `node_modules` trees. With the standard generated `bin.next` build rule loaded from
# `@npm//:next/package_json.bzl`, next is found both in the binary's runfiles `node_modules` and in
# the execroot's `:node_modules` tree. This breaks the build. The work-around here is to have this
# `next_bin` `js_binary` be an empty shim without any `node_modules` and re-direct from it to run
# next from its execroot `:node_modules/next/dist/bin/next` entry point.
js_binary(
name = "next_bin",
entry_point = ":next_entry.js",
)
19 changes: 19 additions & 0 deletions embeddy/next_entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { spawnSync } = require('child_process');
// This path is currently a hack an specific to this projects layout with
// ../../../../../../ being the path from the the next_bin binary runfiles root
// to the execroot.
// TODO: Generalize this path in the future.
const entry = require.resolve(
`../../../../../../../${process.env.BAZEL_BINDIR}/embeddy/node_modules/next/dist/bin/next`
);
const args = process.argv.slice(2);
const spawnOptions = {
shell: process.env.SHELL,
stdio: [process.stdin, 'ignore', process.stderr],
};
const res = spawnSync(entry, args, spawnOptions);
if (res.status === null) {
// Process can fail with a null exit-code (e.g. OOM), handle appropriately
throw new Error(`Process terminated unexpectedly: ${res.signal}`);
}
process.exit(res.status);
2 changes: 1 addition & 1 deletion embeddy/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "learn-starter",
"name": "embeddy",
"version": "0.1.0",
"private": true,
"scripts": {
Expand Down
Loading