[英]Webpack.config.js process.env.NODE_ENV not workinkg. - ReactJS -
I have next Webpack.config.js: 我有下一个Webpack.config.js:
'use strict';
const WEBPACK = require('webpack');
const PATH = require('path');
const CopyFiles = require('copy-webpack-plugin');
const BaseName = "/upct";
const CONFIG = {
resolve: {
extensions: ['', '.js', '.jsx']
},
context: __dirname,
entry: {
app: ['./src/index.jsx']
},
/*
output: {
path: PATH.join(__dirname, '/public'),
/*path: './public',*//*
publicPath: BaseName+'/',
filename: 'index.js'
},
/*
devServer: {
host: 'localhost',
port: 3000,
contentBase: PATH.join(__dirname, '/public'),
inline: true,
historyApiFallback: true,
headers: {
"Access-Control-Allow-Origin": "*"
}
},
*/
module: {
loaders: [
{
test: /(\.js|.jsx)$/,
loader: 'babel',
query: {
"presets": [
"es2015",
"react",
"stage-0"
],
"plugins": [
"react-html-attrs",
"transform-decorators-legacy",
"transform-class-properties"
]
}
}
]
},
plugins: [
new WEBPACK.DefinePlugin({
BASENAME: JSON.stringify(BaseName),
'process.env': {
/*'NODE_ENV': JSON.stringify(process.env.NODE_ENV)*/
'NODE_ENV': JSON.stringify('production')
}
})
]
}
if (process.env.NODE_ENV === 'production') {
CONFIG.output = {
path: PATH.join(__dirname, '/publicProduction'),
publicPath: BaseName+'/',
filename: 'index.js'
};
CONFIG.plugins.push(
new WEBPACK.optimize.UglifyJsPlugin({
beautify: false,
mangle: {
screw_ie8: true,
keep_fnames: true
},
compress: {
screw_ie8: true,
warnings: false
},
comments: false
})
);
//babelSettings.plugins.push("transform-react-inline-elements");
//babelSettings.plugins.push("transform-react-constant-elements");
} else {
//CONFIG.devtool = "#cheap-module-source-map"
CONFIG.output = {
path: PATH.join(__dirname, '/publicDeveloping'),
publicPath: BaseName+'/',
filename: 'index.js'
};
CONFIG.devServer = {
host: 'localhost',
port: 3000,
contentBase: PATH.join(__dirname, '/src'),
inline: true,
historyApiFallback: true,
headers: {
"Access-Control-Allow-Origin": "*"
}
}
/*
CONFIG.plugins.push(
new webpack.HotModuleReplacementPlugin()
);*/
}
module.exports = CONFIG;
and next scripts: 和下一个脚本:
"scripts": {
"build": "SET NODE_ENV='building' && webpack --progress --watch --colors",
"serve": "SET NODE_ENV='testing' && webpack-dev-server --progress --colors",
"production": "SET NODE_ENV='production' && webpack --progress -p"
},
But never into in IF (when I run npm run production
), always into in ELSE (Run the script that I run). 但是永远不要进入IF(当我运行
npm run production
),总是进入ELSE(运行我运行的脚本)。 Why could it be? 为什么会这样呢? (above I have declared NODE_ENV = 'production', I do not understand why it is not working...).
(上面我已经声明了NODE_ENV ='production',我不明白为什么它不起作用...)。
Thank you. 谢谢。
I used to have the same kind of problem. 我曾经有过类似的问题。 Try trimming the string first and then comparing it.
尝试先修剪字符串,然后再进行比较。
if (process.env.NODE_ENV.trim() === 'production') {
// Do Something
}
1 more thing, the -p
flag for webpack
and SET NODE_ENV=production
are basically the same thing so I don't think you need them both. 还有1件事,
webpack
的-p
标志和SET NODE_ENV=production
基本上是同一件事,因此我认为您不需要两者。
You might want to try: SET NODE_ENV=production webpack --progress
您可能需要尝试:
SET NODE_ENV=production webpack --progress
If you care about cross platform, you might want to try npm
package cross-env
. 如果您关心跨平台,则可能要尝试使用
npm
package cross-env
包。
Once you install package cross-env
, you would change your script to look like: 一旦安装了
cross-env
软件包,就可以将脚本更改为:
Your script would change to cross-env NODE_ENV=production webpack --progress
您的脚本将变为
cross-env NODE_ENV=production webpack --progress
Just another thought. 只是另一个想法。 You can try this,
你可以试试看
"production": "webpack -p"
and in webpack config file 并在webpack配置文件中
const PROD_ENV = process.argv.indexOf('-p');
and using ternary condition 并使用三元条件
...PROD_ENV ? [prod settings] : [other settings]
or using if condition 或使用条件
if(PROD_ENV > 0) {
...prod settings
} else {
...other settings
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.