Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for importing npm-provided wasm packages in client/server #532

Open
bcjordan opened this issue Oct 28, 2023 · 1 comment
Open

Support for importing npm-provided wasm packages in client/server #532

bcjordan opened this issue Oct 28, 2023 · 1 comment

Comments

@bcjordan
Copy link

Ahoy! I'm looking to import rapier.js (a Rust physics engine that can use JS) in both the client/server but running in to issues I think with both.

The sample code tried on client.js:

import('@dimforge/rapier2d').then(RAPIER => {
  // Use the RAPIER module here.
  let gravity = { x: 0.0, y: -9.81 };
  let world = new RAPIER.World(gravity);

  // Create the ground
  let groundColliderDesc = RAPIER.ColliderDesc.cuboid(10.0, 0.1);
  world.createCollider(groundColliderDesc);

  // Create a dynamic rigid-body.
  let rigidBodyDesc = RAPIER.RigidBodyDesc.dynamic()
    .setTranslation(0.0, 1.0);
  let rigidBody = world.createRigidBody(rigidBodyDesc);

  // Create a cuboid collider attached to the dynamic rigidBody.
  let colliderDesc = RAPIER.ColliderDesc.cuboid(0.5, 0.5);
  let collider = world.createCollider(colliderDesc, rigidBody);

  // Game loop. Replace by your own game loop system.
  let gameLoop = () => {
    // Ste the simulation forward.
    world.step();

    // Get and print the rigid-body's position.
    let position = rigidBody.translation();
    console.log("Rigid-body position: ", position.x, position.y);

    setTimeout(gameLoop, 16);
  };

  gameLoop();
})

The client.js failed w/:

text: 'No loader is configured for ".wasm" files: node_modules/.pnpm/@dimforge+rapier2d@0.11.2/node_modules/@dimforge/rapier2d/rapier_wasm2d_bg.wasm'

(I might be able to figure it out if I eject from the partykit client build? but I really like the simple build experience atm!)

The server.js while running pnpm run dev is able to complete the import, but within the local dev server it seems like execution is seemingly stopping on the World create JS step, but not sure if there's any way to capture a stack trace / error when a pre-built wasm just halts execution like that 🤔

@threepointone
Copy link
Contributor

hrmm I think I see the problem here, browsers load wasm files differently, they're meant to be considered actual files that get loaded and parsed, but you'd like to use the same code in both places. Lemme spend some time later and show how you could possibly do this with rapier

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

No branches or pull requests

2 participants