Conditional build based on environment using Webpack

You can use the define plugin. I use it by doing something as simple as this in your webpack build file where env is the path to a file that exports an object of settings: // Webpack build config plugins: [ new webpack.DefinePlugin({ ENV: require(path.join(__dirname, ‘./path-to-env-files/’, env)) }) ] // Settings file located at `path-to-env-files/dev.js` … Read more

MySQL Conditional Insert

If your DBMS does not impose limitations on which table you select from when you execute an insert, try: INSERT INTO x_table(instance, user, item) SELECT 919191, 123, 456 FROM dual WHERE NOT EXISTS (SELECT * FROM x_table WHERE user = 123 AND item = 456) In this, dual is a table with one row only … Read more

#ifdef #ifndef in Java

private static final boolean enableFast = false; // … if (enableFast) { // This is removed at compile time } Conditionals like that shown above are evaluated at compile time. If instead you use this private static final boolean enableFast = “true”.equals(System.getProperty(“fast”)); Then any conditions dependent on enableFast will be evaluated by the JIT compiler. … Read more

How to compare strings in C conditional preprocessor-directives

I don’t think there is a way to do variable length string comparisons completely in preprocessor directives. You could perhaps do the following though: #define USER_JACK 1 #define USER_QUEEN 2 #define USER USER_JACK #if USER == USER_JACK #define USER_VS USER_QUEEN #elif USER == USER_QUEEN #define USER_VS USER_JACK #endif Or you could refactor the code a … Read more

Conditional with statement in Python

Python 3.3 and above Python 3.3 introduced contextlib.ExitStack for just this kind of situation. It gives you a “stack”, to which you add context managers as necessary. In your case, you would do this: from contextlib import ExitStack with ExitStack() as stack: if needs_with(): gs = stack.enter_context(get_stuff()) # do nearly the same large block of … Read more