-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make wasm modules work when chunk deduplication is enabled * make wasm modules work with chunks deduplication is disabled
- Loading branch information
1 parent
3f05e81
commit c0ecec3
Showing
3 changed files
with
242 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.