此问题已在此处有答案:
Passing environment-dependent variables in webpack(17回答)
2个月前关闭。
在webpack.config.js
中:
module.exports = {
entry: {
index: "./src/index.js",
content: "./src/content.js"
},
mode: "production", // How do I access this value from the React code?
devtool: 'inline-source-map',
...
根据mode
是'development'
还是'production'
,我需要为PayPal使用不同的客户端ID(沙盒或真实的)。我希望避免重复。那么,我如何从我的React代码中访问该值?
1条答案
按热度按时间yb3bgrhw1#
您可以通过将模块导出扩展为箭头函数来访问该模式。这是第二个参数(args)的一部分,我们可以解构它。使用webpack define来更改环境变量。
然后,您可以通过在客户端JS中的任何位置引用SOME_ENV(或任何您称之为的东西)来访问该环境变量。