const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const HtmlWebpackPlugin = require('html-webpack-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';

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
        // }),
        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/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']
        }),
        //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']
        }),
        //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']
        }),

        //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';
    switch (argv.env.deploy) {
        case 'stage':
            staticHost = 'http://aftts.hnzhiming.com';
            break;
        case 'prod':
            staticHost = 'http://s.jishutao.com';
            break;
        default:
            break;
    }
    staticHost = staticHost + '/' + version + '/';
    theme['@icon-url'] = '"' + staticHost + 'css/iconfont/iconfont"'
    return {
        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: {
            extensions: ['.js', '.jsx']
        },
        plugins: plugins,
        devServer: {
            disableHostCheck: true,
            allowedHosts: ['aft.hnzhiming.com', 'afts.hnzhiming.com'],
            headers: {
                "Access-Control-Allow-Origin": "*"
            }
        }
    };
})();