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

Synchronously importing wasm modules in the main chunk #6615

Closed
alexcrichton opened this issue Feb 28, 2018 · 14 comments
Closed

Synchronously importing wasm modules in the main chunk #6615

alexcrichton opened this issue Feb 28, 2018 · 14 comments

Comments

@alexcrichton
Copy link

Do you want to request a feature or report a bug?

bug

What is the current behavior?

Currently when a wasm module is imported like:

import { foo } from './foo.wasm';
// ...

then wepack will report "Sync WebAssembly compilation is not yet implemented"

If the current behavior is a bug, please provide the steps to reproduce.

$ cat foo.wat
(module
  (type (;0;) (func))
  (func (;0;) (type 0)
    nop)
  (memory (;0;) 17)
  (export "memory" (memory 0))
  (export "foo" (func 0)))
$ wat2wasm foo.wat -o foo.wasm
$ cat index.js
import { foo } from './foo.wasm';
$ yarn run webpack index.js
yarn run v1.3.2
warning package.json: No license field
$ /home/alex/code/wasm-bindgen/js-hello-world/node_modules/.bin/webpack index.js
Hash: 64205dfbff78f2176ada
Version: webpack 4.0.1
Time: 64ms
Built at: 2018-2-27 19:15:17
Entrypoint main =
   [0] ./foo.wasm 17.5 KiB {0} [built]
   [1] ./index.js 34 bytes {0} [built]

WARNING in configuration
The 'mode' option has not been set. Set 'mode' option to 'development' or 'production' to enable defaults for this environment. 

ERROR in chunk main [entry]
[name].js
Sync WebAssembly compilation is not yet implemented
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

What is the expected behavior?

Both import('./foo.wasm') and import { foo } from './foo.wasm'; to work. It looks like this is intentionally unimplemented though, so I was wondering if it'd be good to have a tracking issue for this feature? Or maybe such an issue already exists!

If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.

@sendilkumarn
Copy link
Member

Yes, it is not implemented currently. But it is very much in the cards IIUC

@alexcrichton alexcrichton changed the title Synchronously importing wasm modules Synchronously importing wasm modules in the main chunk Mar 3, 2018
@alexcrichton
Copy link
Author

I've updated the issue title to specifically refer to the main check as @sendilkumarn points out that synchronous imports are supported in non-main chunks!

@camerondavison
Copy link

is this fixed, i was able to build a dist/main.js following these steps.

@jjpe
Copy link

jjpe commented Dec 6, 2018

I just bumped into this.
It'd be absolutely terrific if this issue was given some consideration and especially some priority, because getting a WASM module to work in a larger system has proven... incredibly frustrating thusfar, and need for async imports bears a significant chunk of the blame for that.

@alexander-akait
Copy link
Member

alexander-akait commented Dec 6, 2018

@jjpe A lot of issue and plans, sorry, feel free to send a PR

@xtuc
Copy link
Member

xtuc commented Dec 7, 2018

The priority should be on the WebAssembly<>JavaScript Module integration specificiation. In the initial chunk, synchronously loading the wasm module is complicated and not considered at the moment.

Note that if you dynamicaly load a chunk, you can then use synchronous imports.

@Niedzwiedzw

This comment has been minimized.

@roccomuso

This comment has been minimized.

@afrancht
Copy link

So has it been fixed? How do we fix it?

@alexander-akait
Copy link
Member

/cc @sendilkumarn

@clearloop
Copy link

wasm-simple seems solved this problem, but it doesn't work for me even I copied the template code entirely.

Just saw " prefetching doesn't work for WASM yet" in #9802 as a known problem, will webpack-5 solve this problem?

I'm trying to write a wasm based web-framework currently, and expecting this feature deadly 🦀

@sokra
Copy link
Member

sokra commented Jan 20, 2020

webpack 5 follows the updated wasm-esm-integration spec (experiments.asyncWebAssembly: true) which allows wasm in entry chunk too. It doesn't put any restriction on wasm location. But wasm modules are async modules which behave similar to modules which use top-level-await (The difference isn't noticeable for most people.)

@alixander
Copy link

alixander commented Aug 3, 2020

In case anyone finds this in the future and doesn't want to upgrade to Webpack 5, the solution I used is to just define an entry point that asynchronously loads the actual entry point. This makes the initial chunk ~empty and introduces another roundtrip for the initial page load, but if you're using webassembly in the initial page load anyway, there's no difference between doing this and asynchronously loading the wasm file from a main chunk.

Webpack configuration of entry point: https://github.com/MaxBittker/sandspiel/blob/master/webpack.config.js#L11
Example of entry point: https://github.com/MaxBittker/sandspiel/blob/master/js/bootstrap.js
Which allows synchronous-style wasm importing: https://github.com/MaxBittker/sandspiel/blob/master/js/index.js#L2

@dannywillems
Copy link

In case anyone finds this in the future and doesn't want to upgrade to Webpack 5, the solution I used is to just define an entry point that asynchronously loads the actual entry point. This makes the initial chunk ~empty and introduces another roundtrip for the initial page load, but if you're using webassembly in the initial page load anyway, there's no difference between doing this and asynchronously loading the wasm file from a main chunk.

Webpack entry point: https://github.com/MaxBittker/sandspiel/blob/master/webpack.config.js#L11
Example of entry point: https://github.com/MaxBittker/sandspiel/blob/master/js/bootstrap.js
Which allows synchronous-style wasm importing: https://github.com/MaxBittker/sandspiel/blob/master/js/index.js#L2

I do confirm it does the job for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests