我正在使用react-web和react-native(混合應用程序)的項目。我可以在網上使用cross-env來設置環境變量,但這似乎不適用於react-native。如何將環境變量傳遞給babelrc?
含義:
NPM腳本
cross-env NODE_ENV=development BABEL_ENV=development_web webpack-dev-server --hot --progress --config build/webpack.config.dev.js
工作正常。但
cross-env NODE_ENV=development BABEL_ENV=development_rn && react-native run-ios
不起作用!
我得到了.babelrc
兩個不同BABEL_ENV ENV設置:
{
"env": {
"development_web": {
"presets": ["react", "es2015", "stage-0"],
"plugins": [
[
"react-transform",
{
"transforms": [
{
"transform": "react-transform-hmr",
"imports": ["react"],
"locals": ["module"]
}, {
"transform": "react-transform-catch-errors",
"imports": ["react", "redbox-react"]
}
]
}
],
["import", { "style": "css", "libraryName": "antd-mobile" }],
["transform-decorators-legacy"]
]
},
"development_rn": {
"presets": ["react-native"],
"plugins": [["import", { "libraryName": "antd-mobile" }]]
},
}
}
我怎麼能任命NODE_ENV/BABEL_ENV到development_rn
的反應本土?
我試過'交ENV NODE_ENV =發展BABEL_ENV = development_rn NPM運行test'和測試腳本是:'回聲NODE_ENV = $ NODE_ENV BABEL_ENV = $ BABEL_ENV'它工作正常,回聲結果是正確的。但是當我將測試腳本更改爲'react-native run-ios'時,在react-native應用程序中,它們返回'process.env.NODE_ENV = Nothing'和'process.env.BABEL_ENV = development'。那是否意味着環境變量設置在react-native中不起作用? – Kim
「BABEL_ENV」有一個[開放問題#8723](https://github.com/facebook/react-native/issues/8723)。至於在反應本機代碼中使用環境變量,你可能想看看[在react-native中設置環境變量?](https://stackoverflow.com/questions/33117227/setting-environment-variable-in- react-native),這應該解釋爲什麼他們有默認值,但這並不能解決babel問題。 –