I need different builds for my sveltekit app. I would like to do something like
npm run build --option1
npm run build --option2
For option1, I want the following vite.config.js
import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()],
build: {
minify: false,
outDir: "../inst/www",
sourcemap: true,
emptyOutDir: true,
},
//base: "",
});
and for option 2:
import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()],
build: {
minify: true,
outDir: "../inst/www",
sourcemap: false,
emptyOutDir: true,
},
//base: "",
});
Thus, minify: true
and sourcemap: false
. I was looking for solutions and found a couple but they all seemed rather overcomplicated for such a simple task. So, I am looking for advice of how to best achieve this.
If you really want to use plain arguments you have to jump through a few hoops. Any arguments are first passed to
npm
, to prevent that you would need to add--
. Then the arguments end up being sent tovite
, which does not knowoption1
and would crash, so you need another--
separator.So using
would just set an additional argument which you then could read from
process.argv
in thevite.config.json
and conditionally change the config.If you just want to switch out the entire config, you could just define two different build scripts that set the appropriate Vite flags in
package.json
:Which then would be executed via
(You could also combine adding scripts with the first option, of course.)
Aside from that there is also the environment variable option. You could set them with/before the
npm run
and then access the variables in the config viaprocess.env
. Again, setting the variable could be extracted to named scripts but how this is done is platform dependent (there are helpers likecross-env
to get around that issue).cross-env
example: