Closed 8Observer8 closed 1 year ago
As far as I can tell playcode appears to be running some form of in-process bundler that seems to be paving over a module resolution issue in jsDeliver's magic /+esm
transform.
In the box2d-wasm
's wasm loader, you can see the code:
new URL("Box2D.simd.wasm",import.meta.url)
Knowing that code is being loaded from https://cdn.jsdelivr.net/npm/box2d-wasm@7.0.0/dist/es/Box2D.simd.js/+esm, we can test it out:
new URL("Box2D.simd.wasm","https://cdn.jsdelivr.net/npm/box2d-wasm@7.0.0/dist/es/Box2D.simd.js/+esm").toString()
'https://cdn.jsdelivr.net/npm/box2d-wasm@7.0.0/dist/es/Box2D.simd.js/Box2D.simd.wasm'
So I would say that while it is unfortunate that this doesn't work natively on Plunker, the bug is either in the jsDeliver ESM transform or the actual box2d distribution. PlayCode has done some clever work with their in-browser bundling solution to pave this over.
Thanks a lot for the detailed answer. The solution is to upload the "box2d-wasm" files to the hosting. For example on GitHub Pages:
In my case I have a link to box2d-wasm.min.js
that I can use with import map:
<!-- Since importmap is not yet supported by all browsers, it is
necessary to add the polyfill es-module-shims.min.js -->
<script async src="https://unpkg.com/es-module-shims@0.1.7/dist/es-module-shims.min.js"></script>
<script type="importmap">
{
"imports": {
"box2d-wasm": "https://8observer8.github.io/lib/box2d-wasm-7.0.0-box2d-2.4.1/box2d-wasm.min.js",
"pixi.js": "https://cdn.jsdelivr.net/npm/pixi.js@7.2.4/+esm"
}
}
</script>
<script type="module" src="./js/index.js"></script>
For example, using box2d-wasm with Pixi.js with drawing colliders and ray casting:
UNPKG version works without the problem:
Playground: https://plnkr.co/edit/BGNYcIJRiJXpd9N4?preview
<!-- Since import maps are not yet supported by all browsers, its is
necessary to add the polyfill es-module-shims.js -->
<script async src="https://unpkg.com/es-module-shims@1.8.3/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"box2d-wasm": "https://unpkg.com/box2d-wasm@7.0.0/dist/es/Box2D.js",
"gl-matrix": "https://cdn.jsdelivr.net/npm/gl-matrix@3.4.3/+esm"
}
}
</script>
<script type="module">
import { mat4, vec3 } from "gl-matrix";
const v = vec3.fromValues(1, 2, 3);
console.log(v);
const m = mat4.create();
console.log(m);
</script>
<!-- <script type="module" src="./js/index.js"></script> -->
This example works on PlayCode: https://playcode.io/1512368 but it doesn't work on Plunker: https://plnkr.co/edit/YQTyTFtudEFTq9tj
index.html
js/index.js
js/init-box2d.js
You can see these messages in the browser console: