const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const HtmlWebpackTemplatePlugin = require('html-webpack-template');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

module.exports = (env) => {
  const isProdEnv = (env && env.production);
  return {
    entry: './src/index.jsx',
    devtool: (isProdEnv ? false : 'source-map'),
    devServer: {
      // @see https://webpack.js.org/configuration/dev-server
      contentBase: path.join(__dirname, 'dist'),
      compress: true,
      port: 9000,
      https: false,
    },
    plugins: [
      new CleanWebpackPlugin(['dist'], { exclude: ['icons'] }),
      new FaviconsWebpackPlugin({
        logo: './src/static/logo.png',
        prefix: 'icons/[hash]/',
        emitStats: false,
        statsFilename: 'icons/stats-[hash].json',
        persistentCache: true,
        inject: true,
        background: '#fff',
        title: 'Antonios Karagiannis',
        // @see https://github.com/haydenbleasel/favicons#usage
        icons: {
          android: true,
          appleIcon: true,
          appleStartup: true,
          coast: false,
          favicons: true,
          firefox: true,
          opengraph: true,
          twitter: true,
          yandex: false,
          windows: false,
        },
      }),
      new HtmlWebpackPlugin({
        title: 'Antonios Karagiannis',
        minify: {
          minifyCSS: true,
          minifyJS: true,
          minifyURLs: true,
          removeComments: true,
          sortClassName: true,
          useShortDoctype: true,
          collapseWhitespace: true,
        },
        inject: false,
        template: HtmlWebpackTemplatePlugin,
        mobile: true,
        lang: 'en-US',
        appMountId: 'root',
      }),
      new UglifyJSPlugin({
        sourceMap: !isProdEnv,
      }),
    ],
    output: {
      filename: '[name].bundle.js',
      path: path.resolve(__dirname, 'dist'),
    },
    module: {
      rules: [{
        test: /\.css$/,
        use: [
          { loader: 'style-loader' },
          { loader: 'css-loader' },
        ],
      },
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['es2015', 'react'],
          },
        },
      },
        // {
        //     test: /\.html$/,
        //     use: ['file-loader?name=[path][name].[ext]!extract-loader!html-loader']
        // },
        // {
        //     test: /\.(html)$/,
        //     use: {
        //         loader: 'html-loader',
        //         options: {
        //             attrs: [':data-src'],
        //             minimize: true
        //         }
        //     }
        // },
        // {
        //     test: /\.(png|jpg|gif)$/,
        //     use: [{
        //         loader: 'file-loader',
        //         options: {}
        //     }]
        // }
      ],
    },
  };
};

@@ -369,59 +369,369 @@
 %7B%0A  
-const isProdEnv = (env && env.production);%0A  return
+/**%0A   * override them as needed%0A   * ie. for testing minification / uglification / etc.%0A   * in lower environments (ci/staging) and the like%0A   *%0A   * if preferred, they can be made dynamic%0A   * through env. variables, etc.%0A   */%0A  const isProdEnv = (env && env.production);%0A  const genSourceMaps = !isProdEnv;%0A  const uglifyCode = isProdEnv;%0A%0A  const config =
  %7B%0A 
@@ -777,27 +777,23 @@
 l: (
-isProdEnv ? false :
+genSourceMaps ?
  'so
@@ -801,16 +801,24 @@
 rce-map'
+ : false
 ),%0A    d
@@ -1834,16 +1834,30 @@
  minify:
+ (uglifyCode ?
  %7B%0A     
@@ -2065,32 +2065,41 @@
  true,%0A        %7D
+ : false)
 ,%0A        inject
@@ -2239,76 +2239,8 @@
 %7D),%0A
-      new UglifyJSPlugin(%7B%0A        sourceMap: !isProdEnv,%0A      %7D),%0A
     
@@ -3361,11 +3361,138 @@
 %7D,%0A  %7D;%0A
+  if (uglifyCode) %7B%0A    config.plugins.push(new UglifyJSPlugin(%7B%0A      sourceMap: genSourceMaps,%0A    %7D));%0A  %7D%0A  return config;%0A
 %7D;%0A

make webpack config for sourcemaps/uglify use constants