Hi folks,
in the past I was using ifdef-loader
https://www.npmjs.com/package/ifdef-loader to conditionally compile my projects.
Now that there’s vite
, what’s the closest equivalent for this? Or: how do you guys compile conditionally?
Any comment welcome!
you can set any ENV vars and do conditional stuff in JS like so:
if (process.env.MY_VAR === 'foo') {
doSomething();
} else {
doSomethingElse();
}
And js minifier should remove falsy statements from bundle
Hm - I tried that the other day and it did not work… will try again… (maybe I did something wrong).
Thx for the hint…
OK - finally it works:
if (import.meta.env.VITE_MY_VAR === 'foo') {
doSomething();
} else {
doSomethingElse();
}
it’s not as flexible and powerful as ifdef-loader
, where you can do things like:
/// #if PRODUCTION && version.charAt(0)=='X'
console.log("Ho!");
/// #endif
or even:
/// #if env == 'PRODUCTION'
console.log('Production!');
/// #elif env == 'DEBUG'
console.log('Debug!');
/// #else
console.log('Something else!');
/// #endif
…but it gets most of the work done.
I couldn’t get the process.env.MY_VAR === 'foo'
to work, however, which is not ideal, because I can’t re-use sources from outside (not used with Vite
, but with older versions of F7)…