'use strict';
const path = require('path');
const utils = require('./utils');
const webpack = require('webpack');
const config = require('../config');
const merge = require('webpack-merge');
const baseWebpackConfig = require('./webpack.base.conf');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const es3ifyPlugin=require('es3ify-webpack-plugin');
const env = config.build.env;
const cssSourceMap=config.build.cssSourceMap;


const webpackConfig = merge(baseWebpackConfig, {
    devtool: config.build.productionSourceMap ? '#source-map' : false,
    output: {
        path: config.build.assetsRoot,
        publicPath: "./",
        filename: utils.assetsPath('js/[name].[chunkhash].js'),
        chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
    },
    resolve: {
        alias: {
          'react': 'anujs/dist/ReactIE',
          'react-dom': 'anujs/dist/ReactIE',
          'prop-types': 'anujs/lib/ReactPropTypes'
        }
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader?sourceMap',
                    use: [{
                        loader: 'css-loader',
                        options: {
                            minimize: false,
                            sourceMap: cssSourceMap,
                            localIdentName: '[path][hash:base64:5]',
                            module:true
                        }
                    }]
                })

            },
            {
                test: /\.less/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader?sourceMap',
                    use: [{
                        loader: 'css-loader',
                        options: {
                            minimize: false,
                            sourceMap: cssSourceMap,
                            importLoaders: 1,
                            localIdentName: '[path][hash:base64:5]',
                            module:true
                        }
                    },
                        {
                            loader: 'less-loader',
                            options: {
                                sourceMap: cssSourceMap
                            }
                        }
                    ]
                })

            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': env
        }),
        new UglifyJsPlugin({
            sourceMap: true,
            uglifyOptions:{
                ie8:true,
                ecma:5,
                warnings: false,
                mangle:{
                    reserved:['export','default','$','exports','import','module']
                },
                compress:{
                    // booleans:false,
                    // comparisons:false,
                    // computed_props:false,
                    // conditionals:false,
                    // evaluate:false,
                    // hoist_props:false,
                    // if_return:false,
                    // inline:false,
                    // join_vars:false,
                    // loops:false,
                    // negate_iife:false,
                    // properties:false,
                    // pure_getters:true,
                    reduce_funcs:false,//确定影响IE8的报错的配置
                    // reduce_vars:false,
                    // sequences:false,
                },
                output:{
                    comments:false
                }
            }
        }),
        new HtmlWebpackPlugin({
            filename: config.build.index,
            template: 'index.html',
            inject: 'body',
            minify: {
                removeComments: true,
                collapseWhitespace: true,
                removeAttributeQuotes: true
            },
            chunksSortMode: 'dependency',
        }),
        new ExtractTextPlugin({
            filename: utils.assetsPath('css/[name].[contenthash].css')
        }),
        new OptimizeCSSPlugin({
            cssProcessorOptions: {
                safe: true
            }
        }),
        // new es3ifyPlugin(),
        new webpack.HashedModuleIdsPlugin(),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks: function (module) {
                return (
                    module.resource &&
                    /\.js$/.test(module.resource) &&
                    module.resource.indexOf(
                        path.join(__dirname, '../node_modules')
                    ) === 0
                )
            }
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'manifest',
            chunks: ['vendor']
        }),
        new CopyWebpackPlugin([
            {
                from: path.join(__dirname, "../static/"),
                to:path.join( __dirname , "../app/public/static")
            }
          ])
    ]
});

module.exports = webpackConfig;