-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
WASM bundle loader #473
WASM bundle loader #473
Conversation
Register custom loaders with `bundler.addBundleLoader`
Enables `bundler.getAsset` method to resolve and process an asset at any time, outside of the normal asset tree construction.
This gives us a separate bundle for each output file.
Allows synchronous import to preload modules in external files, e.g. .wasm file, prior to execution of the JS bundle. Also processes the HMR runtime like other assets.
No longer returns a URL to the JS bundle if there is a bundle loader defined. This will cause the asset to be preloaded prior to JS bundle execution.
Hi @devongovett great job! I have been testing it out but I get this error: and I found that compileStreaming checks the MIME type, so it's a matter of adding the MIME type to the response. module.exports = function loadWASMBundle(bundle) {
return fetch(bundle)
.then(response => response.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes, {}))
.then(results => results.instance.exports)
}; and it's works on chrome, firefox and safari! |
Thanks for testing! Ok, I guess we need to use something like https://www.npmjs.com/package/mime to serve correct mime types in the dev server. |
Until pillarjs/send#154 is merged.
@albizures added the correct mime type for wasm, and it seems to be working for me now in chrome. |
One current issue is with HMR - the port is now injected by an environment variable since the HMR runtime is a normal asset now and not injected by the packager. The problem is that env vars don't currently invalidate the cache when they change, and by default the HMR port changes on every run. Going to fix this as part of #297 hopefully. |
Should fix error “WebAssembly.Instance is disallowed on the main thread, if the buffer size is larger than 4KB”
@albizures interesting. I've changed it to use |
it works perfectly! ✨ |
Awesome, going to merge this. 💥 |
This refactors the way async bundle loaders work so that custom formats can be registered. Previously, there were only CSS and JS bundle loaders for dynamic
import()
s hard coded. Now you can call e.g.bundler.addBundleLoader('ext', '/path/to/my/loader.js')
to add a bundle loader for some custom type. These will automatically be included in the bundle by the JSPackager if that type is dynamically imported.This also makes it possible to have other bundles be preloaded prior to a JS bundle being executed. This is for the case of e.g.
wasm
where we don't want to inline the binary into the JS, but the import was synchronous: e.g.import {add} from './add.wasm'
. In this case, the JSPackager notices that the module is in an external file, and adds some code to preload it prior to the JS being run. This is only done for root JS bundles, because for async/dynamic imports the wasm file would be loaded in parallel with the JS already.This is a somewhat large change, and I ended up refactoring a bunch of other stuff to make it work. Please test out this branch and report any bugs you find! Thanks! 😍