Skip to content

Commit 1d70526

Browse files
committed
feat: add omit-api bundler variants
1 parent 1c24db8 commit 1d70526

14 files changed

Lines changed: 98096 additions & 1890 deletions

‎README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,34 @@ this package.
169169

170170
2. Run the build:
171171

172+
By default, this builds the full upstream npm bundle plus these omit-API variants. All variants
173+
omit the deprecated Worker1 API:
174+
175+
- `core`: `omit-api="kvvfs OPFS vtab worker1"`, exported as
176+
`@sqlite.org/sqlite-wasm/bundler/core`
177+
- `core-vtab`: `omit-api="kvvfs OPFS worker1"`, exported as
178+
`@sqlite.org/sqlite-wasm/bundler/vtab`
179+
- `core-kvvfs`: `omit-api="OPFS vtab worker1"`, exported as
180+
`@sqlite.org/sqlite-wasm/bundler/kvvfs`
181+
- `core-opfs`: `omit-api="kvvfs opfs-wl opfs-sahpool vtab worker1"`, exported as
182+
`@sqlite.org/sqlite-wasm/bundler/opfs`
183+
- `core-opfs-wl`: `omit-api="kvvfs opfs opfs-sahpool vtab worker1"`, exported as
184+
`@sqlite.org/sqlite-wasm/bundler/opfs-wl`
185+
- `core-opfs-sahpool`: `omit-api="kvvfs opfs opfs-wl vtab worker1"`, exported as
186+
`@sqlite.org/sqlite-wasm/bundler/sah-pool`
187+
188+
To customize the generated variants, set `SQLITE_WASM_OMIT_API_BUILDS` to a semicolon-separated
189+
list of `name=api api` entries. Set it to an empty string to build only the full bundle. Custom
190+
variants are written to `src/bin`; add them to `tsdown.config.ts` and `package.json` if they
191+
should be published as package subpaths.
192+
193+
Only bundler-friendly JavaScript artifacts are kept for generated variants. They are written to
194+
`src/bin` with their variant name in the filename, for example
195+
`sqlite3-core-bundler-friendly.mjs`, and load the default `sqlite3.wasm` from the full npm bundle
196+
build. The package build emits optimized copies into `dist` and exports the fixed variants under
197+
the `/bundler` package subpaths shown above. If the selected SQLite ref does not support
198+
`omit-api`, set `SQLITE_WASM_OMIT_API_BUILDS` to an empty string to build only the full bundle.
199+
172200
**Unix (Linux/macOS):**
173201

