Skip to content

Commit 076119e

Browse files
suasgnclaude
andcommitted
Load the browser build asynchronously and shrink its memory
- Instantiate with `instantiateNapiModule`, so compiling doesn't block the page, and Chrome's 8 MB limit on synchronous main-thread compiles doesn't apply. - Use an 8 MB stack instead of napi's 64 MB, and start with 16 MB of memory instead of 128 MB. Memory grows on demand. - Stop recommending the threaded browser loader in the README: its `scan()` hangs when it reads more than one file from memfs, because the workers' file reads wait on the blocked calling thread. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent db0c97f commit 076119e

3 files changed

Lines changed: 7 additions & 6 deletions

File tree

‎crates/node/build.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ fn setup_browser() {
3838
"--import-memory",
3939
"--import-undefined",
4040
"--max-memory=4294967296",
41-
"-zstack-size=64000000",
41+
// 8 MB, like a native main thread. The threaded build uses napi's 64 MB.
42+
"-zstack-size=8388608",
4243
"--export=_initialize",
4344
] {
4445
println!("cargo:rustc-link-arg={arg}");

‎crates/node/npm/wasm32-wasi/README.md‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,4 @@ fsScanner.scan() // ['class', 'grid']
2626

2727
The bundler must support ESM, top-level `await`, and `new URL('…', import.meta.url)` asset references. With bundlers that don't (such as esbuild), copy `tailwindcss-oxide.browser.wasm` next to the bundled JavaScript.
2828

29-
On cross-origin isolated pages (COOP/COEP headers), import `@tailwindcss/oxide-wasm32-wasi/tailwindcss-oxide.wasi-browser.js` instead to scan in parallel on the threaded build. It uses `SharedArrayBuffer` and Web Workers, and the bundler must also serve `wasi-worker-browser.mjs`.
30-
3129
Scanning is synchronous. For larger inputs, run it in a module worker to keep the UI responsive.

‎crates/node/npm/wasm32-wasi/browser.mjs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDefaultContext, instantiateNapiModuleSync, WASI } from '@napi-rs/wasm-runtime'
1+
import { getDefaultContext, instantiateNapiModule, WASI } from '@napi-rs/wasm-runtime'
22
import { memfs } from '@napi-rs/wasm-runtime/fs'
33

44
export const { fs: __fs, vol: __volume } = memfs()
@@ -11,12 +11,14 @@ let wasi = new WASI({
1111

1212
// Non-shared memory, so no SharedArrayBuffer is needed. `scripts/build-browser.mjs` reads this
1313
// constant to check that the module fits.
14-
const INITIAL_MEMORY_PAGES = 2048
14+
const INITIAL_MEMORY_PAGES = 256
1515
let memory = new WebAssembly.Memory({ initial: INITIAL_MEMORY_PAGES, maximum: 65536 })
1616
let response = await fetch(new URL('./tailwindcss-oxide.browser.wasm', import.meta.url))
1717
if (!response.ok) throw new Error(`Failed to load Oxide WebAssembly: ${response.status}`)
1818

19-
let { napiModule } = instantiateNapiModuleSync(await response.arrayBuffer(), {
19+
// Compile asynchronously, so loading doesn't block the page (Chrome also rejects synchronous
20+
// compiles of modules over 8 MB on the main thread).
21+
let { napiModule } = await instantiateNapiModule(response, {
2022
context: getDefaultContext(),
2123
wasi,
2224
asyncWorkPoolSize: 0,

0 commit comments

Comments
 (0)