env
2023年12月24日
一、认识
--env
用来设置webpack
配置文件的函数参数
二、配置
2.1 命令行
-
package.json
配置命令 --env{
"name": "Webpack",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev":"webpack --env dev",
"build":"webpack --env prod"
},
"devDependencies": {
"css-loader": "^6.7.1",
"html-webpack-plugin": "^5.5.0",
"style-loader": "^3.3.1",
"webpack": "^5.72.0",
"webpack-cli": "^4.9.2"
}
} -
webpack.config.js
使用 env 变量module.exports = (env) => {
console.log(env) // { WEBPACK_BUNDLE: true, WEBPACK_BUILD: true, prod: true }
if(env['prod']){
console.log('这是生产环境')
}else if(env['dev']){
console.log('这是测试环境')
}
return {
mode: "production",
entry: Path.resolve(process.cwd(), "./src/index.js"),
…………
};
};