由于uglifyJsPlguin,webpack构建失败

时间:2017-06-12 04:52:05

标签: javascript webpack uglifyjs

我尝试构建webpack.config.js文件。我的命令是webpack --config webpack.config.prod.js --progress --colors。但由于ulgifyJsPlugin,我的建筑失败了。当我在webpack.config.prod.js中删除uglifyJsPlugin时,构建成功,但在有UlgifyJsPlugin时无法正常工作。我希望通过webpack最小化我的代码 - 比如删除console.log代码和其他不必要的代码。另外,我使用的是webpack 1。

const webpack = require('webpack');
const path = require('path');
const nodeModulesPath = path.resolve(__dirname, 'node_modules');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const config = {
  entry: [path.join(__dirname, '/src/index.js')],
  devtool: 'source-map',
  output: {
    path: path.join(__dirname, 'dist'), 
    filename: 'bundle.js', 
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/, 
        loader: 'babel-loader', 
        exclude: [nodeModulesPath]
      },
      {
        test: /\.css$/,
        loaders: ['style', 'css']
      }
    ]
  },
  resolve: {
    root: [
      path.resolve('./src'),
      path.resolve('./style')
    ],
    extensions: ['', '.js', '.jsx', '.css'],
    packageAlias: 'browser'
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html'
    }),
    new webpack.DefinePlugin({
      'process.env':{
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new AppCachePlugin({
      exclude: ['.htaccess']
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false,
      }
    })
  ],
};

module.exports = config;

1 个答案:

答案 0 :(得分:1)

尝试修改您的plugins部分以包含这样的插件:

plugins: [
        ...,
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
            },
            comments: false,
            sourceMap: false,
            mangle: true
        })        
    ]

这对我有用。

另外,你确定要npm i --save吗?确保您已在node_modules

中添加了该插件
相关问题