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 = [
        //把入口文件里面的数组打包成verdors.js
        // new ImageminPlugin({
        //     disable: isDev,
        //     test: 'img/**',
        //     pngquant: {
        //         quality: '75-75', 
        //         optimizationLevel: 9
        //     }
        // }),
        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/news.html',
            template: './template/news.html',
            chunks: ['news', 'vendors']
        }),
         new HtmlWebpackPlugin({
            title: '注册/登陆',
            filename: 'html/registered.html',
            template: './template/registered.html',
            chunks: ['registered', '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 '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": "*"
            }
        }
    };
})();