Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript and Webpack 5 issues. #140

Open
CarterFendley opened this issue Sep 12, 2022 · 0 comments
Open

Typescript and Webpack 5 issues. #140

CarterFendley opened this issue Sep 12, 2022 · 0 comments

Comments

@CarterFendley
Copy link

In a normal javascript project, I am able to use Webpack 5 and the wasm-pack-plugin just fine. You can see this working in this Rust Webpack sandbox, just open a new terminal and type yarn up.

However in this Rust Webpack + Typescript sandbox (again use a new terminal to run yarn up as described in the README), I immediately get the following error with syncWebAssembly: true.

ERROR in ./pkg/index_bg.wasm
WebAssembly module is included in initial chunk.
This is not allowed, because WebAssembly download and compilation must happen asynchronous.
Add an async split point (i. e. import()) somewhere between your entrypoint and the WebAssembly module:
* ./src/index.ts --> ./pkg/index.js --> ./pkg/index_bg.wasm
* ... --> ./pkg/index.js --> ./pkg/index_bg.js --> ./pkg/index_bg.wasm
* ... --> ./pkg/index_bg.js --> ./pkg/index_bg.js --> ./pkg/index_bg.wasm
* ... --> ./src/index.ts --> ./src/index.ts --> ./pkg/index.js --> ./pkg/index_bg.wasm
* ... --> ./pkg/index.js --> ./pkg/index_bg.wasm --> ./pkg/index_bg.js --> ./pkg/index_bg.wasm
* ... --> ./pkg/index_bg.js --> ./pkg/index_bg.wasm --> ./pkg/index_bg.js --> ./pkg/index_bg.wasm
 @ ./pkg/index.js 1:0-40
 @ ./src/index.ts 25:55-78

This is odd to me because I use import("../pkg/index") so not sure why this error would be thrown.

When I update to use asyncWebAssembly: true instead, it compiles however the following does not display anything to the screen. Locally, there are a bunch of errors in the console about hello() being undefined.

const rust = import("../pkg/index");

rust.then((m) => {
  console.log(m);
  document.body.innerHTML = `
  <h1>Message from Rust:</h1>
    <h2>${m.hello()}</h2>
  `;
});

When checking the console, we can see that m is a promise not the rust module. (Apologies for the webpack errors I could not clear)

Screen Shot 2022-09-11 at 8 33 27 PM

Taking this into account I can get this working by using the normal ES6 import and just assuming it is a promise. However as you can shown VSCode sees this as an error.
Screen Shot 2022-09-11 at 8 40 34 PM

Property 'then' does not exist on type 'typeof import("blah/blah/blah")' .ts(2339)

This looks different than the esm-integration proposal which I gather is what asyncWebAssembly is based on. To the best of my googling ability, I have not been able to find anything that looks similar.

Questions

  • Why does this only happen with typescript?
  • Does wasm-pack-plugin need to be updated for asyncWebAssembly?
  • Should I take this up with webpack?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant