'use strict';

const path = require('path');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const env = process.env;
const preLoaders = [];
const plugins = [];
const suffix = env.MIN? '.min.js' : '.js';

const babelLoader = env.ES6? {
    test : /\.js$/,
    loader : 'babel?plugins[]=transform-es2015-modules-commonjs'
} : {
    test : /\.js$/,
    loader : 'babel?presets[]=latest'
};

const rawLoader = { test : /\.txt$/, loader : 'raw-loader' };

const styleLoader = {
    test : /\.css$/,
    loader : 'style-loader!css-loader!postcss-loader'
};

if(env.MIN) {
    if(env.ES6) throw Error('Minification requires transpiling to ES5');
    const uglifyjsOptions = {
        compress : { warnings : false },
        mangle : { keep_fnames : true },
        comments : false
    };
    const uglifyjsPlugin = new webpack.optimize.UglifyJsPlugin(uglifyjsOptions);
    plugins.push(uglifyjsPlugin);
}

if(env.COV) {
    preLoaders.push({
        test : /[^(\.spec)]\.js$/,
        loader : 'babel!babel?presets[]=latest&plugins[]=istanbul',
        exclude: [
            path.resolve('node_modules/')
        ],
    });
}

module.exports = {
    plugins,
    resolve : { modulesDirectories : ['node_modules'] },
    watch : Boolean(env.WATCH)
};

const assign = Object.assign;

switch(env.ENTRY) {
    case 'spec':
        assign(module.exports, {
            entry : {
                spec : ['./lib/index.spec']
            },
            output : {
                path : path.join(__dirname, '/dist/docs'),
                filename : 'build.[name]' + suffix,
                pathinfo : !env.MIN
            },
            module : {
                preLoaders,
                loaders : [babelLoader],
            }
        });
        plugins.push(new CopyWebpackPlugin([{ from : './docs/spec.html' }]));
        break;
    case 'index':
        assign(module.exports, {
            entry : {
                index : ['./docs'],
            },
            output : {
                path : path.join(__dirname, '/dist/docs'),
                filename : 'build.[name]' + suffix,
                pathinfo : !env.MIN
            },
            module : {
                loaders : [babelLoader, rawLoader, styleLoader],
            },
            postcss : () => [autoprefixer],
        });
        plugins.push(new CopyWebpackPlugin([{
            from : './docs/index.html',
            to : '../../index.html'
        }]));
        break;
    case 'dist':
    default:
        assign(module.exports, {
            entry : {
                shim : ['./shim'],
                htmlmodule : ['./lib'],
                'window.htmlmodule' : ['./lib/window.htmlmodule']
            },
            output : {
                path : path.join(__dirname, '/dist'),
                filename : 'dist.[name]' + suffix,
                pathinfo : !env.MIN
            },
            module : {
                loaders : [babelLoader],
            }
        });
        break;
}

@@ -289,16 +289,64 @@
 : '.js';
+%0Aconst nodepath = path.resolve('node_modules/');
 %0A%0Aconst 
@@ -453,17 +453,56 @@
 commonjs
-'
+&compact=false',%0A    exclude: %5Bnodepath%5D
 %0A%7D : %7B%0A 
@@ -556,17 +556,56 @@
 %5D=latest
-'
+&compact=false',%0A    exclude: %5Bnodepath%5D
 %0A%7D;%0A%0Acon
@@ -1275,59 +1275,16 @@
 e: %5B
-%0A            path.resolve('node_modules/')%0A        
+nodepath
 %5D,%0A 

exclude node path