plugins icon indicating copy to clipboard operation
plugins copied to clipboard

feat(wasm): allow using a custom loader

Open tiennou opened this issue 1 year ago • 6 comments

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.

tiennou avatar Feb 20 '24 22:02 tiennou

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.

tiennou avatar Feb 20 '24 22:02 tiennou

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.

shellscape avatar Jun 05 '24 01:06 shellscape

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?

tiennou avatar Jul 27 '24 06:07 tiennou

Yeah, give me a couple days to try this out. Thanks for the ping.

nickbabcock avatar Jul 27 '24 22:07 nickbabcock

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.

shellscape avatar Sep 22 '24 23:09 shellscape

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.

nickbabcock avatar Sep 23 '24 14:09 nickbabcock