Dezinater / osrscachereader

A Javascript library that allows you to read Old-School Runescape game files
https://dezinater.github.io/osrscachereader/
BSD 2-Clause "Simplified" License
6 stars 2 forks source link

ESBuild/Vite unsupported due to top-level await requirement #4

Closed ESchouten closed 8 months ago

ESchouten commented 8 months ago

Seems like bundlers using ESBuild like Vite are not able to use this library since ESBuild does not support top-level awaits which the used bzip2 library @foxglove/wasm-bz2 is using. Any solution to this? Or do we have to swap out the bzip2 library?

Error:

[ERROR] This require call is not allowed because the imported file "vite-plugin-wasm-namespace:.../node_modules/.pnpm/@foxglove+wasm-bz2@0.1.1/node_modules/@foxglove/wasm-bz2/wasm/module.wasm" contains a top-level await

    node_modules/.pnpm/@foxglove+wasm-bz2@0.1.1/node_modules/@foxglove/wasm-bz2/wasm/module.js:52:29:
      52 │     const wasmPath = require("./module.wasm");
         ╵                              ~~~~~~~~~~~~~~~

  The top-level await in
  "vite-plugin-wasm-namespace:.../node_modules/.pnpm/@foxglove+wasm-bz2@0.1.1/node_modules/@foxglove/wasm-bz2/wasm/module.wasm"
  is here:

    vite-plugin-wasm-namespace:.../node_modules/.pnpm/@foxglove+wasm-bz2@0.1.1/node_modules/@foxglove/wasm-bz2/wasm/module.wasm:45:27:
      45 │ const __vite__wasmModule = await initWasm({ "env": { _emval_new_object: __vite__wasmImport_0_0, _emval_decref: __vite__wasmImport_0_1, _emval_new_cstring: __vite__wasmIm...
         ╵                            ~~~~~
Dezinater commented 8 months ago

Sorry but I don't really know alot about Vite/ESBuild so it might be hard for me to help. I've asked in the foxglove slack about this problem and they said "Not familiar either but I don’t think this issue is specific to the foxglove library because it claims there is a top level await in what appears to be code generated by vite itself (not the original .wasm file in the library)"

I've only been testing this library with webpack and it doesn't take too much to get it working there so I'm thinking there's probably a simple fix to this

Would this project maybe help/fix the problem? https://www.npmjs.com/package/vite-plugin-wasm

Is there a project you have that I can clone to mess around with and see if I can fix the issue?

ESchouten commented 8 months ago

Will create a project for you soon. Currently I have swapped out wasm-bz2 for compressjs

decompressedData = compressjs.Bzip2.decompressFile(bzData);
Dezinater commented 8 months ago

I was actually using a different bz2 library before this one but it had a small problem. Basically 1 bit would be off after decompressing and it would only happen with some cache versions. I have no idea what causes it but all those issues went away when I switch to this current bz2 library. I'm guessing there's probably some weird JS nuance going on that caused it and since this library is a wasm compilation of the official bz2 implementation it should always work.

If the one that you linked does work with multiple different caches I'd be fine switching over to it.

ESchouten commented 8 months ago

I ran the Load NPC tests which loads the cache, all bz2 compressed idx files seemed to return the same UInt8Array with wasm-bz2 as with compressjs. Have only tried one OSRS cache version though. Will open a PR.

ESchouten commented 8 months ago

Demo project here: https://github.com/ESchouten/svelte-osrscachereader Pinned osrscachereader>web-worker dependency to 1.2.0, 1.3.0 causes some import error. Added vite-plugin-wasm and vite-plugin-top-level-await, excluded env and wasi_snapshot_preview1 from dependency optimisation which made them disappear.