Skip to content

Auto-build WASM packages on npm install via prepare hook#304

Merged
dmarcos merged 1 commit intosparkjsdev:mainfrom
secretrobotron:fix/auto-build-wasm-on-install
Apr 21, 2026
Merged

Auto-build WASM packages on npm install via prepare hook#304
dmarcos merged 1 commit intosparkjsdev:mainfrom
secretrobotron:fix/auto-build-wasm-on-install

Conversation

@secretrobotron
Copy link
Copy Markdown
Contributor

After cloning the repo and running npm install, the npm run build command fails with this error:

[vite]: Rollup failed to resolve import "spark-rs" from "src/SplatPager.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to build.rollupOptions.external.

(fresh clone on osx with cargo 1.94.1 and npm 11.8.0)

The error message suggests a Vite/Rollup config issue, but the real problem is that the spark-rs and spark-worker-rs WASM packages haven't been built yet. Their pkg/ directories are gitignored (correctly), but nothing in the standard setup flow builds them automatically. You have to know to run npm run build:wasm first, which is only mentioned in the README's build instructions in passing.

Fix

This PR sets the prepare script in package.json to npm run build:wasm. The prepare hook runs automatically after npm install, so the WASM packages are built before anything else needs them. This means npm install && npm run build just works out of the box for new contributors.

The WASM build is only triggered by npm install — not on every npm run build — so there's no performance cost during normal development iteration.

Verification

  • Delete rust/spark-rs/pkg and rust/spark-worker-rs/pkg
  • Run npm install — verify WASM packages are built automatically
  • Run npm run build — verify it completes successfully

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@dmarcos
Copy link
Copy Markdown
Contributor

dmarcos commented Apr 21, 2026

Thanks!

@dmarcos dmarcos merged commit 915c474 into sparkjsdev:main Apr 21, 2026
2 checks passed
@mrxz
Copy link
Copy Markdown
Collaborator

mrxz commented Apr 21, 2026

While the change itself is okay, it's worth noting that this prepare script used to be there before. It was removed deliberately in commit 2fbde38. We should avoid changing this back and forth repeatedly.

AFAIUI there are two setups Spark is trying to accommodate: building/developing the project itself and using a specific commit of Spark as dependency in a project. The former requires the user to build the wasm projects. In this case having it part of the prepare script is beneficial. The latter is meant to use the pre-built bundles in the dist/ folder, but many package managers execute the prepare script regardless (see #195).

In practice people that wanted to test the latest commits ran into issues because they didn't have a Rust toolchain setup. Arguably this negates the convenience of providing pre-built bundles.

@secretrobotron
Copy link
Copy Markdown
Contributor Author

@mrxz that totally makes sense. In that case, we should just revert, and update the README, no?

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.

3 participants