var ExtractText = require('extract-text-webpack-plugin');
var Copy = require('copy-webpack-plugin');
var LessClean = require('less-plugin-clean-css');
var HtmlFile = require('html-webpack-plugin');
var webpack = require('webpack');

var ENV = process.env.ENV || 'dev';

var AppResolver = {
  apply: function(resolver) {
    resolver.plugin('module', function (request, callback) {
      if (request.request === 'config') {
        var obj = {
          path: __dirname + '/config',
          request: 'config.' + ENV +'.js',
          query: request.query,
          directory: request.directory
        };
        this.doResolve(['file'], obj, callback);
      } else {
        callback();
      }
    });
  }
};

var config = {
  dev: {
    devtool: 'eval',
    html: {},
    loaders: {
      less: 'css!autoprefixer?browsers=last 5 version' +
        '!less?config=lessLoaderCustom',
      image: ['url?limit=8192&name=asset/[name].[ext]']
    },
    plugins: []
  },

  production: {
    devtool: 'source-map',
    html: {
      minify: {
        collapseWhitespace: true,
        removeScriptTypeAttributes: true,
        removeEmptyAttributes: true,
        removeStyleLinkTypeAttributes: true,
        minifyCSS: true,
      }
    },
    loaders: {
      less: 'css?sourceMap' +
        '!autoprefixer?browsers=last 5 version' +
        '!less?sourceMap=true&config=lessLoaderCustom',
      image: [
        'url?limit=8192&name=asset/[name].[ext]',
        'image-webpack?{progressive:true, optimizationLevel: 7, interlaced: false, pngquant:{quality: "75-90", speed: 4}}'
      ]
    },
    plugins: [
      new webpack.optimize.UglifyJsPlugin({
        compress: {
          warnings: false
        }
      })
    ]
  }
}[ENV];

module.exports = {
  cache: true,
  devtool: config.devtool,
  entry: [
    './main.js',
    './main.less'
  ],
  output: {
    path: 'build',
    filename: 'bundle.js',
    pathinfo: false
  },
  module: {
    loaders: [
      {
        test: /\.jext$/,
        loader: "jext"
      }, {
        test: /\.(png|jpe?g|gif|svg)$/,
        loaders: config.loaders.image
      }, {
        test: /\.less$/,
        loader: ExtractText.extract(config.loaders.less)
      }
    ]
  },
  lessLoader: {
    lessPlugins: [
      new LessClean({advanced: true})
    ]
  },
  plugins: Object.assign([
    new webpack.ResolverPlugin([AppResolver]),
    new webpack.PrefetchPlugin('jext'),
    new webpack.PrefetchPlugin('edispatcher'),
    new webpack.PrefetchPlugin('domd'),
    new ExtractText('bundle.css'),
    new HtmlFile(Object.assign({
      filename: 'index.html',
      template: 'index.html',
      hash: true,
      inject: 'head'
    }, config.html)),
    new Copy([
      { from: './asset', to: 'asset' }
    ])
  ], config.plugins)
};

@@ -1731,16 +1731,479 @@
 %5BENV%5D;%0A%0A
+// Merge plugins%0AArray.prototype.push.apply(config.plugins, %5B%0A  new webpack.ResolverPlugin(%5BAppResolver%5D),%0A  new webpack.PrefetchPlugin('jext'),%0A  new webpack.PrefetchPlugin('edispatcher'),%0A  new webpack.PrefetchPlugin('domd'),%0A  new ExtractText('bundle.css'),%0A  new HtmlFile(Object.assign(%7B%0A    filename: 'index.html',%0A    template: 'index.html',%0A    hash: true,%0A    inject: 'head'%0A  %7D, config.html)),%0A  new Copy(%5B%0A    %7B from: './asset', to: 'asset' %7D%0A  %5D)%0A%5D);%0A%0A
 module.e
@@ -2771,453 +2771,8 @@
 ins:
- Object.assign(%5B%0A    new webpack.ResolverPlugin(%5BAppResolver%5D),%0A    new webpack.PrefetchPlugin('jext'),%0A    new webpack.PrefetchPlugin('edispatcher'),%0A    new webpack.PrefetchPlugin('domd'),%0A    new ExtractText('bundle.css'),%0A    new HtmlFile(Object.assign(%7B%0A      filename: 'index.html',%0A      template: 'index.html',%0A      hash: true,%0A      inject: 'head'%0A    %7D, config.html)),%0A    new Copy(%5B%0A      %7B from: './asset', to: 'asset' %7D%0A    %5D)%0A  %5D,
  con
@@ -2782,13 +2782,12 @@
 .plugins
-)
 %0A%7D;%0A

Fix plugin merge in webpack config