Webpack definePlugin 不是 setting/reading node_env 变量
Webpack definePlugin not setting/reading node_env variable
我在通过 Webpack 的 definePlugin 将 node_env var 注入我的代码时遇到了真正的问题,在阅读了大量帖子后似乎没有任何效果。我觉得我错过了什么...
所以我的生产 webpack 配置是 -
// Config
import path from 'path';
import webpack from 'webpack';
import config from './config';
/**
* Webpack config for compiling the
* React/Redux/ES6 scripts.
*
* ENV = production
*
* @type {Object}
*/
module.exports = {
entry: path.resolve(__dirname, '../', config.assets.scripts.src_dir, config.assets.scripts.entry),
devtool: false,
output: {
path: path.resolve(__dirname, config.assets.scripts.dist_dir),
filename: config.assets.scripts.dist_min
},
module: {
loaders: [
{
test: /.js?$/,
loader: 'babel-loader?presets[]=react,presets[]=es2015,presets[]=stage-0',
exclude: /node_modules/
},
{
test: /\.json$/,
loader: 'json-loader'
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
}),
new webpack.optimize.UglifyJsPlugin({})
]
};
我已经尝试为 react、react-dom 和 react-redux 设置别名,但完全没有用,React 仍然警告我我正在使用 node_env=production 之外的缩小版本(而且 redux 仍然会抛出这个错误)。
仅供参考,我使用的是 webpack 版本 2.2.1。
是不是和babel-loader有什么冲突?如果有人能给我指出正确的方向,那就太好了。
尝试将 NODE_ENV
的 DefinePlugin
更改为这个 -
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV' : JSON.stringify('production')
}),
new webpack.optimize.UglifyJsPlugin({
minimize : true,
compress : {
warnings : false
}
})
]
我在通过 Webpack 的 definePlugin 将 node_env var 注入我的代码时遇到了真正的问题,在阅读了大量帖子后似乎没有任何效果。我觉得我错过了什么...
所以我的生产 webpack 配置是 -
// Config
import path from 'path';
import webpack from 'webpack';
import config from './config';
/**
* Webpack config for compiling the
* React/Redux/ES6 scripts.
*
* ENV = production
*
* @type {Object}
*/
module.exports = {
entry: path.resolve(__dirname, '../', config.assets.scripts.src_dir, config.assets.scripts.entry),
devtool: false,
output: {
path: path.resolve(__dirname, config.assets.scripts.dist_dir),
filename: config.assets.scripts.dist_min
},
module: {
loaders: [
{
test: /.js?$/,
loader: 'babel-loader?presets[]=react,presets[]=es2015,presets[]=stage-0',
exclude: /node_modules/
},
{
test: /\.json$/,
loader: 'json-loader'
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
}),
new webpack.optimize.UglifyJsPlugin({})
]
};
我已经尝试为 react、react-dom 和 react-redux 设置别名,但完全没有用,React 仍然警告我我正在使用 node_env=production 之外的缩小版本(而且 redux 仍然会抛出这个错误)。
仅供参考,我使用的是 webpack 版本 2.2.1。
是不是和babel-loader有什么冲突?如果有人能给我指出正确的方向,那就太好了。
尝试将 NODE_ENV
的 DefinePlugin
更改为这个 -
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV' : JSON.stringify('production')
}),
new webpack.optimize.UglifyJsPlugin({
minimize : true,
compress : {
warnings : false
}
})
]