Visitar URL original
@swc/core fails to load on wasm32 runtimes: `@swc/core-wasm32-wasi` is referenced by the loader but never published · Issue #12466 · swc-project/swc · GitHub
Skip to content

@swc/core fails to load on wasm32 runtimes: @swc/core-wasm32-wasi is referenced by the loader but never published #12466

Open

Description

Describe the feature

The napi-rs loader in packages/core/binding.js tries require('@swc/core-wasm32-wasi') as its last fallback, but that package has never been built or published. On any wasm32 runtime, require('@swc/core') therefore fails with Failed to load native binding before the @swc/wasm fallback in index.js gets a chance to run.

Why it matters

In-browser Node runtimes such as WebContainers and Strapkit run everything on wasm32-wasi, so there is no native binary to load. Anything that pulls in @swc/core is currently broken in those environments, including:

  • @swc/cli builds (swc src -d dist)
  • @swc/jest
  • webpack's swc-loader
  • @vitejs/plugin-react-swc

Each runtime ends up carrying its own workaround. rolldown, oxc and rspack all publish a wasi binding through the same napi-rs pipeline, so swc is the odd one out here.

To reproduce

  1. Open a project that depends on @swc/core in a wasm32-wasi Node runtime (e.g. WebContainers).
  2. Run node -e "require('@swc/core')".
  3. It throws Failed to load native binding.

Expected behavior

@swc/core loads on wasm32 runtimes via a published @swc/core-wasm32-wasi package, as binding.js already expects.

Proposed fix

Build and publish the wasi binding through the existing napi-rs pipeline:

  • Add wasm32-wasip1-threads to napi.targets in packages/core/package.json, so napi artifacts picks up the wasi files and napi prepublish adds @swc/core-wasm32-wasi to optionalDependencies. cpu: ["wasm32"] keeps it off native installs.
  • Add packages/core/scripts/npm/wasm32-wasi/, mirroring the other per-target npm dirs.
  • Add a build matrix entry to publish-npm-package.yml. It only needs to build for @swc/core (the other packages skip it and the artifact upload tolerates that), and the upload glob needs to also grab the napi-generated wasi-worker* files.

The swc CLI binary can be skipped for this target; a wasi CLI build is a separate discussion.

Additional context

I have a working branch with these changes: #12064. But It's probably easier for the maintainers to just add it themselves.

Babel plugin or link to the feature description

No response

Additional context

No response

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions