feat(wasm): allow using a custom loader
Rollup Plugin Name: rollup-wasm
This PR contains:
- [ ] bugfix
- [x] feature
- [ ] refactor
- [x] documentation
- [ ] other
Are tests included?
- [x] yes
- [ ] no
Breaking Changes?
- [ ] yes
- [x] no
Description
This deprecates the targetEnv option and replaces it with loader which performs the same thing while also accepting an actual function.
When that happens, that function's body will be inserted in lieu of the default one, giving complete control over the way WASM is loaded into the runtime.
I need it because I'm trying to load code in a sandboxed Node 10 environment, which doesn't have process nor Buffer available, and that felt like the cleanest way to do that without giving you a bunch of hardcoded code that's meaningless outside of it.
My initial attempt at this was to give a function name from the bundle instead of something that would only live in the config. But I couldn't find a way to get the mangling done by Rollup to apply to it from inside the plugin; that's why I settled on having a real function.
In other words, if I had set loader: 'myCustomLoader', that would translate to something like this in the final bundle:
function wasm (imports) { return myCustomLoader(0, null, 'AGFzbQEAAAABBwFgAn9/AX8DAgEABQMBAAAHEAIDYWRkAAAGbWVtb3J5AgAKCQEHACAAIAFqCwAkEHNvdXJjZU1hcHBpbmdVUkwSLi9yZWxlYXNlLndhc20ubWFw', imports) }
/* somewhere else */
function myCustomLoader$1(sync, path, src, imports) { /* … */ }
and that $1 prefix would have been a problem.
Thanks for the PR. I would love to move this forward, but I just don't know enough about WASM to approve or not. Please tag some of the past committers to this plugin to see if they can offer some feedback.
Okay, rebased because someone else expressed interest (this is for the Screeps MMO game BTW), so I'm gonna tag people I find in the history that touched the wasm package in a meaningful way not too long ago.
@sky0014 @nickbabcock @featherbear @bminixhofer Would one of you have feedback on the changes I made?
Yeah, give me a couple days to try this out. Thanks for the ping.
Since I am not a wasm developer and @nickbabcock is the only active reviewer in our repository so far, I'm going to go off of his signal on whether or not to merge this.
Yeah I'd wait to merge this until either the caveats are addressed or documented as there's a few rough edges for users to be aware of that aren't intuitive.