MeshJS / mesh

An open-source library to advance Web3 development on Cardano
https://meshjs.dev
Apache License 2.0
192 stars 55 forks source link

Load WASM libraries asynchronously #197

Open olliejm opened 5 months ago

olliejm commented 5 months ago

Should the wasm libraries like https://github.com/MeshJS/mesh/blob/main/packages/module/src/core/CSL.ts#L3 not be loaded asynchronously ideally with await import? Since I added Mesh to a Next.js project the build time and first load times increased dramatically. I had this same problem when I was directly using the Emurgo WASM packages in another project, I instead had to create a Loader class which lazy loads the libraries on demand at first use, and that significantly improved the app's build and initial response times

olliejm commented 5 months ago

The loader class just looked like this:

class WasmLoader {
  // eslint-disable-next-line @typescript-eslint/consistent-type-imports
  private csl: typeof import('@emurgo/cardano-serialization-lib-browser') | undefined;

  async load() {
    if (this.csl) {
      return;
    }

    this.csl = await import('@emurgo/cardano-serialization-lib-browser');
  }

  get CSL() {
    return this.csl;
  }
}

export const loader = new WasmLoader();