require('babel-polyfill');
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const entries = require('../../configs/entries');

// Load custome webpack config
var webpackConfig = (env, config) => config
try {
    webpackConfig = require('../site/webpack.config.js');
} catch (e) {}

module.exports = env => {

    /**
     * Augument entries with 'whatwg-fetch' and 'babel-polyfill'
     */
    const _entries = {}
    Object.keys(entries).forEach((key, i) => {
        if(key === 'base'){
            _entries[key] = ['babel-polyfill', 'whatwg-fetch'].concat(entries[key]);
        } else {
            _entries[key] = ['whatwg-fetch'].concat(entries[key]);
        }
    });

    // 
    const entriesOrder = { core: 0 };
    Object.keys(entries).forEach((key, i) => {
        entriesOrder[key] = i+1;
    });

    /**
     * Plugin setting
     */
    var plugins = [
        new ExtractTextPlugin('../css/core.bundle.css'),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'core',
            minChunks: Infinity,
        }),
        new HtmlWebpackPlugin({
            excludeChunks: ['base_styles'],
            minify: false,
            chunksSortMode: (e1, e2) => (entriesOrder[e1.names[0]] - entriesOrder[e2.names[0]]),
            filename: path.resolve('../_includes/dist/footer_scripts.inc'),
            template: 'src/core/.empty',
        }),
    ];
    // Production
    if (env.prod) {
        plugins = plugins.concat([
            new webpack.LoaderOptionsPlugin({
                minimize: true,
                debug: false,
            }),
            new webpack.DefinePlugin(
                Object.assign({},
                    {},
                    {
                        'process.env': {
                            NODE_ENV: JSON.stringify('production'),
                        }
                    }
                )
            ),
            new webpack.optimize.UglifyJsPlugin({
                compress: {
                    screw_ie8: true,
                    warnings: false,
                }
            }),
        ])
    }
    // Dev
    else if (env.dev) {
        plugins = plugins.concat([
            new webpack.DefinePlugin({})
        ])
    }

    return webpackConfig(env, {
        entry: _entries,
        output: {
            filename: env.prod ? '[name].[chunkhash].bundle.js' : '[name].[chunkhash].dev.bundle.js',
            path: path.resolve('../assets/dist/js/'),
            publicPath: '/assets/dist/js/',
        },
        resolve: {
            extensions: ['.js', '.jsx', '.yaml'],
        },
        devtool: env.dev ? 'source-map': '',
        module: {
            loaders: [
                {
                    exclude: /(node_modules|bower_components)/,
                    test: /(\.js$|\.jsx$)/,
                    loader: 'babel-loader',
                    query: {
                    presets: ['env', 'react', 'stage-2'],
                    },
                },
                {
                    exclude: /(node_modules|bower_components)/,
                    test: /\.yaml$/,
                    loader: 'yaml-loader',
                },
                {
                    test: /\.json$/,
                    loader: 'json-loader',
                },
                {
                    test: /\.scss$|\.css$/,
                    loader: ExtractTextPlugin.extract('css-loader!sass-loader'),
                },
            ],
        },
        plugins: plugins,
        node: {
            fs: 'empty',
            child_process: 'empty',
        }
    });
};

@@ -206,24 +206,83 @@
 k-plugin');%0A
+const UglifyJsPlugin = require('uglifyjs-webpack-plugin');%0A
 const entrie
@@ -2098,33 +2098,16 @@
     new 
-webpack.optimize.
 UglifyJs
@@ -2135,15 +2135,20 @@
     
-compres
+uglifyOption
 s: %7B
@@ -2170,24 +2170,22 @@
     
-  screw_ie8: tru
+warnings: fals
 e,%0A 
@@ -2197,34 +2197,27 @@
             
-  warnings
+ie8
 : false,%0A   
@@ -2223,32 +2223,32 @@
               %7D%0A
+
             %7D),%0A
@@ -2237,33 +2237,32 @@
 %7D%0A            %7D)
-,
 %0A        %5D)%0A    

Update webpack.config.js