Astro Use different wasm files for development and production
While adding wasm to my astro site i came across the need to have 2 versions of my wasm-bindgen wasm one for development with more development logging and better stacktraces etc, and one for production that was MUCH smaller and didnt include all the logging. I was using svelte and wanted to keep the top level import due to loading it dynamically making no sense for this use case. So instead i ended up creating 2 folders one for my production wasm and one for development like this:
Then i added this import import { Game } from "./lib/wasmdev/lib_knuckle";
to the top of my svelte file. But as you might have noticed itll always pull the dev version. To work around this i created a custom astro plugin
When using astro build
the NODE_ENV is set to production making it so the vite aliases the dev build to be the production build making it so only the wasm binary of the production build is used and put into the output folder!
This helps reduce the data i sent to my users and after making these “tools” costs nothing for me to do!
If you use wasm-bindgen like me you might get errors trying to import wasm this can be easily fixed using the vite-plugin-wasm
npm package
just add it to your astro config ala:
And everything will work as expected.