Here is a collection of some babel macros I created.
- install babel-plugin-macros and add it to your babel config.
- choose one macro from list below and start shipping.
import runCodeForEnvVar from 'jser.macro/dist/runCodeForEnvVar.macro.cjs';
runCodeForEnvVar("ENABLE_DEBUG", () => {
console.log("ENABLE_DEBUG enabled! 1");
});
runCodeForEnvVar("ENABLE_ADVANCED_LOGGING", () => {
console.log("ENABLE_ADVANCED_LOGGING enabled! 2");
});
runCodeForEnvVar("ENABLE_AB_TEST", () => {
console.log("ENABLE_AB_TEST enabled");
});
Let's say if ENABLE_DEBUG=true ENABLE_ADVANCED_LOGGING=true
is set when build,
then the built asset will be like sth below. See that the last call expression is
stripped completely.
(() => {
console.log("ENABLE_DEBUG enabled! 1");
})();
(() => {
console.log("ENABLE_ADVANCED_LOGGING enabled! 2");
})();