Skip to content

Ship CommonJS build for librosa-wasm (0.1.3)#10

Merged
olilarkin merged 2 commits into
mainfrom
wasm-cjs-build
Jun 3, 2026
Merged

Ship CommonJS build for librosa-wasm (0.1.3)#10
olilarkin merged 2 commits into
mainfrom
wasm-cjs-build

Conversation

@olilarkin
Copy link
Copy Markdown
Owner

@olilarkin olilarkin commented Jun 3, 2026

Makes @olilarkin/librosa-wasm ship a CommonJS/Node build alongside the existing ESM one, so any require() consumer (Electron main, plain Node, Ableton Extension Host) gets a working module with no manual conversion.

Two commits: the CommonJS build, and a separate Emscripten version bump.

CommonJS build

  • scripts/make-cjs.mjs — scripted post-build step (pure JS, no Emscripten needed). It:
    • Converts the emscripten ESM glue → dist/wasm/librosa_wasm.cjs. The conversion handles both emscripten createRequire shim forms (static top-level import in 3.1.x, inline await import in 4.0.x), rewrites import.meta.url and export default, and then asserts no ESM construct survives — so a future emscripten upgrade fails the build loudly instead of producing a broken .cjs.
    • Emits dist/src/index.cjs by transforming the compiled ESM entry, so the arity table stays single-sourced from src/index.ts.
    • Generates dist/src/index.d.cts as a callable export = createLibrosa with a merged namespace re-exporting the function (and default) plus all type names (derived from types.d.ts). This matches the module.exports = createLibrosa runtime so import x = require(pkg) is callable under node16/nodenext.
  • scripts/smoke.cjsnode:test smoke test that require()s the built CJS entry, instantiates the wasm, and computes.
  • package.jsonimport/require conditions in exports (each with its own types); main.cjs, module.js; build:cjs/test:cjs wired into build/test. Version bumped to 0.1.3.
  • README.md — documents require() usage.

The existing CI build/test steps already run npm run build and npm test, both of which now include the CJS conversion + smoke test, so no extra publish-workflow wiring was needed.

Emscripten bump (separate commit)

  • Bumps all three workflows (ci, npm-publish, web-pages) from 3.1.614.0.22, the version the package + test suite were validated against locally. The conversion script is version-robust regardless, so this is a toolchain refresh rather than a fix.

Verification

  • 32 ESM tests + 2 CJS smoke tests pass; CI green on Emscripten 4.0.22.
  • Converted glue has zero ESM leftovers.
  • CJS and ESM consumers type-check under module: NodeNext (incl. import x = require(pkg) callable).
  • End-to-end require("@olilarkin/librosa-wasm") resolves through exports to the .cjs, loads the sibling wasm, and computes; ESM import path still works.
  • npm pack includes index.cjs, index.d.cts, librosa_wasm.cjs, and the .wasm.

Once published, the downstream extension can drop its vendored .cjs and consume the package's CJS entry directly.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e35c06e63c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/librosa-wasm/scripts/make-cjs.mjs Outdated
olilarkin added 2 commits June 3, 2026 08:04
`@olilarkin/librosa-wasm` now ships a CommonJS/Node build alongside the ESM one,
so any `require()` consumer (Electron main, plain Node, Ableton Extension Host)
gets a working module with no manual conversion.

- scripts/make-cjs.mjs: a pure-JS post-build step (no Emscripten needed) that
  converts the emscripten ESM glue to a `.cjs`, emits a CommonJS wrapper
  mirroring the compiled ESM entry, and generates a callable `export =` .d.cts
  (with the type list derived from types.d.ts). The ESM->CJS conversion handles
  both emscripten createRequire shim forms (static 3.1.x and inline 4.0.x) and
  asserts no ESM construct survives, failing loudly if the glue ever changes.
- scripts/smoke.cjs: a node:test smoke test that require()s the built CJS entry,
  instantiates the wasm, and computes.
- package.json: import/require exports conditions (each with its own types),
  main -> .cjs, module -> .js, build:cjs/test:cjs wired into build/test, version
  bumped to 0.1.3.
- README: documents require() usage.

Verified: 32 ESM tests + 2 CJS smoke tests pass; CJS and ESM consumers type-check
under NodeNext; `npm pack` includes the cjs/d.cts/wasm assets.
The 3.1.61 pin dated from the initial commit with no specific reason. Move all
three workflows (ci, npm-publish, web-pages) to 4.0.22, the version the WASM
package and its test suite were validated against locally.
@olilarkin olilarkin merged commit ce1db11 into main Jun 3, 2026
8 checks passed
@olilarkin olilarkin deleted the wasm-cjs-build branch June 3, 2026 06:14
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