'use strict';

var path = require('path')
  , webpack = require('webpack');

const SRC_PATH = path.join(__dirname, 'src')
    , BUILD_PATH = path.join(__dirname, 'static');

// Check if module is vendor, for chunking
const isVendor = (module) => {
  var r = module.userRequest;
  return typeof r === 'string' && r.indexOf('/node_modules/') >= 0;
}

var config = {
  // What file to start at
  entry: [path.join(SRC_PATH, 'main.coffee')],

  // Where to output
  output: {
    path: BUILD_PATH,
    publicPath: '/static/',
    filename: '[name].js',
  },

  // Don't tolerate errors
  bail: true,

  // Coffeelint options
  coffeelint: {
    configFile: './coffeelint.json'
  },

  // Stylint options
  stylint: {
    config: './.stylintrc'
  },

  module: {
    // Linters, etc
    preLoaders: [
      {
        test: /\.coffee$/,
        loader: 'coffee-lint',
        exclude: /node_modules/
      },
      {
        test: /\.styl/,
        loader: 'stylint',
        exclude: /node_modules/
      },
    ],

    // What to load
    loaders: [
      { // Coffeescript (DO NOT MOVE)
        test: /\.coffee$/,
        loaders: ['coffee'],
      },
      { // Stylus
        test: /\.styl$/,
        exclude: /\.u\.styl/,
        loader: 'style!css!stylus',
      },
      { // Reference-counted stylus
        test: /\.u\.styl/,
        loader: 'style/useable!css!stylus',
      },
      { // Plain CSS
        test: /\.css$/,
        loader: 'style!css',
      },
    ],
  },

  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks: isVendor
    }),
  ],
};

// Production
if (process.env.NODE_ENV === 'production') {
  config.plugins.unshift(
    // Production environment variable
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),

    // Deduplicate
    new webpack.optimize.DedupePlugin()
  );
  config.plugins.push(
    // Minify
    new webpack.optimize.UglifyJsPlugin({
      compress: {warnings: false}
    })
  );

// Development
} else {
  // Source maps
  config.devtool = 'eval';

  // -- Hot loading --

  config.entry.unshift('webpack/hot/only-dev-server');

  // loaders[0] is coffee
  config.module.loaders[0].loaders.unshift('react-hot-loader/webpack');
  config.plugins.unshift(new webpack.HotModuleReplacementPlugin());

  config.devServer = {
    // Adjust entry point
    inline: true,

    // Hot reloading
    hot: true,

    // Allow routing
    historyApiFallback: true,

    stats: {
      // Do not show list of hundreds of files included in a bundle
      chunkModules: false,
      colors: true
    },
  }
}

module.exports = config;

@@ -164,18 +164,101 @@
 static')
+%0A    , VENDORS = %5B'node_modules'%5D;%0A%0Aconst VENDOR_RE = new RegExp(VENDORS.join('%7C'))
 ;%0A
-
 %0A// Chec
@@ -394,38 +394,24 @@
 & r.
-indexOf('/node_modules/') %3E= 0
+match(VENDOR_RE)
 ;%0A%7D%0A
@@ -613,24 +613,209 @@
 .js',%0A  %7D,%0A%0A
+  // Where to load modules from%0A  resolveLoader: %7B%0A    modulesDirectories: VENDORS%0A  %7D,%0A%0A  // What extensions to load%0A  resolve: %7B%0A    extensions: %5B'', '.js', '.coffee', '.styl'%5D%0A  %7D,%0A%0A
   // Don't t
@@ -1122,38 +1122,33 @@
    exclude: 
-/node_modules/
+VENDOR_RE
 %0A      %7D,%0A  
@@ -1225,22 +1225,17 @@
 de: 
-/node_modules/
+VENDOR_RE
 %0A   
@@ -1477,29 +1477,37 @@
 ader
+s
 : 
+%5B
 'style
-!css!
+', 'css', '
 stylus'
-,
+%5D
 %0A   
@@ -1585,26 +1585,28 @@
       loader
+s
 : 
+%5B
 'style/useab
@@ -1611,21 +1611,27 @@
 able
-!css!
+', 'css', '
 stylus'
-,
+%5D
 %0A   
@@ -1699,22 +1699,27 @@
 ader
+s
 : 
+%5B
 'style
-!
+', '
 css'
-,
+%5D
 %0A   

make vendors into a variable