Add wasm-metadce build
As the original announcement for wasm-metadce stated, it could be used for shrinking size of Wasm in JS bundlers. /cc @kripken @surma
However, right now there are no JS/Wasm builds of wasm-metadce, and the only place it's used in remains inside Emscripten.
Could we build metadce as a JS library as part of binaryen.js bots?
Could we build metadce as a JS library as part of binaryen.js bots?
I think so. Iirc Binaryen's build scripts already support building most, if not all, of the tools to JS so these can be run with node. While we are on it we may also investigate wasm-as, wasm-dis and wasm2js perhaps, if these seem useful to npm install. Doing something similar for the WABT binaries btw.
Yeah that sounds good. Although, aside from installing with node, I think important part of this is ability to run this tool as a library. For that, we'd need to also add a JS interface similarly to what binaryen.js does.
Perhaps one option requiring little effort could be to support importing the tools in JS and call their main function as an API (providing custom stdout/stderr streams). Not as sophisticated as a proper library, but perhaps would already be helpful?
@dcodeIO I think that would be good. A generic way to turn a commandline tool into a library would let us get a lot of stuff working fast. The API might not be perfect but it would be a start to build a nicer API on top of. And doing it that way would avoid the need to refactor the metadce code into a library, etc.
I'm not sure exactly how that would work, but it seems like it could even be a generic option in upstream emscripten perhaps. But we can experiment with it in binaryen maybe, and see how it goes.
I'm not sure exactly how that would work, but it seems like it could even be a generic option in upstream emscripten perhaps.
Sounds good! Iirc this is typically done by checking whether the executing module is the main module, quite similar to how one would do that in Python with __name__ == '__main__', and unconditionally export the main function. Something along the lines of:
function main(argv, options) {
...
return code;
}
exports.main = main;
if (require.main === module) {
process.exit(
main(process.argv.slice(2), {
stdout: process.stdout,
stderr: process.stder
})
);
}
I like this idea, yeah. Just one note: it should support passing stdin as well, as most JS build tools don't produce intermediate files and having to create those just for Binaryen tools would be not very ergonomic.
I've meanwhile added wasm2js, but held back on wasm-metadce as I am unsure how useful it would be without a programmatic API as discussed above. Let me know if you see a use case where it would be sufficient to just have the command.
There is a metadce build now.