Does Babel eliminate ternary operator condition via Dead Code Elimination?

466 Views Asked by At

I'm working with React Native and I'm using a dependency (Reactotron) that is used only on debug builds and should not exist on release builds. While reading through the docs, I found out about dead code elimination on Babel.

let foo = baz;
if (__DEV__) {
  foo = bar;
}

As per this comment, Babel will eliminate the if block on release builds since __DEV__ will always be false. But does it still work for ternary operators?

const foo = __DEV__ ? bar : baz;

I can't seem to find any resources that says one way or the other for ternaries. Would Babel simplify the ternary condition into the following on release builds?

const foo = baz;

The usage being the following code block. I was thinking of removing the if statement by using a ternary operator. But I'm not sure if it'll prevent Reactotron from being stripped from the release build.

let enhancer = applyMiddleware(...middlewares);

// Add Reactotron enhancer if debug mode
if (__DEV__) {
  const Reactotron = require('@flashmobile:config/reactotron').default;
  enhancer = compose(applyMiddleware(...middlewares), Reactotron.createEnhancer());
}

const store: Store = createStore(reducers, INITIAL_STATE, enhancer);
0

There are 0 best solutions below