const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin');
const WebpackDevServer = require('webpack-dev-server');
const entries = require('./webpack/entry.config.js');
const version = require('./package.json').version;
const argv = require('yargs').argv;
const dirVars = require('./webpack/dir.config.js');

let theme = {
    '@primary-color': '#58a3ff',
    '@link-color': '#58a3ff'
};
let isWatch = argv.env.watch == 'watch';
let isDev = isWatch || argv.env.deploy == 'dev';

process.traceDeprecation = true;
process.noDeprecation = true;

module.exports = (function () {
    let plugins = [
        //把入口文件里面的数组打包成verdors.js
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendors'
        }),
      /* 配置好Dll */
       new webpack.DllReferencePlugin({
           context: dirVars.staticRootDir, // 指定一个路径作为上下文环境,需要与DllPlugin的context参数保持一致,建议统一设置为项目根目录
           manifest: require('./dll/'+argv.env.deploy+'/manifest.json') // 指定manifest.json
       }),
	   // 把dll文件复制到打包后的文件中
		new CopyWebpackPlugin([
		    {
		      from: path.resolve('./dll/'+argv.env.deploy),
		      to: (__dirname, './dll/'),
		      ignore: ['.*']
		    }
		]),
		// 将 dll.js 插入HTML里
		new HtmlWebpackIncludeAssetsPlugin({
		    assets: [(__dirname, 'dll/dll.js'),(__dirname, 'dll/dll.css')],
		    append: false
        }),
        new webpack.ProvidePlugin({
			$: 'jquery',
			React:'react',
            Mock:'mockjs'
		}),
		//定义全局常量
		// new webpack.DefinePlugin({
		// 	"sub": {
		// 		"name":JSON.stringify("liting")
		// 	}
		// }),
        new ExtractTextPlugin({
            filename: "[name].css",
            disable: false,
            allChunks: true
        }),
        //user
        new HtmlWebpackPlugin({
            title: '用户首页',
            filename: 'user/index.html',
            template: './template/template.html',
            chunks: ['user/index', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '用户登录',
            filename: 'user/login.html',
            template: './template/template.html',
            chunks: ['user/login', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '用户注册',
            filename: 'user/signIn.html',
            template: './template/template.html',
            chunks: ['user/signIn', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '用户中心管理',
            filename: 'user/account/index.html',
            template: './template/template.html',
            chunks: ['user/account/index', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '在线评估',
            filename: 'user/account/evaluateInfo.html',
            template: './template/template.html',
            chunks: ['user/account/evaluateInfo', 'vendors']
        }),
        //管理员模块
        new HtmlWebpackPlugin({
            title: '管理员-登录',
            filename: 'admin/login.html',
            template: './template/template.html',
            chunks: ['admin/login', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-首页',
            filename: 'admin/index.html',
            template: './template/template.html',
            chunks: ['admin/index', 'vendors']
        })
        
    ];
    if (!isDev) {
        //这个使用uglifyJs压缩你的js代码
        plugins.unshift(new webpack.DefinePlugin({
            "process.env": {
                NODE_ENV: JSON.stringify("production")
            }
        }));
        plugins.unshift(new webpack.optimize.UglifyJsPlugin({
            minimize: true,
            compress: {
                warnings: true
            }
        }));
    }

    let staticHost = 'http://192.168.0.188:8088';    
    switch (argv.env.deploy) {
        case 'test':
            staticHost = 'http://statics.jishutao.com';
            break;
        case 'prod':
            staticHost = 'http://ss.jishutao.com';
            break;
        default:
            break;
    }
    staticHost = staticHost + '/client/' + version + '/';
    theme['@icon-url'] = '"' + staticHost + 'css/iconfont/iconfont"'
    return {
    	//devtool:"source-map",
        entry: isWatch ? entries.watch : entries.prod,
        output: {
            path: path.resolve(__dirname, './build/' + argv.env.deploy+'/'+ version),
            filename: '[name].js',
            publicPath: staticHost,
            chunkFilename: 'chunks/[name].[hash:8].js'
        },
        module: require('./webpack/module.config.js')(theme),
        resolve: {
        	alias:{
                '@':__dirname+'/js/component',
                'img':__dirname+'/image',
                'css':__dirname+'/css',
                'js':__dirname+'/js'
            },
            extensions: ['.js', '.jsx']
        },
        plugins: plugins,
        devServer: {
            disableHostCheck: true,
            host: '192.168.0.188',
            port: 8088,
            allowedHosts: ['127.0.0.1','192.168.0.20','192.168.0.99'],
            headers: {
                "Access-Control-Allow-Origin": "*"
            }
        }
    };
})();