const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const OfflinePlugin = require('offline-plugin')

module.exports = require('./webpack.base.babel')({
  // Skip hot-loading in production
  entry: [
    path.join(process.cwd(), 'app/app.js')
  ],

  // Utilize long-term caching by adding content hashes to compiled assets
  output: {
    filename: '[name].[chunkhash].js',
    chunkFilename: '[name].[chunkhash].chunk.js'
  },

  plugins: [
    new webpack.optimize.ModuleConcatenationPlugin(),
    // new webpack.LoaderOptionsPlugin({
    //   minimize: true,
    //   debug: false
    // }),

    // new webpack.optimize.UglifyJsPlugin({
    //   compress: {
    //     unused: true,
    //     dead_code: true,
    //     warnings: false
    //   }
    // }),

    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      children: true,
      minChunks: 2,
      async: true
    }),

    // Minify and optimize index.html
    new HtmlWebpackPlugin({
      template: 'app/index.html',
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeRedundantAttributes: true,
        useShortDoctype: true,
        removeEmptyAttributes: true,
        removeStyleLinkTypeAttributes: true,
        keepClosingSlash: true,
        minifyJS: true,
        minifyCSS: true,
        minifyURLs: true
      },
      inject: true
    }),

    // Keep it in the end to capture all HtmlWebpackPlugin's assets manipulation
    // and leak it's manipulations to HtmlWebpackPlugin
    new OfflinePlugin({
      relativePaths: false,
      publicPath: '/',

      // No need to cache .htaccess. See http://mxs.is/googmp
      excludes: ['.htaccess'],

      caches: {
        main: [':rest:'],

        // All chunks marked as `additional`, loaded after main section
        // and do not prevent SW to install. Change to `optional` if
        // you do not want them to be preloaded at all (cached on first load)
        additional: ['*.chunk.js']
      },
      safeToUseOptionalCaches: true,
      AppCache: false
    })
  ],

  performance: {
    assetFilter: (assetFilename) => !(/(\.map$)|(^(main\.|favicon\.))/.test(assetFilename))
  }
})

@@ -557,27 +557,24 @@
 lugin(),%0A   
- //
  new webpack
@@ -599,19 +599,16 @@
 in(%7B%0A   
- //
    minim
@@ -621,19 +621,16 @@
 rue,%0A   
- //
    debug
@@ -636,27 +636,24 @@
 g: false%0A   
- //
  %7D),%0A%0A    //
@@ -649,19 +649,16 @@
 %7D),%0A%0A   
- //
  new web
@@ -692,21 +692,18 @@
 n(%7B%0A    
-//
   
- 
 compress
@@ -705,27 +705,24 @@
 press: %7B%0A   
- //
      unused:
@@ -727,27 +727,24 @@
 d: true,%0A   
- //
      dead_co
@@ -756,19 +756,16 @@
 rue,%0A   
- //
      war
@@ -784,19 +784,16 @@
 %0A   
- //
    %7D%0A   
  // 
@@ -788,19 +788,16 @@
    %7D%0A   
- //
  %7D),%0A%0A  

add back loader options and uglify