'use strict';
var path = require('path');

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

var merge = require('./lib/merge');

var pkg = require('./package.json');

var TARGET = process.env.TARGET;
var ROOT_PATH = path.resolve(__dirname);
var DEMO_DIR = 'demo';
var config = {
    paths: {
        dist: path.join(ROOT_PATH, 'dist'),
        src: path.join(ROOT_PATH, 'src'),
        demo: path.join(ROOT_PATH, DEMO_DIR),
        demoIndex: path.join(ROOT_PATH, DEMO_DIR, '/index'),
    },
    filename: 'boilerplate',
    library: 'Boilerplate',
    demoDirectory: DEMO_DIR,
};

var mergeDemo = merge.bind(null, {
    resolve: {
        extensions: ['', '.js', '.jsx', '.md', '.css', '.png', '.jpg'],
    },
    module: {
        loaders: [
            {
                test: /\.css$/,
                loaders: ['style', 'css'],
            },
            {
                test: /\.md$/,
                loaders: ['html', 'highlight', 'markdown'],
            },
            {
                test: /\.png$/,
                loader: 'url?limit=100000&mimetype=image/png',
                include: config.paths.demo,
            },
            {
                test: /\.jpg$/,
                loader: 'file',
                include: config.paths.demo,
            },
            {
                test: /\.json$/,
                loader: 'json',
            },
        ]
    }
});

if (TARGET === 'dev') {
    module.exports = mergeDemo({
        port: 3000,
        devtool: 'eval',
        entry: [
            'webpack-dev-server/client?http://0.0.0.0:3000',
            'webpack/hot/only-dev-server',
            config.paths.demoIndex,
        ],
        output: {
            path: __dirname,
            filename: 'bundle.js',
            publicPath: '/' + config.demoDirectory + '/'
        },
        plugins: [
            new webpack.DefinePlugin({
                'process.env': {
                    'NODE_ENV': JSON.stringify('development'),
                }
            }),
            new webpack.HotModuleReplacementPlugin(),
            new webpack.NoErrorsPlugin(),
        ],
        module: {
            preLoaders: [
                {
                    test: /\.jsx?$/,
                    loaders: ['eslint', 'jscs'],
                    include: [config.paths.demo, config.paths.src],
                }
            ],
            loaders: [
                {
                    test: /\.jsx?$/,
                    loaders: ['react-hot', 'babel'],
                    include: [config.paths.demo, config.paths.src],
                },
            ]
        }
    });
}

if (TARGET === 'gh-pages') {
    module.exports = mergeDemo({
        entry: [
            config.paths.demoIndex,
        ],
        output: {
            path: './gh-pages',
            filename: 'bundle.js',
        },
        plugins: [
            new webpack.DefinePlugin({
                'process.env': {
                    // This has effect on the react lib size
                    'NODE_ENV': JSON.stringify('production'),
                }
            }),
            new webpack.optimize.DedupePlugin(),
            new webpack.optimize.UglifyJsPlugin({
                compress: {
                    warnings: false
                },
            }),
            new HtmlWebpackPlugin({
                title: pkg.name + ' - ' + pkg.description
            }),
        ],
        module: {
            loaders: [
                {
                    test: /\.jsx?$/,
                    loaders: ['babel'],
                    include: [config.paths.demo, config.paths.src],
                }
            ]
        }
    });
}

var mergeDist = merge.bind(null, {
    devtool: 'source-map',
    output: {
        path: config.paths.dist,
        libraryTarget: 'umd',
        library: config.library,
    },
    entry: config.paths.src,
    externals: {
        react: 'react',
        'react/addons': 'react/addons'
    },
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                loaders: ['babel'],
                include: config.paths.src,
            }
        ]
    }
});

if (TARGET === 'dist') {
    module.exports = mergeDist({
        output: {
            filename: config.filename + '.js',
        },
    });
}

if (TARGET === 'dist-min') {
    module.exports = mergeDist({
        output: {
            filename: config.filename + '.min.js',
        },
        plugins: [
            new webpack.optimize.UglifyJsPlugin({
                compress: {
                    warnings: false
                },
            }),
        ],
    });
}

@@ -2709,33 +2709,33 @@
 %0A        entry: 
-%5B
+%7B
 %0A            con
@@ -2722,32 +2722,37 @@
 y: %7B%0A           
+ app:
  config.paths.de
@@ -2760,33 +2760,158 @@
 oIndex,%0A        
-%5D
+    // tweak this to include your externs unless you load them some other way%0A            vendors: %5B'react/addons'%5D,%0A        %7D
 ,%0A        output
@@ -2968,32 +2968,44 @@
 lename: 'bundle.
+%5Bchunkhash%5D.
 js',%0A        %7D,%0A
@@ -3442,32 +3442,122 @@
             %7D),%0A
+            new webpack.optimize.CommonsChunkPlugin('vendors', 'vendors.%5Bchunkhash%5D.js'),%0A
             new 
@@ -4143,24 +4143,24 @@
 nals: %7B%0A
-
         
 react: '
@@ -4151,16 +4151,74 @@
         
+//// if you are not testing, just react will do%0A        //
 react: '

Split vendor bundle for gh-pages and support caching