const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CSSSplitWebpackPlugin = require('css-split-webpack-plugin').default;
const theme = require('../theme');
const CopyWebpackPlugin=require('copy-webpack-plugin');

let entryIndex = '';
if (process.env.NODE_ENV === "production") {
    entryIndex = ["babel-polyfill",path.join(__dirname, '../src/index.js')];
    // entryIndex = path.join(__dirname, '../src/index.js');
}
if (process.env.NODE_ENV === "development") {
    entryIndex = ["babel-polyfill",'react-hot-loader/patch', path.join(__dirname, '../src/index.js')];
    // entryIndex = ['react-hot-loader/patch', path.join(__dirname, '../src/index.js')];
}


module.exports = {
    entry: {
        app:entryIndex
    },
    output: {
        filename: 'js/[name].[hash:8].js',
        chunkFilename: 'js/[name]-[id].[hash:8].bundle.js',
        path: path.resolve(__dirname, '../dist')
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: [['env'], ['react'], ['stage-0']],
                            plugins: [
                                'transform-decorators-legacy',
                                "transform-remove-strict-mode",
                                "react-hot-loader/babel",
                                ["import", { libraryName: "antd", style: true }],
                                //"transform-runtime"与'babel-polyfill'一样
                                [
                                    "transform-runtime",
                                    {
                                        "helpers": false,
                                        "polyfill": false,
                                        "regenerator": true,
                                        "moduleName": "babel-runtime"
                                    }
                                ]
                            ]
                        }
                    }
                ],
                exclude: /node_modules/
            },
            {
                test: /\.(css)$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            // {
            //     test: /\.(less)$/,
            //     loader: ['style-loader', 'css-loader', 'less-loader']
            // },
            {
                test: /\.(less)$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: [
                        {
                            loader: 'css-loader'
                            ,
                            options: {
                                url: false
                            }
                        },
                        {
                            loader: 'less-loader',
                            options: {
                                modifyVars: theme,
                                javascriptEnabled: true,
                            }
                        },
                    ]
                })
            },
            {
                test: /\.(png|gif|jpg|jpeg|svg)$/,
                include: [
                    path.resolve(__dirname, '../img')
                ],
                loader: 'file-loader',
                options: {
                    name: 'img/[name].[ext]'
                }
            },
            {
                test:/\.(eot|svg|ttf|woff|woff2)$/,
                include:[
                    path.resolve(__dirname, '../fonts/iconfont')
                ],
                loader:'file-loader',
                options:{
                    name: 'css/fonts/iconfont/[name].[ext]'
                }
            }
        ]
    },
    resolve: {
        alias: {
            src: path.resolve(__dirname, '../src'),
            '@': path.resolve(__dirname, '../src'),
            css: path.resolve(__dirname, '../css'),
            img: path.resolve(__dirname, '../img'),
            common: path.resolve(__dirname, '../src/common'),
            publicConfig:path.resolve(__dirname,'../publicConfig')
        },
        extensions: ['*', '.js', '.css', '.less', '.scss']
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: '邮储银行国结系统',
            template: path.resolve(__dirname, '../index.html')
        }),
        new CopyWebpackPlugin([
            {from: path.resolve(__dirname, '../config.js'), to:path.resolve(__dirname, '../dist/config.js')},
            {from: path.resolve(__dirname, '../fonts'), to:path.resolve(__dirname, '../dist/fonts')},
            {from: path.resolve(__dirname, '../img'), to:path.resolve(__dirname, '../dist/img')},
            {from: path.resolve(__dirname, '../tipUpgrade.html'), to:path.resolve(__dirname, '../dist/tipUpgrade.html')}
        ]),
        new ExtractTextPlugin({
            filename: "[name]-[id].[hash:8].bundle.css",
            allChunks: true
        }),
        //new webpack.optimize.ModuleConcatenationPlugin(),
        new CSSSplitWebpackPlugin({
            size: 4000,
            filename: '[name]-[part].[ext]'
        }),
        // new CleanWebpackPlugin(['dist'], {
        //     root: path.resolve(__dirname,'../'),//根目录
        //     verbose: true,
        //     dry: false
        // })
    ]
};

console.log(path.resolve(__dirname,'../publicConfig'))