'use strict';

const NODE_ENV = process.env.NODE_ENV || 'development';

const cssnano = require('cssnano'),
    ExtractTextPlugin = require('extract-text-webpack-plugin'),
    path = require('path'),
    postcssFlexbugsFixes = require('postcss-flexbugs-fixes'),
    webpack = require('webpack');

module.exports = {
    context: path.resolve(__dirname, './assets'),
    entry: {
        bundle: './js/app.js',
        styles: './css/app.scss'
    },
    output: {
        path: path.resolve(__dirname, './public/assets'),
        filename: '[name].js'
    },

    devtool: NODE_ENV == 'development' ? 'cheap-eval-source-map' : null,

    module: {
        loaders: [
            // Compile SASS
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract('style-loader', 'css-loader!postcss-loader!resolve-url!sass-loader?sourceMap')
            }
        ]
    },

    plugins: [
        // Extract CSS to separate file
        new ExtractTextPlugin('[name].css', {
            allChunks: true
        }),
        // Provide jQuery globally
        new webpack.ProvidePlugin({
            jQuery: 'jquery'
        })
    ],

    postcss: function () {
        return [
            postcssFlexbugsFixes,
            cssnano({
                autoprefixer: {
                    add: true
                },
                discardComments: NODE_ENV == 'production'
            })
        ];
    },

    resolve: {
        modulesDirectories: ['node_modules']
    },

    watch: NODE_ENV == 'development'
};

// Minify JS in production mode
if (NODE_ENV == 'production') {
    module.exports.plugins.push(
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            },
            output: {
                comments: false,
            },
            sourceMap: false
        })
    );
}

@@ -62,16 +62,98 @@
 lopment'
+,%0A    isDevelopment = 'development' == NODE_ENV,%0A    isProduction = !isDevelopment
 ;%0A%0Aconst
@@ -372,16 +372,171 @@
 ack');%0A%0A
+// Minify CSS for production%0Aconst sassLoader = 'css-loader' + (isDevelopment ? '?sourceMap' : '!postcss-loader') + '!resolve-url!sass-loader?sourceMap';%0A%0A
 module.e
@@ -803,30 +803,19 @@
 evtool: 
-NODE_ENV == 'd
+isD
 evelopme
@@ -820,23 +820,32 @@
 ment
-'
  ? '
+#
 cheap-
-eval
+module-inline
 -sou
@@ -1040,69 +1040,18 @@
 r', 
-'css-loader!postcss-loader!resolve-url!sass-loader?sourceMap'
+sassLoader
 )%0A  
@@ -1181,49 +1181,8 @@
 css'
-, %7B%0A            allChunks: true%0A        %7D
 ),%0A 
@@ -1455,16 +1455,50 @@
     add:
+ true,%0A                    remove:
  true%0A  
@@ -1551,32 +1551,91 @@
 ts: 
-NODE_ENV == 'production'
+%7B%0A                    removeAll: true%0A                %7D,%0A                safe: true
 %0A   
@@ -1710,16 +1710,18 @@
 ries: %5B'
+./
 node_mod
@@ -1750,22 +1750,11 @@
 ch: 
-NODE_ENV == 'd
+isD
 evel
@@ -1759,17 +1759,16 @@
 elopment
-'
 %0A%7D;%0A%0A// 
@@ -1804,22 +1804,11 @@
 if (
-NODE_ENV == 'p
+isP
 rodu
@@ -1812,17 +1812,16 @@
 oduction
-'
 ) %7B%0A    

Use correct source maps for SASS