7

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__

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

it seems there is a mistake that came from the blog post, I guess the condition must be: __DEV__: process.env.NODE_ENV !== 'production'
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;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.