feat: add browser-only WASM build variant#622
Conversation
…ings in bundlers
When bundling sql.js for browser environments, tools like Vite, Webpack,
and esbuild emit warnings about `require("node:fs")` and
`require("node:crypto")` being externalized for browser compatibility.
This adds a new `sql-wasm-browser.js` build variant compiled with
`-s ENVIRONMENT=web,worker`, which completely eliminates Node.js code
paths from the generated JavaScript. The browser variant is exposed via
the `exports` field in package.json using the `browser` condition, so
bundlers automatically select the clean version.
The original `sql-wasm.js` with full Node.js support is preserved as
the `default` export, ensuring no breaking changes for existing users.
Closes sql-js#620
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Verified with pnpm patchI've verified this change works in a real project (a Chrome extension built with WXT + Vite 7 + rolldown). After applying the patch:
For pnpm users who need this fix nowYou can use
|
|
Patch file: https://gist.github.com/rxliuli/06a26d53fa45359c8079834aa20d4369 Download the raw file and place it at |
|
For reference: the new build introduced by this pr was broken, with most functions misbehaving because of an optimization. Since the tests were not run against the new build, we did not notice. see #624 |
Summary
When bundling sql.js for browser environments, tools like Vite, Webpack, and esbuild emit warnings about
require("node:fs")andrequire("node:crypto")being externalized for browser compatibility. Theserequire()calls exist in the emcc-generated code because the defaultENVIRONMENTincludesnode.This PR adds a new
sql-wasm-browser.jsbuild variant that eliminates these warnings:EMFLAGS_WASM_BROWSERin Makefile: same asEMFLAGS_WASMbut with-s ENVIRONMENT=web,worker, which tells Emscripten to strip all Node.js code paths at compile timedist/sql-wasm-browser.js(optimized) anddist/sql-wasm-browser-debug.js(debug)package.json: bundlers targeting browsers automatically resolve to the cleansql-wasm-browser.jsvia thebrowsercondition, while Node.js and other environments continue using the originalsql-wasm.jsBefore (bundler output)
After
No sql.js related warnings.
Build comparison
require()callssql-wasm.js(unchanged)require("node:fs"),require("node:crypto")sql-wasm-browser.js(new).wasmbinaryNo breaking changes — the original
sql-wasm.jsis fully preserved as thedefaultexport.Closes #620
Test plan
sql-wasm-browser.jswithmake dist/sql-wasm-browser.js— succeedssql-wasm-browser.jscontains zerorequire()callssql-wasm.jsstill contains Node.js support code🤖 Generated with Claude Code