174202
```bash

‎package.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@
3939
"browser": "./dist/index.mjs"
4040
},
4141
"./package.json": "./package.json",
42-
"./sqlite3.wasm": "./dist/sqlite3.wasm"
42+
"./sqlite3.wasm": "./dist/sqlite3.wasm",
43+
"./bundler/core": "./dist/sqlite3-core-bundler-friendly.mjs",
44+
"./bundler/vtab": "./dist/sqlite3-core-vtab-bundler-friendly.mjs",
45+
"./bundler/kvvfs": "./dist/sqlite3-core-kvvfs-bundler-friendly.mjs",
46+
"./bundler/opfs": "./dist/sqlite3-core-opfs-bundler-friendly.mjs",
47+
"./bundler/opfs-wl": "./dist/sqlite3-core-opfs-wl-bundler-friendly.mjs",
48+
"./bundler/sah-pool": "./dist/sqlite3-core-opfs-sahpool-bundler-friendly.mjs"
4349
},
4450
"workspaces": [
4551
"demos/*"

‎scripts/build-sqlite3-wasm.sh‎

Lines changed: 125 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ OUT_DIR="${OUT_DIR:-/out}"
77
SRC_DIR="${WORKDIR:-/build/sqlite-src}"
88
HOST_UID="${HOST_UID:-}"
99
HOST_GID="${HOST_GID:-}"
10+
DEFAULT_OMIT_API_BUILDS="core=kvvfs OPFS vtab worker1;core-vtab=kvvfs OPFS worker1;core-kvvfs=OPFS vtab worker1;core-opfs=kvvfs opfs-wl opfs-sahpool vtab worker1;core-opfs-wl=kvvfs opfs opfs-sahpool vtab worker1;core-opfs-sahpool=kvvfs opfs opfs-wl vtab worker1"
11+
12+
if [ "${SQLITE_WASM_OMIT_API_BUILDS+x}" = "x" ]; then
13+
OMIT_API_BUILDS="$SQLITE_WASM_OMIT_API_BUILDS"
14+
elif [ "${OMIT_API_BUILDS+x}" != "x" ]; then
15+
OMIT_API_BUILDS="$DEFAULT_OMIT_API_BUILDS"
16+
fi
1017

1118
# Ensure out exists and is writable
1219
mkdir -p "$OUT_DIR"
@@ -34,6 +41,84 @@ check_mounts() {
3441
# Helper: log
3542
log() { printf '\033[1;34m[build]\033[0m %s\n' "$*"; }
3643

44+
trim() {
45+
local value="$*"
46+
value="${value#"${value%%[![:space:]]*}"}"
47+
value="${value%"${value##*[![:space:]]}"}"
48+
printf '%s' "$value"
49+
}
50+
51+
extract_npm_bundle() {
52+
local zip_file="$1"
53+
local target_dir="$2"
54+
55+
mkdir -p "$target_dir"
56+
find "$target_dir" -mindepth 1 -maxdepth 1 -exec rm -rf {} +
57+
unzip -q -o "$zip_file" -d "$target_dir"
58+
59+
shopt -s nullglob
60+
local entries=("$target_dir"/*)
61+
if [ "${#entries[@]}" -eq 1 ] && [ -d "${entries[0]}" ]; then
62+
log "Zip contained top-level dir: moving contents up"
63+
mv "${entries[0]}"/* "$target_dir"/ || true
64+
rmdir "${entries[0]}" || true
65+
fi
66+
shopt -u nullglob
67+
}
68+
69+
copy_if_exists() {
70+
local source_file="$1"
71+
local target_file="$2"
72+
73+
if [ -f "$source_file" ]; then
74+
cp -f "$source_file" "$target_file"
75+
fi
76+
}
77+
78+
install_variant_artifacts() {
79+
local variant="$1"
80+
local extracted_dir="$2"
81+
82+
log "Installing bundler-friendly variant artifacts for: $variant"
83+
copy_if_exists "$extracted_dir/sqlite3-bundler-friendly.mjs" "/src/bin/sqlite3-${variant}-bundler-friendly.mjs"
84+
}
85+
86+
create_variant_zip() {
87+
local variant="$1"
88+
local zip_file="$2"
89+
local staging_dir="/tmp/sqlite-wasm-variant-${variant}"
90+
91+
rm -rf "$staging_dir"
92+
mkdir -p "$staging_dir"
93+
cp -f "/src/bin/sqlite3-${variant}-bundler-friendly.mjs" "$staging_dir/"
94+
95+
rm -f "$zip_file"
96+
(cd "$staging_dir" && zip -q -r "$zip_file" .)
97+
chmod a+r "$zip_file"
98+
}
99+
100+
build_npm_bundle() {
101+
local label="$1"
102+
local omit_apis="$2"
103+
104+
log "Running make clean for $label"
105+
make clean >/dev/null 2>&1 || true
106+
rm -f npm-bundle.zip
107+
108+
if [ -n "$omit_apis" ]; then
109+
log "Running make -j4 npm omit-api=\"$omit_apis\" (ext/wasm)"
110+
make -j4 npm omit-api="$omit_apis"
111+
else
112+
log "Running make -j4 npm (ext/wasm)"
113+
make -j4 npm
114+
fi
115+
116+
if [ ! -f npm-bundle.zip ]; then
117+
log "ERROR: npm-bundle.zip not found in ext/wasm"
118+
exit 3
119+
fi
120+
}
121+
37122
# Handle ownership fixup on exit
38123
cleanup() {
39124
if [ -n "$HOST_UID" ] && [ -n "$HOST_GID" ]; then
@@ -58,6 +143,7 @@ log "Repo: $SQLITE_REPO"
58143
log "Ref: $SQLITE_REF"
59144
log "Out: $OUT_DIR"
60145
log "Src dir: $SRC_DIR"
146+
log "Omit API builds: ${OMIT_API_BUILDS:-<none>}"
61147

62148
# prepare source dir: clone shallow or fetch+reset if exists
63149
if [ -d "$SRC_DIR/.git" ]; then
@@ -106,37 +192,54 @@ make sqlite3.c
106192

107193
cd ext/wasm
108194

109-
log "Running make -j4 npm (ext/wasm)"
110-
make -j4 npm
111-
112-
# ensure artifact exists
113-
if [ ! -f npm-bundle.zip ]; then
114-
log "ERROR: npm-bundle.zip not found in ext/wasm"
115-
exit 3
195+
if [ -n "${OMIT_API_BUILDS:-}" ] && ! grep -q 'omit-api' GNUmakefile; then
196+
log "ERROR: omit API builds were requested, but this SQLite ref does not support omit-api."
197+
log "Set SQLITE_WASM_OMIT_API_BUILDS= to build only the full bundle, or use a SQLite ref with omit-api support."
198+
exit 5
116199
fi
117200

118-
# copy zip into OUT_DIR
201+
build_npm_bundle "full" ""
202+
203+
# copy zip into OUT_DIR and extract the full build into /src/bin
119204
log "Copying npm-bundle.zip to $OUT_DIR"
120205
cp -f npm-bundle.zip "$OUT_DIR/npm-bundle.zip"
121206
chmod a+r "$OUT_DIR/npm-bundle.zip"
122207

123208
# extract into /src/bin (normalize single top-level dir)
124209
log "Extracting npm-bundle.zip into /src/bin"
125-
# Clear existing contents to ensure a clean overwrite
126-
rm -rf /src/bin/*
127-
mkdir -p /src/bin
128-
unzip -q -o "$OUT_DIR/npm-bundle.zip" -d /src/bin
129-
rm -f "$OUT_DIR/npm-bundle.zip"
130-
131-
# If a single top-level directory exists, move its contents up
132-
shopt -s nullglob
133-
entries=(/src/bin/*)
134-
if [ "${#entries[@]}" -eq 1 ] && [ -d "${entries[0]}" ]; then
135-
log "Zip contained top-level dir: moving contents up"
136-
mv "${entries[0]}"/* /src/bin/ || true
137-
rmdir "${entries[0]}" || true
210+
extract_npm_bundle "$OUT_DIR/npm-bundle.zip" /src/bin
211+
212+
if [ -n "${OMIT_API_BUILDS:-}" ]; then
213+
IFS=';' read -r -a omit_build_specs <<< "$OMIT_API_BUILDS"
214+
for spec in "${omit_build_specs[@]}"; do
215+
spec="$(trim "$spec")"
216+
if [ -z "$spec" ]; then
217+
continue
218+
fi
219+
220+
if [[ "$spec" != *=* ]]; then
221+
log "ERROR: invalid omit API build spec '$spec'. Expected name=api api."
222+
exit 4
223+
fi
224+
225+
variant="$(trim "${spec%%=*}")"
226+
omit_apis="$(trim "${spec#*=}")"
227+
if [[ ! "$variant" =~ ^[a-z0-9][a-z0-9-]*$ ]]; then
228+
log "ERROR: invalid variant name '$variant'. Use lowercase letters, numbers, and hyphens."
229+
exit 4
230+
fi
231+
232+
build_npm_bundle "$variant" "$omit_apis"
233+
234+
variant_zip="$OUT_DIR/npm-bundle-${variant}.zip"
235+
variant_dir="/tmp/sqlite-wasm-npm-${variant}"
236+
log "Extracting variant bundle for artifact renaming"
237+
extract_npm_bundle npm-bundle.zip "$variant_dir"
238+
install_variant_artifacts "$variant" "$variant_dir"
239+
log "Writing bundler-friendly variant bundle to $variant_zip"
240+
create_variant_zip "$variant" "$variant_zip"
241+
done
138242
fi
139-
shopt -u nullglob
140243

141244
log "Build complete. Extracted contents are in /src/bin"
142245
exec /bin/ls -la /src/bin

‎src/bin/sqlite3-bundler-friendly.mjs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
**
3030
** SQLITE_VERSION "3.53.4"
3131
** SQLITE_VERSION_NUMBER 3053004
32-
** SQLITE_SOURCE_ID "2026-07-24 19:02:57 bf7c7f30031888f4e796e429ab3978879485813aaca6f641c7b33e4e09459bcc"
32+
** SQLITE_SOURCE_ID "2026-07-24 19:02:57 bf7c7f30031888f4e796e429ab3978879485813aaca6f641c7b33e4e0945alt1"
3333
**
3434
** Emscripten SDK: 5.0.5
3535
*/
@@ -4733,7 +4733,7 @@ Module.runSQLite3PostLoadInit = async function(
47334733
**
47344734
** SQLITE_VERSION "3.53.4"
47354735
** SQLITE_VERSION_NUMBER 3053004
4736-
** SQLITE_SOURCE_ID "2026-07-24 19:02:57 bf7c7f30031888f4e796e429ab3978879485813aaca6f641c7b33e4e09459bcc"
4736+
** SQLITE_SOURCE_ID "2026-07-24 19:02:57 bf7c7f30031888f4e796e429ab3978879485813aaca6f641c7b33e4e0945alt1"
47374737
**
47384738
** Emscripten SDK: 5.0.5
47394739
*/
@@ -6978,7 +6978,7 @@ globalThis.sqlite3ApiBootstrap.defaultConfig = Object.create(null);
69786978
*/
69796979
globalThis.sqlite3ApiBootstrap.sqlite3 = undefined;
69806980
globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
6981-
sqlite3.version = {"libVersion": "3.53.4", "libVersionNumber": 3053004, "sourceId": "2026-07-24 19:02:57 bf7c7f30031888f4e796e429ab3978879485813aaca6f641c7b33e4e09459bcc","downloadVersion": 3530400,"scm":{ "sha3-256": "bf7c7f30031888f4e796e429ab3978879485813aaca6f641c7b33e4e09459bcc","branch": "branch-3.53","tags": "release version-3.53.4","datetime": "2026-07-24T19:02:57.525Z"}};
6981+
sqlite3.version = {"libVersion": "3.53.4", "libVersionNumber": 3053004, "sourceId": "2026-07-24 19:02:57 bf7c7f30031888f4e796e429ab3978879485813aaca6f641c7b33e4e0945alt1","downloadVersion": 3530400,"scm":{ "sha3-256": "bf7c7f30031888f4e796e429ab3978879485813aaca6f641c7b33e4e0945alt1","branch": "branch-3.53","tags": "release version-3.53.4","datetime": "2026-07-24T19:02:57.525Z"}};
69826982
});
69836983
/**
69846984
2022-07-08
@@ -19565,6 +19565,9 @@ globalThis.sqlite3ApiBootstrap.initializersAsync.push(async (sqlite3)=>{
1956519565
- Also because of that, it does not require the SharedArrayBuffer,
1956619566
so can function without the COOP/COEP HTTP response headers.
1956719567

19568+
- Also because of that, this one is much faster and the performance
19569+
gap increases as the job sizes increase.
19570+
1956819571
- It can hypothetically support Safari 16.4+, whereas the "opfs" VFS
1956919572
requires v17 due to a subworker/storage bug in 16.x which makes it
1957019573
incompatible with that VFS.

0 commit comments

Comments
 (0)