4

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);
Thahzan
  • 975
  • 2
  • 9
  • 23
  • Are you using the dead code elimination plugin? Or just the basic dead code elimination built in? – Deckerz Jun 11 '19 at 08:11
  • Haven't added on anything. Just stock React Native (and it's metro bundler). If there are plugins I could add to make it more efficient, I'm more than happy to know about them. – Thahzan Jun 11 '19 at 08:13
  • 1
    I believe the default dead code eliminator in babel is very basic. But they do have an extra plugin on their site if you search dead code. Also looking at the example. Is there a need to turn this code into a ternary? As a if statement block for things like this is significantly easier to read than an one-line code block. – Deckerz Jun 11 '19 at 08:48
  • Agreed that this is easier to read. Just curious whether it works like that. I'll take a look at plugins. Thanks – Thahzan Jun 11 '19 at 10:02

0 Answers0