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/index.html',
            chunks: ['index', 'vendors']
        }),
        new HtmlWebpackPlugin({
            title: '支付跳转页面',
            filename: 'html/toPayment.html',
            template: './template/toPayment.html',
            chunks: ['toPayment', '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/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/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']
        }),
    ];
    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://aftts.hnzhiming.com';
    let staticHost = 'http://afts.hnzhiming.com';
    //let staticHost = 'http://127.0.0.1';
    switch (argv.env.deploy) {
        case 'test':
            staticHost = 'http://static.jishutao.com';
            break;
        case 'stage':
            staticHost = 'http://aftts.hnzhiming.com';
            break;
        case 'prod':
            staticHost = 'http://s.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']
        },
        plugins: plugins,
        devServer: {
            disableHostCheck: true,
            //allowedHosts: ['aft.hnzhiming.com', 'afts.hnzhiming.com'],
            headers: {
                "Access-Control-Allow-Origin": "*"
            }
        }
    };
})();