Skip to content

Commit c0ecec3

Browse files
Add wasm support (#256)
* make wasm modules work when chunk deduplication is enabled * make wasm modules work with chunks deduplication is disabled
1 parent 3f05e81 commit c0ecec3

File tree

3 files changed

+242
-19
lines changed

3 files changed

+242
-19
lines changed

.changeset/flat-bobcats-glow.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
'@cloudflare/next-on-pages': patch
3+
---
4+
5+
introduce wasm support
6+
7+
introduce wasm support by tweaking how the wasm modules are imported, what `vercel build` does is adding dynamic
8+
requires at the top of the func files, like for example:
9+
10+
```js
11+
// file: .vercel/output/functions/index.func/index.js
12+
const wasm_fbeb8adedbc833032bda6f13925ba235b8d09114 = require('/wasm/wasm_fbeb8adedbc833032bda6f13925ba235b8d09114.wasm');
13+
```
14+
15+
then such identifier is used in the rest of the file (likely only inside chunks), as in:
16+
17+
```js
18+
// file: .vercel/output/functions/index.func/index.js
19+
649:e=>{e.exports=wasm_fbeb8adedbc833032bda6f13925ba235b8d09114}
20+
```
21+
22+
the above can't work with next-on-pages because:
23+
24+
- dynamic requires are not supported
25+
- when we perform the chunks deduplication chunks containing such identifiers will not find their declaration causing
26+
(e.g. a chunk file containing the `649` chunk code illustrated above won't know where `wasm_fbeb8adedbc833032bda6f13925ba235b8d09114`
27+
comes from and would just provide a runtime error saying that it is not defined)
28+
- `/wasm/...` isn't a real directory, just some sort of convention used by vercel, the wasm files are located in the same
29+
directory as the func file
30+
31+
the adopted solution consists in:
32+
33+
- copying the wasm files from their func relative locations into the `__next-on-pages-dist/wasm` directory
34+
- converting the func top level requires into standard relative esm imports, like for example:
35+
```js
36+
// file: .vercel/output/functions/index.func/index.js
37+
import wasm_fbeb8adedbc833032bda6f13925ba235b8d09114 from '../wasm/wasm_fbeb8adedbc833032bda6f13925ba235b8d09114.wasm';
38+
```
39+
so that any part of the func file will be able to reference the variable (so that this works with chunks deduplication disabled)
40+
- adding similar import statements to any chunk files that reference these wasm identifiers, like for example:
41+
```js
42+
// file: .vercel/output/static/_worker.js/__next-on-pages-dist__/chunks/649.js
43+
import wasm_fbeb8adedbc833032bda6f13925ba235b8d09114 from '../wasm/wasm_fbeb8adedbc833032bda6f13925ba235b8d09114.wasm';
44+
var a = b => {
45+
b.exports = wasm_fbeb8adedbc833032bda6f13925ba235b8d09114;
46+
};
47+
export { a as default };
48+
```
49+
(so that this works with chunks deduplication enabled)

0 commit comments

Comments
 (0)