'use strict';

// Modules
const path = require('path');
const webpack = require('webpack');

const DefinePlugin = require('webpack/lib/DefinePlugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ProvidePlugin = require('webpack/lib/ProvidePlugin');
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');

let ENV = process.env.npm_lifecycle_event;
let isTest = ENV === 'test' || ENV === 'test-watch';
let isProd = ENV === 'build';

module.exports = function makeWebpackConfig() {

    let config = {};

    config.entry = isTest ? {} : {
        app: './src/app/app.js',
        vendor: './src/app/vendor.js'
    };

    config.resolve = {
        modulesDirectories: ['web_modules', 'node_modules', 'bower_components']
    };

    config.output = isTest ? {} : {
        path: __dirname + '/dist',
        publicPath: isProd ? './' : 'http://localhost:8080/',
        filename: isProd ? '[name].[hash].js' : '[name].bundle.js',
        chunkFilename: isProd ? '[id].[hash].js' : '[id].bundle.js'
    };

    if (isTest) {
        config.devtool = 'inline-source-map';
    } else if (isProd) {
        config.devtool = 'source-map';
    } else {
        config.devtool = 'eval-source-map';
    }

    config.module = {
        preLoaders: [],
        loaders: [{
            test: /\.js$/,
            loaders: ['babel'],
            exclude: /(node_modules|bower_components)/
        }, {
            test: /\.css$/,
            loader: isTest ? 'null' : ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap!postcss-loader')
        }, {
            test: /\.scss$/,
            loaders: ['style', 'css', 'postcss', 'sass'],
            include: /bootstrap/
        }, {
            test: /\.scss$/,
            loaders: ['style', 'css?modules&localIdentName=[name]__[local]___[hash:base64:5]', 'postcss', 'sass'],
            exclude: /bootstrap/
        }, {
            test: /\.woff$/,
            loader: 'url-loader?limit=10000&mimetype=application/font-woff&name=/assets/font/[hash].[ext]'
        }, {
            test: /\.woff2$/,
            loader: 'url-loader?limit=10000&mimetype=application/font-woff2&name=/assets/font/[hash].[ext]'
        }, {
            test: /\.(eot|ttf)$/,
            loader: 'url-loader?limit=10000&name=/assets/font/[hash].[ext]'
        }, {
            test: /\.(svg|gif|png|jpe?g)$/,
            loader: 'url-loader?limit=1024&name=/assets/images/[hash].[ext]'
        }, {
            test: /\.html$/,
            loader: 'html'
        }]
    };

    if (isTest) {
        config.module.preLoaders.push({
            test: /\.js$/,
            exclude: [
                /node_modules/,
                /__tests__/,
                /test.webpack.js$/
            ],
            loader: 'isparta'
        })
    } else {
        config.module.preLoaders.push({
            test: /\.js$/,
            loader: 'eslint',
            exclude: /(node_modules|bower_components|__tests__)/
        });
    }

    config.postcss = [
        require('autoprefixer')({
            browsers: ['last 3 version']
        })
    ];

    config.plugins = [
        new ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
            'root.jQuery': 'jquery'
        }),
        new webpack.ResolverPlugin(
            new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin('.bower.json', ['main'])
        )
    ];

    if (!isTest) {
        config.plugins.push(
            new DefinePlugin({
                ENV: JSON.stringify(require('./config.js'))
            }),
            new HtmlWebpackPlugin({
                template: './src/public/index.html',
                inject: 'body'
            }),
            new ExtractTextPlugin('[name].[hash].css', {
                disable: !isProd
            })
        )
    } else {
        config.plugins.push(new DefinePlugin({
            ENV: {}
        }))
    }

    if (!isProd) {
        config.plugins.push(
            new webpack.HotModuleReplacementPlugin()
        )
    }

    if (isProd) {
        config.plugins.push(
            new webpack.DefinePlugin({
                'process.env': {
                    NODE_ENV: JSON.stringify('production')
                }
            }),
            new webpack.NoErrorsPlugin(),
            new webpack.optimize.DedupePlugin(),
            new webpack.optimize.UglifyJsPlugin(),
            new CopyWebpackPlugin([{
                from: __dirname + '/src/public'
            }])
        )
    }

    config.devServer = {
        contentBase: './src/public',
        hot: true,
        colors: true,
        inline: true,
        compress: true,
        proxy: {
            '/oauth': {
                target: 'http://localhost:' + (process.env.PORT || 3000) + '/'
            }
        },
        stats: 'minimal'
    };
    return config;
}();

@@ -3232,24 +3232,163 @@
   %7D)%0A    %5D;%0A
+    let CONFIG;%0A    try %7B%0A        CONFIG = JSON.stringify(require('./config.js'))%0A    %7D catch (error) %7B%0A        CONFIG = process.env;%0A    %7D
 %0A    config.
@@ -3713,69 +3713,10 @@
    )
+,
 %0A
-    %5D;%0A%0A    if (!isTest) %7B%0A        config.plugins.push(%0A    
     
@@ -3730,36 +3730,32 @@
  DefinePlugin(%7B%0A
-    
             ENV:
@@ -3794,38 +3794,90 @@
 g.js'))%0A        
+%7D)%0A
     
-%7D)
+%5D;%0A%0A    if (!isTest) %7B%0A        config.plugins.push(
 ,%0A            ne
@@ -4122,100 +4122,8 @@
    %7D
- else %7B%0A        config.plugins.push(new DefinePlugin(%7B%0A            ENV: %7B%7D%0A        %7D))%0A    %7D
 %0A%0A  

fix define