Compiling conditionally (ifdef-loader)

Hi folks,
in the past I was using 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') {
} else {

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') {
} else {

it’s not as flexible and powerful as ifdef-loader, where you can do things like:

/// #if PRODUCTION && version.charAt(0)=='X'
/// #endif

or even:

/// #if env == 'PRODUCTION'
/// #elif env == '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)…