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('./entry.js');
const version = require('./package.json').version;
const argv = require('yargs').argv;

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

module.exports = (function () {
    let plugins = [
        new ExtractTextPlugin({
            filename: "[name].css",
            disable: false,
            allChunks: true
        }),
        //把入口文件里面的数组打包成verdors.js
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendors'
        }),
        //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']
        }),
        //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']
        }),
        //user-account-services-patent
        new HtmlWebpackPlugin({
            title: '用户中心-科技服务',
            filename: 'user/account/services.html',
            template: './template/template.html',
            chunks: ['user/account/services', '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/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/technology.html',
            template: './template/template.html',
            chunks: ['admin/servicesManage/technology', 'vendors']
        }),
        //admin-set
        new HtmlWebpackPlugin({
            title: '管理员-用户管理',
            filename: 'admin/set.html',
            template: './template/template.html',
            chunks: ['admin/set', '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://static.goafanti.com';
            break;
        default:
            break;
    }
    staticHost = staticHost + '/' + 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: {
            rules: [{
                    test: /\.jsx?$/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            "presets": ["es2015", "react"],
                            "plugins": [
                                ["antd", {
                                    "style": true
                                }]
                            ]
                        }
                    }
                },
                {
                    test: /\.less$/,
                    use: ExtractTextPlugin.extract({
                        fallback: 'style-loader',
                        use: [{
                            loader: 'css-loader',
                            options: {
                                importLoaders: 1
                            }
                        }, {
                            loader: 'less-loader',
                            options: {
                                modifyVars: theme
                            }
                        }]
                    })
                }, {
                    test: /\.(png|jpg)$/,
                    use: [{
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                            name: '[path][name].[ext]'
                        }
                    }]
                }
            ]
        },
        resolve: {
            extensions: ['.js', '.jsx']
        },
        plugins: plugins
    };
})();