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 ImageminPlugin = require('imagemin-webpack-plugin').default;

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 = [
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendors'
        }),
        new ExtractTextPlugin({
            filename: "[name].css",
            disable: false,
            allChunks: true
        }),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        }),
        new HtmlWebpackPlugin({
            title: '首页',
            filename: 'html/index.html',
            template: './template/inded.html',
            chunks: ['index', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '搜索页',
            filename: 'html/index.html',
            template: './template/search.html',
            chunks: ['search', 'vendors']
        }),
        // new HtmlWebpackPlugin({
        //     title: '支付跳转页面',
        //     filename: 'html/toPayment.html',
        //     template: './template/toPayment.html',
        //     chunks: ['toPayment', 'vendors']
        // }),
        new HtmlWebpackPlugin({
            title: '关于公司',
            filename: 'html/aboutUs.html',
            template: './template/aboutUs.html',
            chunks: ['aboutUs', 'vendors']
        }),
        // new HtmlWebpackPlugin({
        //     title: '关于公司APP',
        //     filename: 'html/APPaboutUs.html',
        //     template: './template/APPaboutUs.html',
        //     chunks: ['APPaboutUs', 'vendors']
        // }),
        new HtmlWebpackPlugin({
            title: '联系我们',
            filename: 'html/contactUs.html',
            template: './template/contactUs.html',
            chunks: ['contactUs', 'vendors']
        }),
        // new HtmlWebpackPlugin({
        //     title: '科技服务',
        //     filename: 'html/service.html',
        //     template: './template/service.html',
        //     chunks: ['service', 'vendors']
        // }),
        new HtmlWebpackPlugin({
            title: '注册',
            filename: 'html/register.html',
            template: './template/register.html',
            chunks: ['register', 'vendors']
        }),
        // new HtmlWebpackPlugin({
        //     title: '会员',
        //     filename: 'html/member.html',
        //     template: './template/member.html',
        //     chunks: ['member', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '科技交易',
        //     filename: 'html/technologyTrading.html',
        //     template: './template/technologyTrading.html',
        //     chunks: ['technologyTrading', 'vendors']
        // }),
        new HtmlWebpackPlugin({
            title: '科技智库',
            filename: 'html/thinkTank.html',
            template: './template/thinkTank.html',
            chunks: ['thinkTank', 'vendors']
        }),
        // new HtmlWebpackPlugin({
        //     title: '科技金融',
        //     filename: 'html/financialIndex.html',
        //     template: './template/financialIndex.html',
        //     chunks: ['financialIndex', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '科技交易-技术专利',
        //     filename: 'html/technologyTrading/achievement.html',
        //     template: './template/technologyTrading/achievement.html',
        //     chunks: ['technologyTrading/achievement', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '科技交易-技术需求',
        //     filename: 'html/technologyTrading/demand.html',
        //     template: './template/technologyTrading/demand.html',
        //     chunks: ['technologyTrading/demand', 'vendors']
        // }),
        new HtmlWebpackPlugin({
            title: '科技交易-技术专利详情',
            filename: 'html/technologyTrading/achievementDetail.html',
            template: './template/technologyTrading/achievementDetail.html',
            chunks: ['technologyTrading/achievementDetail', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '科技交易-技术需求详情',
            filename: 'html/technologyTrading/demandDetail.html',
            template: './template/technologyTrading/demandDetail.html',
            chunks: ['technologyTrading/demandDetail', 'vendors']
        }),
        //  new HtmlWebpackPlugin({
        //     title: '在线评估',
        //     filename: 'html/technologyTrading/assessMent.html',
        //     template: './template/technologyTrading/assessMent.html',
        //     chunks: ['technologyTrading/assessMent', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '科技交易-技术成果下单页面',
        //     filename: 'html/technologyTrading/achievementOrder.html',
        //     template: './template/technologyTrading/achievementOrder.html',
        //     chunks: ['technologyTrading/achievementOrder', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '科技交易-技术需求下单页面',
        //     filename: 'html/technologyTrading/demandOrder.html',
        //     template: './template/technologyTrading/demandOrder.html',
        //     chunks: ['technologyTrading/demandOrder', 'vendors']
        // }),
        new HtmlWebpackPlugin({
            title: '智库咨询-智政详情',
            filename: 'html/thinkTank/policyDetail.html',
            template: './template/thinkTank/policyDetail.html',
            chunks: ['thinkTank/policyDetail', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '智库咨询-智政列表',
            filename: 'html/thinkTank/policyList.html',
            template: './template/thinkTank/policyList.html',
            chunks: ['thinkTank/policyList', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '智库咨询-智者详情',
            filename: 'html/thinkTank/thinkerDetail.html',
            template: './template/thinkTank/thinkerDetail.html',
            chunks: ['thinkTank/thinkerDetail', 'vendors']
        }),
        // new HtmlWebpackPlugin({
        //     title: '科技金融-投资机构',
        //     filename: 'html/financial/investmentInstitution.html',
        //     template: './template/financial/investmentInstitution.html',
        //     chunks: ['financial/investmentInstitution', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '科技金融-投资机构',
        //     filename: 'html/financial/investmentDetail.html',
        //     template: './template/financial/investmentDetail.html',
        //     chunks: ['financial/investmentDetail', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '科技服务-高新认定',
        //     filename: 'html/service/highTechCognizance.html',
        //     template: './template/service/highTechCognizance.html',
        //     chunks: ['service/highTechCognizance', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '科技服务-知识产权',
        //     filename: 'html/service/propertyRight.html',
        //     template: './template/service/propertyRight.html',
        //     chunks: ['service/propertyRight', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '科技服务-高新认定',
        //     filename: 'html/service/projectDetails.html',
        //     template: './template/service/projectDetails.html',
        //     chunks: ['service/projectDetails', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '科技服务-服务意向',
        //     filename: 'html/service/purpose.html',
        //     template: './template/service/purpose.html',
        //     chunks: ['service/purpose', 'vendors']
        // }),
        //newsApp 
        new HtmlWebpackPlugin({
            title: '新闻详情',
            filename: 'html/news/newsDetailApp.html',
            template: './template/news/newsDetailApp.html',
            chunks: ['news/newsDetailApp', 'vendors']
        }),
        // new HtmlWebpackPlugin({
        //     title: '高企认定',
        //     filename: 'html/news/appCognizance.html',
        //     template: './template/news/appCognizance.html',
        //     chunks: ['news/appCognizance', 'vendors']
        // }),
        new HtmlWebpackPlugin({
            title: '注册协议',
            filename: 'html/news/agreement.html',
            template: './template/news/agreement.html',
            chunks: ['news/agreement', 'vendors']
        }),
        /*  2018-6 改版后科技需求 */
        new HtmlWebpackPlugin({
            title: '科技需求',
            filename: 'html/newMenu/demand.html',
            template: './template/newMenu/demand.html',
            chunks: ['newMenu/demand', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '科技成果',
            filename: 'html/newMenu/achievement.html',
            template: './template/newMenu/achievement.html',
            chunks: ['newMenu/achievement', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '科技成果列表',
            filename: 'html/newMenu/achievementList.html',
            template: './template/newMenu/achievementList.html',
            chunks: ['newMenu/achievementList', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '科技需求列表',
            filename: 'html/newMenu/demandList.html',
            template: './template/newMenu/demandList.html',
            chunks: ['newMenu/demandList', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '科技服务',
            filename: 'html/newMenu/services.html',
            template: './template/newMenu/services.html',
            chunks: ['newMenu/services', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '科技服务列表',
            filename: 'html/newMenu/serviceList.html',
            template: './template/newMenu/serviceList.html',
            chunks: ['newMenu/serviceList', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '科技服务详情',
            filename: 'html/newMenu/serviceDetails.html',
            template: './template/newMenu/serviceDetails.html',
            chunks: ['newMenu/serviceDetails', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '购物车',
            filename: 'html/newMenu/shopList.html',
            template: './template/newMenu/shopList.html',
            chunks: ['newMenu/shopList', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '专利列表',
            filename: 'html/newMenu/patentList.html',
            template: './template/newMenu/patentList.html',
            chunks: ['newMenu/patentList', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '知识产权交易首页',
            filename: 'html/newMenu/tradingIndex.html',
            template: './template/newMenu/tradingIndex.html',
            chunks: ['newMenu/tradingIndex', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '知识产权申请首页',
            filename: 'html/newMenu/patent.html',
            template: './template/newMenu/patent.html',
            chunks: ['newMenu/patent', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '公司简介',
            filename: 'html/newMenu/companyProfile.html',
            template: './template/newMenu/companyProfile.html',
            chunks: ['newMenu/companyProfile', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '高企认定',
            filename: 'html/newMenu/continueHigh.html',
            template: './template/newMenu/landingPage/continueHigh.html',
            chunks: ['newMenu/continueHigh', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '实用新型专利',
            filename: 'html/newMenu/practicalPatent.html',
            template: './template/newMenu/landingPage/practicalPatent.html',
            chunks: ['newMenu/practicalPatent', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '军工认证',
            filename: 'html/newMenu/militaryProject.html',
            template: './template/newMenu/landingPage/militaryProject.html',
            chunks: ['newMenu/militaryProject', 'vendors']
        }),
        // new HtmlWebpackPlugin({
        //     title: '高企认定App',
        //     filename: 'html/newMenu/continueHighApp.html',
        //     template: './template/newMenu/landingPage/continueHighApp.html',
        //     chunks: ['newMenu/continueHighApp', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '实用新型专利App',
        //     filename: 'html/newMenu/practicalPatentApp.html',
        //     template: './template/newMenu/landingPage/practicalPatentApp.html',
        //     chunks: ['newMenu/practicalPatentApp', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '军工认证App',
        //     filename: 'html/newMenu/militaryProjectApp.html',
        //     template: './template/newMenu/landingPage/militaryProjectApp.html',
        //     chunks: ['newMenu/militaryProjectApp', 'vendors']
        // }),
        new HtmlWebpackPlugin({
            title: '知识产权维权',
            filename: 'html/newMenu/safeguarding.html',
            template: './template/newMenu/safeguarding.html',
            chunks: ['newMenu/safeguarding', 'vendors']
        }),
        //app
        // new HtmlWebpackPlugin({
        //     title: '评估',
        //     filename: 'html/newMenu/app/assessment.html',
        //     template: './template/newMenu/app/assessment.html',
        //     chunks: ['newMenu/app/assessment', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: '托管',
        //     filename: 'html/newMenu/app/trusteeship.html',
        //     template: './template/newMenu/app/trusteeship.html',
        //     chunks: ['newMenu/app/trusteeship', 'vendors']
        // }),
        // new HtmlWebpackPlugin({
        //     title: 'app维权',
        //     filename: 'html/newMenu/app/safeguardApp.html',
        //     template: './template/newMenu/app/safeguardApp.html',
        //     chunks: ['newMenu/app/safeguardApp', 'vendors']
        // })
        new HtmlWebpackPlugin({
            title: '高企自评',
            filename: 'html/evaluation/evaluate.html',
            template: './template/newMenu/evaluate.html',
            chunks: ['evaluation/evaluate', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '知识产权顾问首页',
            filename: 'html/newMenu/adviser.html',
            template: './template/newMenu/adviser.html',
            chunks: ['newMenu/adviser', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '知识产权顾问列表',
            filename: 'html/newMenu/adviserList.html',
            template: './template/newMenu/adviserList.html',
            chunks: ['newMenu/adviserList', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '知识产权顾问详情',
            filename: 'html/newMenu/adviserDetail.html',
            template: './template/newMenu/adviserDetail.html',
            chunks: ['newMenu/adviserDetail', '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.190';
    switch (argv.env.deploy) {
        case 'test':
            staticHost = 'http://statics.jishutao.com';
            break;
        case 'prod':
            staticHost = 'http://ss.jishutao.com';
            break;
        default:
            break;
    }
    staticHost = staticHost + '/portal/' + version + '/';
    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'],
            alias: {
                'js': __dirname + '/src/js',
                'css': __dirname + '/src/css',
                'imgs': __dirname + '/img'
            }
        },
        plugins: plugins,
        devServer: {
            disableHostCheck: true,
            host: '192.168.0.190',
            port: 80,
            allowedHosts: ['192.168.0.190', '192.168.0.99', '192.168.0.20'],
            headers: {
                "Access-Control-Allow-Origin": "*"
            }
        }
    };
})();