5

If you want to share code between RN and RN-web, __DEV__ should also be provided in the both platform.

However I can't add DEV using const __DEV__ = process.env.NODE_ENV !== 'production'; new webpack.DefinePlugin({__DEV__})

I can set window.__DEV__ fine, but RN code uses __DEV__

I've also tried adding module:metro-react-native-babel-preset

I've seen React Native - __DEV__ is not defined

/* global __DEV__ */ works, but hope there's a way to fix it without modifying all source which uses __DEV__

eugene
  • 39,839
  • 68
  • 255
  • 489

2 Answers2

4

in your webpack.config.js, add this:

  plugins: [
    // `process.env.NODE_ENV === 'production'` must be `true` for production
    // builds to eliminate development checks and reduce build size. You may
    // wish to include additional optimizations.
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
      __DEV__: process.env.NODE_ENV !== 'production' || true,
    }),
  ],

see https://medium.com/@alexander.forselius/experiment-combining-native-web-ios-android-and-macos-development-in-rectnative-part-1-ecd5887e9cfc

KingAmo
  • 384
  • 3
  • 14
  • 3
    it seems there is a mistake that came from the blog post, I guess the condition must be: `__DEV__: process.env.NODE_ENV !== 'production'` – a.malyushko Aug 22 '21 at 12:28
1

I solved it by making it depend on the webpack mode input.

In webpack.config.js:

const config = {
  ...
  plugins: [],
  ...
}

module.exports = (env, argv) => {
  config.plugins.push(new webpack.DefinePlugin({
    __DEV__: JSON.stringify(argv.mode !== 'production'),
  }));

  return config;
};
user3087615
  • 355
  • 1
  • 3
  • 12