Add build for nodejs or web

Change how VFS gets loaded based on feature flags
This commit is contained in:
Elijah Morgan 2025-01-01 16:54:31 -05:00
parent 5765ccbfbb
commit 4bda7803c3
4 changed files with 48 additions and 1 deletions

View file

@ -17,3 +17,8 @@ limbo_core = { path = "../../core", default-features = false }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = "0.3"
[features]
web = []
nodejs = []
default = ["nodejs"]

View file

@ -4,9 +4,15 @@ This source tree contains Limbo Wasm bindings.
## Building
For nodejs
```
./scripts/build
```
For web
```
./scripts/build web
```
# Browser Support

View file

@ -303,10 +303,39 @@ impl limbo_core::DatabaseStorage for DatabaseStorage {
}
}
#[cfg(all(feature = "web", feature = "nodejs"))]
compile_error!("Features 'web' and 'nodejs' cannot be enabled at the same time");
#[cfg(feature = "web")]
#[wasm_bindgen(module = "/src/web-vfs.js")]
extern "C" {
type VFS;
#[wasm_bindgen(constructor)]
fn new() -> VFS;
#[wasm_bindgen(method)]
fn open(this: &VFS, path: &str, flags: &str) -> i32;
#[wasm_bindgen(method)]
fn close(this: &VFS, fd: i32) -> bool;
#[wasm_bindgen(method)]
fn pwrite(this: &VFS, fd: i32, buffer: &[u8], offset: usize) -> i32;
#[wasm_bindgen(method)]
fn pread(this: &VFS, fd: i32, buffer: &mut [u8], offset: usize) -> i32;
#[wasm_bindgen(method)]
fn size(this: &VFS, fd: i32) -> u64;
#[wasm_bindgen(method)]
fn sync(this: &VFS, fd: i32);
}
#[cfg(feature = "nodejs")]
#[wasm_bindgen(module = "/vfs.js")]
extern "C" {
type VFS;
#[wasm_bindgen(constructor)]
fn new() -> VFS;

View file

@ -1,4 +1,11 @@
#!/bin/bash
wasm-pack build --no-pack --target web
TARGET=${1:-nodejs}
FEATURE="nodejs"
if [ "$TARGET" = "web" ]; then
FEATURE="web"
fi
wasm-pack build --no-pack --target $TARGET --no-default-features --features $FEATURE
cp package.json pkg/package.json