2016-08-10 47 views
0

我在web应用程序上使用webpack + react + redux。我使用webpack-dev-server来启动开发Web服务器。当我访问一个浏览器我的应用程序,它提供了以下错误消息在控制台上:为什么webpack给NODE_ENV一个错误?

Warning: It looks like you're using a minified copy of the development build of React. When deploying React apps to production, make sure to use the production build which skips development warnings and is faster. 

You are currently using minified code outside of NODE_ENV === 'production'. This means that you are running a slower development build of Redux. You can use loose-envify to ensure you have the correct code for your production build. 

下面是我的webpack.config.js文件。我没有指定生产模式,为什么webpack会给我这样的警告信息?我该如何摆脱它?

const webpack = require('webpack'); 
const path = require('path'); 
const NpmInstallPlugin = require('npm-install-webpack-plugin'); 
const WebpackShellPlugin = require('webpack-shell-plugin'); 
var CompressionPlugin = require("compression-webpack-plugin"); 

const PATHS = { 
    react: path.join(__dirname, 'node_modules/react/dist/react.min.js'), 
    app: path.join(__dirname, 'src'), 
    build: path.join(__dirname, './dist') 
}; 

module.exports = { 
    entry: { 
    app: './app/index.jsx', 
    android: './app/utils/platform_android.js', 
    ios: './app/utils/platform_ios.js', 
    web: './app/utils/platform_web.js', 
    vendor: [ 
     'axios', 
     'react', 
     'react-dom', 
     'react-redux', 
     'react-router', 
     'react-router-redux', 
     'redux', 
     'redux-thunk', 
     'react-alert', 
     'sha1', 
     'moment', 
     'nuka-carousel', 
     'react-cookie', 
     'material-ui', 
     'react-spinkit', 
     'react-tap-event-plugin', 
     'react-tappable', 
    ], 
    }, 
    output: { 
    path: PATHS.build, 
    filename: '[name].bundle.js', 
    }, 
    watch: true, 
    devtool: 'source-map', 
    relativeUrls: true, 
    resolve: { 
    extensions: ['', '.js', '.jsx', '.css', '.less'], 
    modulesDirectories: ['node_modules'], 
    alias: { 
     normalize_css: __dirname + '/node_modules/normalize.css/normalize.css', 
    } 
    }, 
    module: { 
    preLoaders: [ 

     { 
     test: /\.js$/, 
     loader: "source-map-loader" 
     }, 
     // { 
     // test: /\.js$/, 
     // exclude: /node_modules/, 
     // loader: 'jshint-loader' 

     // } 
    ], 
    loaders: [ 

     { 
     test: /\.html$/, 
     loader: 'file?name=[name].[ext]', 
     }, 
     { 
     test: /\.jsx?$/, 
     exclude: /node_modules/, 
     loader: 'babel-loader?presets=es2015', 
     }, 
     { 
     test: /\.less$/, 
     loader: "style!css!less", 
     }, 
     {test: /\.css$/, loader: 'style-loader!css-loader'}, 
     {test: /\.png$/, loader: "url-loader?limit=100000"}, 

     { 
     test: /\.js$/, 
     exclude: /node_modules/, 
     loaders: ['babel-loader?presets=es2015'] 
     }, 
     { 
     test: /\.svg$/, 
     loader: 'svg-sprite', 
     include: /public\/icons/ 
    } 
    ] 
    }, 
    plugins: [ 
    new webpack.optimize.UglifyJsPlugin({ 
     compress: { 
     warnings: false, 
     }, 
     output: { 
     comments: false, 
     }, 
    }), 
    new NpmInstallPlugin({ 
     save: true // --save 
    }), 

    new CompressionPlugin({ 
     asset: "[path].gz[query]", 
     algorithm: "gzip", 
     test: /\.js$|\.html$/, 
     threshold: 10240, 
     minRatio: 0.8 
    }), 
    new webpack.optimize.CommonsChunkPlugin(/* chunkName= */["vendor"], /* filename= */"[name].bundle.js", Infinity), 
    ], 
    devServer: { 
    colors: true, 
    contentBase: __dirname, 
    historyApiFallback: true, 
    hot: true, 
    inline: true, 
    port: 9093, 
    progress: true, 
    stats: { 
     cached: false 
    } 
    } 
} 

EDIT1

我删除了这条线:

react: path.join(__dirname, 'node_modules/react/dist/react.min.js'), 

然后更新NODE_ENV发展如下:

new webpack.DefinePlugin({ 
     "process.env": { 
     NODE_ENV: JSON.stringify("development") 
     } 
    }) 

然后我仍然得到了同样的警告。

warning.js:14You are currently using minified code outside of NODE_ENV === 'production'. This means that you are running a slower development build of Redux. You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) to ensure you have the correct code for your production build. 

回答

0

你现在得到的警告是因为你让的WebPack运行如下的建立,但你仍然将NODE_ENV设置为development

如果您使用UglifyJsPlugin,应始终将NODE_ENV设置为production。如果您不打算生产,请删除UglifyJsPlugin以免发生任何警告。

Webpack,React和Redux尝试给你一些最佳实践提示。在开发模式下(NODE_ENV未设置为production),它们都会发出更多警告并且性能较低。当你缩小它们时,他们假设你正在运行一个生产版本。对于生产版本,他们真的希望NODE_ENV被正确设置。

因此,在短期:

  • 生产建立:设定NODE_ENVproduction和使用UglifyJsPlugin
  • 开发版本:设置NODE_ENVdevelopment并且不使用任何缩小插件。
1

我不知道怎么说的更清晰比的WebPack呢......

你是不是在生产构建模式,但使用作出反应的缩小版本。使用非精缩之一,所以跟你有你的反应不精缩

react: path.join(__dirname, 'node_modules/react/dist/react.js'), 

或任何地方更换

react: path.join(__dirname, 'node_modules/react/dist/react.min.js'), 

而这不是一个错误 - 这是一个警告,所以你仍然可以使用这个。

只是另一件事:path.join为每个操作系统添加适当的斜杠。你在这里做什么是

path.join(__dirname, 'node_modules/react/dist/react.min.js') 

时,你应该去

path.join(__dirname, 'node_modules', 'react', 'dist', 'react.min.js') 

这是怎么path应正确使用

+0

这是否意味着我需要定义两个webpack配置文件?一种用于开发模式,一种用于生产模式?这两个文件中的大部分都是相同的,只有几行不同,对吧? –

+0

似乎我没有在我的配置文件中的任何地方使用反应路径,所以我删除了它。我编辑了我的帖子,仍然有相同的错误。 –

+0

@赵一好两个webpack文件是一个很正常的做法,但是你可以使用一个非缩小版本和一个UglifyJS webpack插件,它可以缩小包括你的代码在内的所有东西。关于第二条评论:错误是否一样?关于反应? – smnbbrv

0

我通过从开发构建的webpack.config.js中删除下面的配置来修复它。看来这个插件会压缩不适合开发模式的js代码。

new webpack.optimize.UglifyJsPlugin({ 
    compress: { 
    warnings: false, 
    }, 
    output: { 
    comments: false, 
    }, 
}) 
相关问题