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 jumlApi='';
    switch(argv.env.deploy){
        case 'dev':jumlApi="http://sb.jishutao.com";break;
        case 'test':jumlApi="http://uat.jishutao.com";break;
        case 'prod':jumlApi = "http://bm.jishutao.com";break;
        default:
             break;
    }
    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 ExtractTextPlugin({
            filename: "[name].css",
            disable: false,
            allChunks: true
        }),
        //定义全局变量区分本地测试生产做跳转
        new webpack.DefinePlugin({
            jumpUrl: `'${jumlApi}'`
        }),
        //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/certify.html',
            template: './template/template.html',
            chunks: ['user/certify', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '团体用户认证',
            filename: 'user/groupCertify.html',
            template: './template/template.html',
            chunks: ['user/groupCertify', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '用户详情',
            filename: 'user/subscriberDetail.html',
            template: './template/template.html',
            chunks: ['user/subscriberDetail', 'vendors']
        }),
        //user-account
        new HtmlWebpackPlugin({
            title: '用户中心-首页',
            filename: 'user/account/index.html',
            template: './template/template.html',
            chunks: ['user/account/index', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '用户中心-设置',
            filename: 'user/account/set.html',
            template: './template/template.html',
            chunks: ['user/account/set', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '用户中心-科技服务',
            filename: 'user/account/services.html',
            template: './template/template.html',
            chunks: ['user/account/services', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '用户中心-科技服务',
            filename: 'user/account/achievement.html',
            template: './template/template.html',
            chunks: ['user/account/achievement', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '用户中心-科技服务',
            filename: 'user/account/demand.html',
            template: './template/template.html',
            chunks: ['user/account/demand', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-科技服务管理-科技评估信息',
            filename: 'user/account/evaluateInfo.html',
            template: './template/template.html',
            chunks: ['user/account/evaluateInfo', 'vendors']
        }),
        //admin
        new HtmlWebpackPlugin({
            title: '管理员-首页',
            filename: 'admin/index.html',
            template: './template/template.html',
            chunks: ['admin/index', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-登录',
            filename: 'admin/login.html',
            template: './template/template.html',
            chunks: ['admin/login', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-用户管理',
            filename: 'admin/userManage.html',
            template: './template/template.html',
            chunks: ['admin/userManage', 'vendors']
        }),
        //admin-servicesManage
        new HtmlWebpackPlugin({
            title: '管理员-科技服务管理-专利',
            filename: 'admin/servicesManage/contract.html',
            template: './template/template.html',
            chunks: ['admin/servicesManage/contract', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-科技服务管理-专利',
            filename: 'admin/servicesManage/patent.html',
            template: './template/template.html',
            chunks: ['admin/servicesManage/patent', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-科技服务管理-软著',
            filename: 'admin/servicesManage/copyright.html',
            template: './template/template.html',
            chunks: ['admin/servicesManage/copyright', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-科技服务管理-高企',
            filename: 'admin/servicesManage/highTech.html',
            template: './template/template.html',
            chunks: ['admin/servicesManage/highTech', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-科技服务管理-高企信息',
            filename: 'admin/servicesManage/highTechInfo.html',
            template: './template/template.html',
            chunks: ['admin/servicesManage/highTechInfo', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-科技服务管理-科技',
            filename: 'admin/servicesManage/technology.html',
            template: './template/template.html',
            chunks: ['admin/servicesManage/technology', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-科技服务管理-项目单',
            filename: 'admin/servicesManage/projectOrder.html',
            template: './template/template.html',
            chunks: ['admin/servicesManage/projectOrder', 'vendors']
        }),
        //admin-demand
        new HtmlWebpackPlugin({
            title: '管理员-用户管理',
            filename: 'admin/demand.html',
            template: './template/template.html',
            chunks: ['admin/demand', 'vendors']
        }),
        //admin-achievement
        new HtmlWebpackPlugin({
            title: '管理员-用户管理',
            filename: 'admin/achievement.html',
            template: './template/template.html',
            chunks: ['admin/achievement', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-用户管理',
            filename: 'admin/customer.html',
            template: './template/template.html',
            chunks: ['admin/customer', 'vendors']
        }),
        //admin-set
        new HtmlWebpackPlugin({
            title: '管理员-用户管理',
            filename: 'admin/set.html',
            template: './template/template.html',
            chunks: ['admin/set', 'vendors']
        }),
        //admin-idea
        new HtmlWebpackPlugin({
            title: '管理员-财务管理',
            filename: 'admin/idea.html',
            template: './template/template.html',
            chunks: ['admin/idea', 'vendors']
        }),
        //admin-userOrder
        new HtmlWebpackPlugin({
            title: '管理员-用户订单管理',
            filename: 'admin/userOrder.html',
            template: './template/template.html',
            chunks: ['admin/userOrder', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-订单管理',
            filename: 'admin/order.html',
            template: './template/template.html',
            chunks: ['admin/order', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-平台管理',
            filename: 'admin/operate.html',
            template: './template/template.html',
            chunks: ['admin/operate', 'vendors']
        }),//statistics
        new HtmlWebpackPlugin({
            title: '管理员-数据统计',
            filename: 'admin/statistics.html',
            template: './template/template.html',
            chunks: ['admin/statistics', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '管理员-项目管理',
            filename: 'admin/project.html',
            template: './template/template.html',
            chunks: ['admin/project', 'vendors']
        }),
        //portal 门户
        // new HtmlWebpackPlugin({
        //     title: '主页-需求搜索',
        //     filename: 'portal/search/demand.html',
        //     template: './template/template.html',
        //     chunks: ['portal/search/demand', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '主页-科技成果搜索',
        //     filename: 'portal/search/achievement.html',
        //     template: './template/template.html',
        //     chunks: ['portal/search/achievement', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '主页-行家搜索',
        //     filename: 'portal/search/subscriberUser.html',
        //     template: './template/template.html',
        //     chunks: ['portal/search/subscriberUser', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '主页-伙伴搜索',
        //     filename: 'portal/search/subscriberOrg.html',
        //     template: './template/template.html',
        //     chunks: ['portal/search/subscriberOrg', '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://afts.hnzhiming.com';
    let staticHost = 'http://192.168.1.222';
    switch (argv.env.deploy) {
        case 'test':
            staticHost = "http://static.jishutao.com";
            break;
        case 'prod':
            staticHost = "http://s.jishutao.com";
            break;
        default:
            break;
    }
    staticHost = staticHost + '/' + 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',
            },
            extensions: ['.js', '.jsx']
        },
        plugins: plugins,
        // devServer: {
        //     disableHostCheck: true,
        //     allowedHosts: ['bm.kedexinxi.com'],
        //     headers: {
        //         "Access-Control-Allow-Origin": "*"
        //     }
        // }
        devServer: {
            disableHostCheck: true,
            host: '192.168.1.222',
            port: 80,
            allowedHosts: ['127.0.0.1','192.168.0.20','192.168.0.99'],
            headers: {
                "Access-Control-Allow-Origin": "*"
            }
        }
    };
})();