MeshJS / mesh

An open-source library to advance Web3 development on Cardano

Home Page:https://meshjs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Load WASM libraries asynchronously

olliejm opened this issue · comments

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

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();