Skip to content

Commit cddc3ef

Browse files
feat: demonstrate Node-API for native interop (#468)
* feat: demonstrate Node-API for native interop * Apply suggestions from code review Co-authored-by: aspect-workflows[bot] <143031405+aspect-workflows[bot]@users.noreply.github.com> * fix gazelle --------- Co-authored-by: aspect-workflows[bot] <143031405+aspect-workflows[bot]@users.noreply.github.com>
1 parent f794ee9 commit cddc3ef

File tree

16 files changed

+648
-1
lines changed

16 files changed

+648
-1
lines changed

MODULE.bazel

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ bazel_dep(name = "rules_nodejs", version = "6.3.5")
3232
bazel_dep(name = "rules_oci", version = "2.0.1")
3333
bazel_dep(name = "rules_proto", version = "7.1.0")
3434
bazel_dep(name = "rules_python", version = "0.40.0")
35+
bazel_dep(name = "rules_rust", version = "0.63.0")
3536
bazel_dep(name = "rules_mypy", version = "0.29.0")
3637
bazel_dep(name = "rules_python_gazelle_plugin", version = "0.35.0")
3738
bazel_dep(name = "rules_shell", version = "0.4.1")
@@ -179,7 +180,10 @@ use_repo(
179180
# https://github.com/aspect-build/rules_ts/tree/main/e2e/bzlmod
180181

181182
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node", dev_dependency = True)
182-
node.toolchain(node_version = "20.18.0")
183+
node.toolchain(
184+
include_headers = True,
185+
node_version = "20.18.0",
186+
)
183187
use_repo(node, "nodejs_toolchains")
184188

185189
pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")
@@ -277,3 +281,20 @@ oci.pull(
277281
tag = "latest",
278282
)
279283
use_repo(oci, "distroless_base", "distroless_base_linux_amd64", "distroless_base_linux_arm64", "ubuntu", "ubuntu_linux_amd64", "ubuntu_linux_arm64_v8")
284+
285+
#########################
286+
# Support for Rust, see https://github.com/bazelbuild/rules_rust
287+
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
288+
rust.toolchain(
289+
edition = "2024",
290+
versions = ["1.89.0"],
291+
)
292+
293+
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
294+
crate.from_cargo(
295+
name = "crate_index",
296+
# TODO: we should have a Cargo.toml file in the repository root
297+
cargo_lockfile = "//npm_packages/napi:Cargo.lock",
298+
manifests = ["//npm_packages/napi:Cargo.toml"],
299+
)
300+
use_repo(crate, "crate_index")

nodejs_apps/typescript/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ts_project(
1111
declaration = True,
1212
tsconfig = {},
1313
deps = [
14+
":node_modules/@bazel-examples/napi",
1415
":node_modules/@bazel-examples/one",
1516
":node_modules/@bazel-examples/shared",
1617
":node_modules/@types/node",

nodejs_apps/typescript/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"inspirational-quotes": "2.0.1",
44
"@bazel-examples/one": "workspace:*",
55
"@bazel-examples/shared": "workspace:*",
6+
"@bazel-examples/napi": "workspace:*",
67
"@types/node": "18.11.9",
78
"star-wars-quotes": "1.0.2"
89
}

nodejs_apps/typescript/src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { platform, arch } from 'node:os';
22
import { one } from '@bazel-examples/one';
33
import { shared } from '@bazel-examples/shared';
4+
import { rust, c } from '@bazel-examples/napi';
45
import { getRandomQuote } from 'inspirational-quotes';
56
import * as quotes from 'star-wars-quotes';
67

78
shared();
89
one();
910
console.log(getRandomQuote());
1011
console.log(quotes());
12+
console.log(rust.hello('NodeJS'));
13+
console.log('C code does math: 1 + 2 = ' + c.add(1, 2));
1114
console.log('Platform: ' + platform());
1215
console.log('Architecture: ' + arch());

npm_packages/napi/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_library")
2+
load("@npm//:defs.bzl", "npm_link_all_packages")
3+
4+
npm_link_all_packages(name = "node_modules")
5+
6+
js_library(
7+
name = "pkg",
8+
srcs = ["package.json"],
9+
visibility = ["//visibility:public"],
10+
deps = ["//npm_packages/napi/src:tsc"],
11+
)

npm_packages/napi/Cargo.lock

Lines changed: 260 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

npm_packages/napi/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "bazel-examples-napi"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
napi = "3.1.0"
11+
napi-derive = "3.1.0"
12+
13+
[build-dependencies]
14+
napi-build = "2"
15+
16+
[profile.release]
17+
lto = true
18+
strip = "symbols"

0 commit comments

Comments
 (0)