Skip to main content

How to use __DEV__ constant in ReScript

Global constant __DEV__ is not part of the ReScript language, but you can use it via %external. It is a common pattern that allows you to easily distinguish dev and prod environments. I started using it in my projects as well.

switch %external(__DEV__) {
| Some(_) => Js.log("dev mode")
| None => Js.log("production mode")
}

The code above translates to something like this:

var match$1 = typeof __DEV__ === 'undefined' ? undefined : __DEV__;
if (match$1 !== undefined) {
console.log('dev mode');
} else {
console.log('production mode');
}

Obviously, this code still needs some kind of transpilation (since __DEV__ doesn't exist in JS either).