响应本机Web,未定义__DEV__

时间:2020-04-09 07:04:04

标签: reactjs react-native react-native-web

如果您想在RN和RN-web之间共享代码,则两个平台中也应提供__DEV__

但是我无法使用const __DEV__ = process.env.NODE_ENV !== 'production'; new webpack.DefinePlugin({__DEV__})

添加 DEV

我可以设置window.__DEV__很好,但是RN代码使用__DEV__

我也尝试添加module:metro-react-native-babel-preset

我见过React Native - __DEV__ is not defined

/* global __DEV__ */可行,但希望有一种方法可以解决此问题,而无需修改所有使用__DEV__的源代码

2 个答案:

答案 0 :(得分:1)

在您的webpack.config.js中,添加以下内容:

  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,
    }),
  ],

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

答案 1 :(得分:1)

我通过让它依赖于 webpack 模式输入来解决它。

webpack.config.js中:

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

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

  return config;
};