Skip to content

Conversation

@rxliuli
Copy link

@rxliuli rxliuli commented Feb 11, 2026

Summary

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. These require() calls exist in the emcc-generated code because the default ENVIRONMENT includes node.

This PR adds a new sql-wasm-browser.js build variant that eliminates these warnings:

  • New EMFLAGS_WASM_BROWSER in Makefile: same as EMFLAGS_WASM but with -s ENVIRONMENT=web,worker, which tells Emscripten to strip all Node.js code paths at compile time
  • New build targets: dist/sql-wasm-browser.js (optimized) and dist/sql-wasm-browser-debug.js (debug)
  • Conditional exports in package.json: bundlers targeting browsers automatically resolve to the clean sql-wasm-browser.js via the browser condition, while Node.js and other environments continue using the original sql-wasm.js

Before (bundler output)

[plugin rolldown:vite-resolve] Module "fs" has been externalized for browser compatibility...
[plugin rolldown:vite-resolve] Module "crypto" has been externalized for browser compatibility...
[plugin rolldown:vite-resolve] Module "path" has been externalized for browser compatibility...

After

No sql.js related warnings.

Build comparison

File require() calls Size
sql-wasm.js (unchanged) require("node:fs"), require("node:crypto") 45.7 KB
sql-wasm-browser.js (new) None 44.3 KB
.wasm binary Both identical 658 KB

No breaking changes — the original sql-wasm.js is fully preserved as the default export.

Closes #620

Test plan

  • Build sql-wasm-browser.js with make dist/sql-wasm-browser.js — succeeds
  • Verify sql-wasm-browser.js contains zero require() calls
  • Verify sql-wasm.js still contains Node.js support code
  • Test in a real Vite/WXT project — bundler warnings eliminated

🤖 Generated with Claude Code

…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>
@rxliuli
Copy link
Author

rxliuli commented Feb 11, 2026

Verified with pnpm patch

I've verified this change works in a real project (a Chrome extension built with WXT + Vite 7 + rolldown). After applying the patch:

  • All Module "fs"/"crypto"/"path" has been externalized for browser compatibility warnings are completely eliminated
  • The inject.js bundle size decreased by ~4KB (924.98 KB → 920.62 KB) due to removal of dead Node.js code paths
  • No functional regressions

For pnpm users who need this fix now

You can use pnpm patch to apply this change before it's merged and released. I've attached the patch file — to use it:

  1. Download the attached sql.js@1.13.0.patch file to your project's patches/ directory
  2. Add to your package.json:
    {
      "pnpm": {
        "patchedDependencies": {
          "sql.js@1.13.0": "patches/sql.js@1.13.0.patch"
        }
      }
    }
  3. Run pnpm install

Note: The patch file is large (~766KB) because it includes the pre-built sql-wasm-browser.js and sql-wasm-browser.wasm files, since building from source requires Emscripten.

@rxliuli
Copy link
Author

rxliuli commented Feb 11, 2026

Patch file: https://gist.github.com/rxliuli/06a26d53fa45359c8079834aa20d4369

Download the raw file and place it at patches/sql.js@1.13.0.patch in your project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to bundle due to "fs" (sveltekit + cloudflare workers).

1 participant