Skip to content

Commit

Permalink
Add wasm support (#256)
Browse files Browse the repository at this point in the history
* make wasm modules work when chunk deduplication is enabled
* make wasm modules work with chunks deduplication is disabled
  • Loading branch information
dario-piotrowicz authored May 19, 2023
1 parent 3f05e81 commit c0ecec3
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 19 deletions.
49 changes: 49 additions & 0 deletions .changeset/flat-bobcats-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
'@cloudflare/next-on-pages': patch
---

introduce wasm support

introduce wasm support by tweaking how the wasm modules are imported, what `vercel build` does is adding dynamic
requires at the top of the func files, like for example:

```js
// file: .vercel/output/functions/index.func/index.js
const wasm_fbeb8adedbc833032bda6f13925ba235b8d09114 = require('/wasm/wasm_fbeb8adedbc833032bda6f13925ba235b8d09114.wasm');
```

then such identifier is used in the rest of the file (likely only inside chunks), as in:

```js
// file: .vercel/output/functions/index.func/index.js
649:e=>{e.exports=wasm_fbeb8adedbc833032bda6f13925ba235b8d09114}
```

the above can't work with next-on-pages because:

- dynamic requires are not supported
- when we perform the chunks deduplication chunks containing such identifiers will not find their declaration causing
(e.g. a chunk file containing the `649` chunk code illustrated above won't know where `wasm_fbeb8adedbc833032bda6f13925ba235b8d09114`
comes from and would just provide a runtime error saying that it is not defined)
- `/wasm/...` isn't a real directory, just some sort of convention used by vercel, the wasm files are located in the same
directory as the func file

the adopted solution consists in:

- copying the wasm files from their func relative locations into the `__next-on-pages-dist/wasm` directory
- converting the func top level requires into standard relative esm imports, like for example:
```js
// file: .vercel/output/functions/index.func/index.js
import wasm_fbeb8adedbc833032bda6f13925ba235b8d09114 from '../wasm/wasm_fbeb8adedbc833032bda6f13925ba235b8d09114.wasm';
```
so that any part of the func file will be able to reference the variable (so that this works with chunks deduplication disabled)
- adding similar import statements to any chunk files that reference these wasm identifiers, like for example:
```js
// file: .vercel/output/static/_worker.js/__next-on-pages-dist__/chunks/649.js
import wasm_fbeb8adedbc833032bda6f13925ba235b8d09114 from '../wasm/wasm_fbeb8adedbc833032bda6f13925ba235b8d09114.wasm';
var a = b => {
b.exports = wasm_fbeb8adedbc833032bda6f13925ba235b8d09114;
};
export { a as default };
```
(so that this works with chunks deduplication enabled)
Loading

0 comments on commit c0ecec3

Please sign in to comment.