之前我总结过使用react-scripts
如何通过env
进行环境配置(原文),但是我们在打包的时候常用的是webpack
。所以我针对webpack
总结了一下如何使用env
进行环境配置。
安装
首先我们需要安装相应的插件,我将我的package.json
放在下面了,大家按需安装吧。
{
"name": "react-remplate",
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/polyfill": "^7.12.1",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"axios": "^0.21.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "dotenv -e .env.development webpack-dev-server",
"backserver": "dotenv -e .env.backserver webpack-dev-server",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/cli": "^7.14.8"