選項 | 描述 |
---|---|
development | 會將 process.env.NODE_ENV 的值設爲 development 。啓用 NamedChunksPlugin 和 NamedModulesPlugin |
production | 會將 process.env.NODE_ENV 的值設爲 production 。啓用 FlagDependencyUsagePlugin , FlagIncludedChunksPlugin , ModuleConcatenationPlugin , NoEmitOnErrorsPlugin , OccurrenceOrderPlugin , SideEffectsFlagPlugin 和 UglifyJsPlugin |
"scripts": {
// 默認 mode 爲 development
"dev1": "webpack-dev-server",
// 默認 mode 爲 production,不過這樣寫,打包的時候會有警告
"build1": "webpack",
}
複製代碼
process.env.NODE_ENV
獲取當前的環境變量// index.js
function getEnv() {
console.log(process.env.NODE_ENV);// development | production
}
// webpack.config.js
const path = require('path');
const webpack = require('webpack');
console.log('NODE_ENV',process.env.NODE_ENV);// undefined
module.exports = {
entry:'./src/index.js',
output: {
filename: 'js/[name].js'
},
...
};
複製代碼
"scripts": {
"dev2": "webpack --mode=development",
"build2": "webpack --mode=production",
}
複製代碼
"scripts": {
"dev3": "webpack-dev-server --env=development",
"build3": "webpack --env=production",
}
複製代碼
process.env.NODE_ENV
獲取當前的環境變量// index.js
function getEnv() {
console.log(process.env.NODE_ENV);// undefined
}
// webpack.config.js
const path = require('path');
const webpack = require('webpack');
console.log('NODE_ENV',process.env.NODE_ENV);// undefined
// 經過函數獲取當前環境變量
module.exports = (env,argv) => {
console.log('env',env);// development | production
return {
entry:'./src/index.js',
output: {
filename: 'js/[name].js'
},
...
}
};
複製代碼
// index.js
function getEnv() {
console.log(process.env.NODE_ENV);// development | production
}
// webpack.config.js
const path = require('path');
const webpack = require('webpack');
console.log('NODE_ENV',process.env.NODE_ENV);// undefined
module.exports = {
mode:'development',// development | production
entry:'./src/index.js',
output: {
filename: 'js/[name].js'
},
...
};
複製代碼
window
上),全部模塊都能讀取到該變量的值// index.js
function getEnv() {
console.log(process.env.NODE_ENV);// development
console.log(NODE_ENV);// production
}
// webpack.config.js
const path = require('path');
const webpack = require('webpack');
// 這裏只是湊巧和環境變量同名了,因此纔不會報錯
console.log('process.env.NODE_ENV',process.env.NODE_ENV);// undefined
console.log('NODE_ENV',NODE_ENV);// error !!!
module.exports = {
mode:'development',// development | production
entry:'./src/index.js',
output: {
filename: 'js/[name].js'
},
plugins:[
new webpack.DefinePlugin({
'process.env.NODE_ENV':JSON.stringify('development'),
'NODE_ENV':JSON.stringify('production'),
}),
],
...
};
複製代碼
process.env.NODE_ENV
獲取當前的環境變量NODE_ENV
"scripts": {
"dev1": "cross-env NODE_ENV='production' webpack-dev-server",
"build1": "cross-env NODE_ENV='development' webpack",
}
複製代碼
process.env.NODE_ENV
獲取當前的環境變量// !!!!!!
// npm run build1
// !!!!!!
// index.js
function getEnv() {
console.log(process.env.NODE_ENV);// production
}
// webpack.config.js
const path = require('path');
const webpack = require('webpack');
console.log('process.env.NODE_ENV',process.env.NODE_ENV);// development
module.exports = {
entry:'./src/index.js',
output: {
filename: 'js/[name].js'
},
...
};
複製代碼
"scripts": {
"dev2": "cross-env NODE_ENV='development' --mode development",
"build2": "cross-env NODE_ENV='production' --mode production",
}
複製代碼
process.env.NODE_ENV
獲取當前的環境變量mode
,在 node 環境下讀取到環境變量的值,靠的是 cross-env
// !!!!!!
// npm run build2
// !!!!!!
// index.js
function getEnv() {
console.log(process.env.NODE_ENV);// production
}
// webpack.config.js
const path = require('path');
const webpack = require('webpack');
console.log('process.env.NODE_ENV',process.env.NODE_ENV);// production
module.exports = {
entry:'./src/index.js',
output: {
filename: 'js/[name].js'
},
...
};
複製代碼
cross-env
和 webpack.DefinePlugin
配合使用的時候,沒法經過 process.env.xxx
來獲取到設置的環境變量,須要經過 webpack.DefinePlugin
裏面設置的 key
來獲取。NODE_ENV
變量,而後又到 DefinePlugin
裏設置了一遍環境變量"scripts": {
"dev": "cross-env NODE_ENV='development' --mode development"
}
複製代碼
// index.js
function getEnv() {
console.log(process.env.NODE_ENV);// development
}
// webpack.config.js
const path = require('path');
const webpack = require('webpack');
console.log('process.env.NODE_ENV',process.env.NODE_ENV);// development
module.exports = {
entry:'./src/index.js',
output: {
filename: 'js/[name].js'
},
plugins:[
new webpack.DefinePlugin({
// 設置環境變量
'process.env.NODE_ENV':JSON.stringify('development'),
}),
],
...
};
複製代碼
此時會發現瀏覽器環境和 node 環境都能獲取到設置的變量了,而後就認爲以後再設置其餘的全局變量時,也像這樣寫就好了。javascript
認爲 cross-env
和 DefinePlugin
是配套一塊兒使用的,這個見解自己就很奇怪,由於二者的做用點不同前端
DefinePlugin
的做用:是設置瀏覽器環境下能讀取到的 "全局變量",直接經過 key
讀取,在 node 環境下是沒法讀取到的cross-env
的做用:是經過命令行設置環境變量 NODE_ENV
,使 node 環境下能讀取到,經過 process.env.NODE_ENV
讀取DefinePlugin
裏設置的 key
是 process.env.NODE_ENV
,會覆蓋 webpack 經過 mode
模式設置的環境變量的值"scripts": {
"dev": "cross-env NODE_ENV='development' --mode development"
}
複製代碼
// index.js
function getEnv() {
console.log(process.env.NODE_ENV);// 666666
}
// webpack.config.js
const path = require('path');
const webpack = require('webpack');
console.log('process.env.NODE_ENV',process.env.NODE_ENV);// development
module.exports = {
entry:'./src/index.js',
output: {
filename: 'js/[name].js'
},
plugins:[
new webpack.DefinePlugin({
// 注意:由於同名的 key,這裏的值會覆蓋默認的值 !!!!!!
'process.env.NODE_ENV':JSON.stringify('666666'),
}),
],
...
};
複製代碼
cross-env
是專門用來設置 node 環境變量的webpack.DefinePlugin
是專門用來設置瀏覽器環境下的全局變量(不會掛載到 window
上)React Hooks 詳解 【近 1W 字】+ 項目實戰node
React SSR 詳解【近 1W 字】+ 2個項目實戰webpack