如果您想在RN和RN-web之间共享代码,则两个平台中也应提供__DEV__
。
但是我无法使用const __DEV__ = process.env.NODE_ENV !== 'production'; new webpack.DefinePlugin({__DEV__})
我可以设置window.__DEV__
很好,但是RN代码使用__DEV__
我也尝试添加module:metro-react-native-babel-preset
我见过React Native - __DEV__ is not defined
/* global __DEV__ */
可行,但希望有一种方法可以解决此问题,而无需修改所有使用__DEV__
的源代码
答案 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,
}),
],
答案 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;
};