2017-06-12 47 views
0

我嘗試編譯webpack.config.js文件。我的命令是webpack --config webpack.config.prod.js --progress --colors。但是我的建築因ulgifyJsPlugin而失敗。當我移除webpack.config.prod.js中的uglifyJsPlugin時,構建成功,但在UlgifyJsPlugin時無法工作。我想通過webpack最小化我的代碼 - 就像刪除console.log代碼和其他不必要的代碼。另外,我使用的WebPack 1webpack編譯失敗,因爲uglifyJsPlguin

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; 
+0

請確保你已安裝[UglifyJS](https://github.com/mishoo/UglifyJS2)...正如[此處]所述(https://webpack.js.org/plugins/uglifyjs-webpack-plugin /),UglifyJS是這個插件的一個peer-dependency –

回答

1

嘗試修改您的plugins部分,包括像這樣的插件:

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

這對我的作品。

另外,你確定要做npm i --save嗎?確保你的插件在你的node_modules