2016-08-10 96 views
0

我会用丑化版怎样的WebPack React.js使用丑化版

webpack.config.js

module.exports = { 
    entry: './_react/index', 
    output: { 
     path: './dist/public/app', 
     filename: 'bundle.min.js' 
    }, 
    devtool: 'source-map', 
    devServer: { 
     contentBase: __dirname 
    }, 
    module: { 
     loaders: [{ 
      test: /\.js?$/, 
      exclude: /(node_modules|bower_components)/, 
      loader: ['babel-loader'], 
      query: { 
       presets: ['es2015', 'react'] 
      } 
     }] 
    } 
} 

喜欢的WebPack我React.js应用终端

webpack -p 

enter image description here

预览在网络浏览器后,我得到这个错误

enter image description here

请指导,谢谢

回答

1

正如jmargolisvt所说,您需要设置NODE_ENV。

尝试更新webpack.config.js如下:

plugins: [ 
    new webpack.DefinePlugin({ 
    'process.env': { 
     'NODE_ENV': JSON.stringify('production') 
    } 
    }) 
], 

或者在你目前的配置为:

module.exports = { 
    entry: './_react/index', 
    output: { 
     path: './dist/public/app', 
     filename: 'bundle.min.js' 
    }, 
    devtool: 'source-map', 
    devServer: { 
     contentBase: __dirname 
    }, 
    module: { 
     loaders: [{ 
      test: /\.js?$/, 
      exclude: /(node_modules|bower_components)/, 
      loader: ['babel-loader'], 
      query: { 
       presets: ['es2015', 'react'] 
      } 
     }] 
    } 
    plugins: [ 
     new webpack.DefinePlugin({ 
      'process.env': { 
       'NODE_ENV': JSON.stringify('production') 
      } 
     }) 
    ], 
} 

确保使用JSON.stringify插入一个字符串,而不是变量production

1

您需要设置NODE_ENV生产。来自React site

注意:默认情况下,React将处​​于开发模式。要在生产模式下使用React,请将环境变量NODE_ENV设置为生产(使用envify或webpack的DefinePlugin)。