const autoprefixer = require('autoprefixer');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
const WebpackMd5Hash = require('webpack-md5-hash');


//=========================================================
//  ENVIRONMENT VARS
//---------------------------------------------------------
const NODE_ENV = process.env.NODE_ENV;

const ENV_DEVELOPMENT = NODE_ENV === 'development';
const ENV_PRODUCTION = NODE_ENV === 'production';
const ENV_TEST = NODE_ENV === 'test';

const HOST = process.env.HOST || 'localhost';
const PORT = process.env.PORT || 3000;


//=========================================================
//  CONFIG
//---------------------------------------------------------
const config = {};
module.exports = config;


config.resolve = {
  extensions: ['', '.ts', '.js'],
  modulesDirectories: ['node_modules'],
  root: path.resolve('.')
};

config.module = {
  loaders: [
    {test: /\.ts$/, loader: 'ts', exclude: /node_modules/},
    {test: /\.html$/, loader: 'raw'},
    {test: /\.scss$/, loader: 'raw!postcss!sass', exclude: path.resolve('src/common/styles'), include: path.resolve('src')}
  ]
};

config.plugins = [
  new webpack.DefinePlugin({
    'process.env.NODE_ENV': JSON.stringify(NODE_ENV)
  })
];

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

config.sassLoader = {
  outputStyle: 'compressed',
  precision: 10,
  sourceComments: false
};


//=====================================
//  DEVELOPMENT or PRODUCTION
//-------------------------------------
if (ENV_DEVELOPMENT || ENV_PRODUCTION) {
  config.entry = {
    main: ['./src/main.ts'],
    polyfills: './src/polyfills.ts',
    vendor: './src/vendor.ts'
  };

  config.output = {
    filename: '[name].js',
    path: path.resolve('./target'),
    publicPath: '/'
  };

  config.plugins.push(
    new webpack.optimize.CommonsChunkPlugin({
      name: ['vendor', 'polyfills'],
      minChunks: Infinity
    }),
    new HtmlWebpackPlugin({
      chunkSortMode: 'dependency',
      filename: 'index.html',
      hash: false,
      inject: 'body',
      template: './src/index.html'
    })
  );
}


//=====================================
//  DEVELOPMENT
//-------------------------------------
if (ENV_DEVELOPMENT) {
  config.devtool = 'cheap-module-source-map';

  config.entry.main.unshift(`webpack-dev-server/client?http://${HOST}:${PORT}`);

  config.module.loaders.push(
    {test: /\.scss$/, loader: 'style!css!postcss!sass', include: path.resolve('src/common/styles')}
  );

  config.devServer = {
    contentBase: './src',
    historyApiFallback: true,
    host: HOST,
    outputPath: config.output.path,
    port: PORT,
    publicPath: config.output.publicPath,
    stats: {
      cached: true,
      cachedAssets: true,
      chunks: true,
      chunkModules: false,
      colors: true,
      hash: false,
      reasons: true,
      timings: true,
      version: false
    }
  };
}


//=====================================
//  PRODUCTION
//-------------------------------------
if (ENV_PRODUCTION) {
  config.devtool = 'source-map';

  config.output.filename = '[name].[chunkhash].js';

  config.module.loaders.push(
    {test: /\.scss$/, loader: ExtractTextPlugin.extract('css?-autoprefixer!postcss!sass'), include: path.resolve('src/common/styles')}
  );

  config.plugins.push(
    new WebpackMd5Hash(),
    new ExtractTextPlugin('styles.[contenthash].css'),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin({
      mangle: {
        keep_fnames: true, // eslint-disable-line camelcase
        screw_ie8: true    // eslint-disable-line camelcase
      },
      compress: {
        dead_code: true, // eslint-disable-line camelcase
        screw_ie8: true, // eslint-disable-line camelcase
        unused: true,
        warnings: false
      }
    })
  );
}


//=====================================
//  TEST
//-------------------------------------
if (ENV_TEST) {
  config.devtool = 'inline-source-map';

  config.module.loaders.push(
    {test: /\.scss$/, loader: 'style!css!postcss!sass', include: path.resolve('src/common/styles')}
  );
}

@@ -688,24 +688,573 @@
  %7C%7C 3000;%0A%0A%0A
+//=========================================================%0A//  LOADERS%0A//---------------------------------------------------------%0Aconst loaders = %7B%0A  componentStyles: %7B%0A    test: /%5C.scss$/,%0A    loader: 'raw!postcss!sass',%0A    exclude: path.resolve('src/common/styles')%0A  %7D,%0A  sharedStyles: %7B%0A    test: /%5C.scss$/,%0A    loader: 'style!css!postcss!sass',%0A    include: path.resolve('src/common/styles')%0A  %7D,%0A  html: %7B%0A    test: /%5C.html$/,%0A    loader: 'raw'%0A  %7D,%0A  typescript: %7B%0A    test: /%5C.ts$/,%0A    loader: 'ts',%0A    exclude: /node_modules/%0A  %7D%0A%7D;%0A%0A%0A
 //==========
@@ -1391,12 +1391,8 @@
 g = 
-%7B%7D;%0A
 modu
@@ -1404,22 +1404,18 @@
 ports = 
-config
+%7B%7D
 ;%0A%0A%0Aconf
@@ -1572,225 +1572,73 @@
     
-%7Btest: /%5C.ts$/, loader: 'ts', exclude: /node_modules/%7D,%0A    %7Btest: /%5C.html$/, loader: 'raw'%7D,%0A    %7Btest: /%5C.scss$/, loader: 'raw!postcss!sass', exclude: path.resolve('src/common/styles'), include: path.resolve('src')%7D
+loaders.typescript,%0A    loaders.html,%0A    loaders.componentStyles
 %0A  %5D
@@ -2909,111 +2909,28 @@
 ush(
-%0A    %7Btest: /%5C.scss$/, loader: 'style!css!postcss!sass', include: path.resolve('src/common/styles')%7D%0A  
+loaders.sharedStyles
 );%0A%0A
@@ -3032,96 +3032,18 @@
     
-outputPath: config.output.path,%0A    port: PORT,%0A    publicPath: config.output.publicPath
+port: PORT
 ,%0A  
@@ -3488,38 +3488,38 @@
 le.loaders.push(
+%7B
 %0A    
-%7B
 test: /%5C.scss$/,
@@ -3510,32 +3510,36 @@
 test: /%5C.scss$/,
+%0A   
  loader: Extract
@@ -3591,16 +3591,20 @@
 !sass'),
+%0A   
  include
@@ -3638,20 +3638,20 @@
 styles')
-%7D
 %0A  
+%7D
 );%0A%0A  co
@@ -4361,111 +4361,28 @@
 ush(
-%0A    %7Btest: /%5C.scss$/, loader: 'style!css!postcss!sass', include: path.resolve('src/common/styles')%7D%0A  
+loaders.sharedStyles
 );%0A%7D

clean up config