Skip to content

fix(pow): mark Node-only imports so browser bundlers skip them - #35

Open
0x3639 wants to merge 1 commit into
digitalSloth:feature/browser-bundlesfrom
0x3639:feat/tree-shakeable-browser
Open

fix(pow): mark Node-only imports so browser bundlers skip them#35
0x3639 wants to merge 1 commit into
digitalSloth:feature/browser-bundlesfrom
0x3639:feat/tree-shakeable-browser

Conversation

@0x3639

@0x3639 0x3639 commented Sep 7, 2026

Copy link
Copy Markdown

Summary

Follow-up to #34 (targets feature/browser-bundles, one commit). Prefixes the three Node-only dynamic imports in src/pow/pow.ts with node: and adds /* @vite-ignore */ alongside the existing /* webpackIgnore: true */:

const { readFileSync } = await import(/* webpackIgnore: true */ /* @vite-ignore */ "node:fs");
const { fileURLToPath } = await import(/* webpackIgnore: true */ /* @vite-ignore */ "node:url");
const { dirname, join } = await import(/* webpackIgnore: true */ /* @vite-ignore */ "node:path");

Why

With the modular build now resolved directly by browser bundlers, Vite/Rolldown still tries to resolve the bare fs/url/path specifiers even though they sit under isNode():

[plugin rolldown:vite-resolve] Module "path" has been externalized for browser compatibility, imported by ".../znn-typescript-sdk/dist/pow/pow.js"
[plugin rolldown:vite-resolve] Module "fs" has been externalized for browser compatibility, imported by ".../znn-typescript-sdk/dist/pow/pow.js"

and, when the consumer has a Node polyfill plugin (common for Buffer), it emits a never-fetched url polyfill chunk (~51 KB minified) referenced from the SDK chunk. The node: prefix plus @vite-ignore makes every bundler leave the specifiers alone.

Verification

  • zwap.fun (Vite 8.1.4 / Rolldown, production): both warnings gone, url chunk gone, 271 → 223 modules transformed, no __vite-browser-external stubs emitted. Tree-shaking results from feat: make the browser build tree-shakeable, drop UMD #34 unchanged (no argon2 / KeyFile / KeyStore / eval in output).
  • esbuild 0.28 --bundle --platform=browser on dist/index.js: clean exit.
  • Node 22.12 from the packed tarball: PoW init() + generate() succeed through the node: imports; KeyStore / KeyFile round trip unaffected.
  • npm test: 650 passing. npm run lint / npm run build clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NoKrSzV9FwJHGutEwsATAY

Prefix the fs/url/path dynamic imports in pow.ts with `node:` and add
`/* @vite-ignore */` next to the existing `webpackIgnore`. Vite/Rolldown
otherwise resolves the three specifiers, emits "externalized for browser
compatibility" warnings for fs and path, and (with a polyfill plugin
present) ships a never-fetched ~51 KB `url` polyfill chunk. Behaviour
under isNode() is unchanged: verified PoW init + generate from the packed
tarball on Node 22.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NoKrSzV9FwJHGutEwsATAY
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.

1 participant