Skip to content

feat(sdk-ts): migrate build to Vite with dual ESM+CJS output#4

Merged
tupe12334 merged 2 commits into
mainfrom
feat/esm-vite-build
Jun 2, 2026
Merged

feat(sdk-ts): migrate build to Vite with dual ESM+CJS output#4
tupe12334 merged 2 commits into
mainfrom
feat/esm-vite-build

Conversation

@tupe12334
Copy link
Copy Markdown
Owner

Closes #3.

Summary

  • Replaces tsc build with vite build (library mode), emitting dist/index.js (ESM) and dist/index.cjs (CJS) from a single source
  • Updates package.json exports to include both "import" and "require" conditions — ESM consumers no longer need the createRequire workaround
  • Uses a variable ref for the WASM path (const _wasmRel = '../polyhook.wasm') to prevent Vite from inlining the binary as a base64 data URL
  • Adds fixCjsImportMetaUrl Rolldown plugin: Vite 8's Rolldown emits {}.url for import.meta.url in CJS chunks; the plugin patches it to require('url').pathToFileURL(__filename).href
  • Switches tsconfig module to ESNext + moduleResolution: bundler for Vite compatibility; adds typecheck script

Test plan

  • pnpm build in packages/sdk-ts — verify dist/index.js (~1.8 kB) and dist/index.cjs (~2 kB) exist with no WASM inlining
  • pnpm test — all 19 tests pass
  • ESM consumer: import { read } from '@polyhook/sdk' works without createRequire workaround
  • CJS consumer: require('@polyhook/sdk') continues to work

🤖 Generated with Claude Code

tupe12334 and others added 2 commits June 2, 2026 21:19
Fixes #3. Replace tsc with vite build (library mode) to emit both
dist/index.js (ESM) and dist/index.cjs (CJS) from a single source.

- Use import.meta.url + variable ref to avoid Vite inlining polyhook.wasm
- Add fixCjsImportMetaUrl plugin: Rolldown (Vite 8) emits {}.url for
  import.meta.url in CJS chunks; patch it to require('url').pathToFileURL(__filename).href
- Update package.json exports with import/require conditions and module field
- Switch tsconfig to ESNext/bundler for Vite compatibility

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Add ESM/CJS note, CJS usage example, and API reference table.
Update gen-readmes.sh as the source of truth.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tupe12334 tupe12334 merged commit d5f7dd3 into main Jun 2, 2026
6 of 7 checks passed
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.

feat: add ESM export condition to @polyhook/sdk

1 participant