const fs = require('fs')
const path = require('path')
const baseConfig = require('./webpack.base')
const { merge } = require('webpack-merge')
// 清除构建目录
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const components = require('../components.json');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
var nodeExternals = require('webpack-node-externals');

const resolve = name => {
    return path.resolve(__dirname, name)
}

let modules = {}

Object.keys(components).forEach(name => {
    modules[name] = path.join(__dirname, '../', components[name])
})
console.log(modules)

// const config = {
//     mode: 'production',
//     // mode: 'development',
//     // devtool: 'source-map',
//     entry: modules,
//     output: {
//         path: path.resolve(__dirname, '../lib'),
//         filename: '[name].js',
//         chunkFilename: '[id].js',
//         // libraryTarget: 'umd',
//         library: 'vue-element-ui-plus',
//         libraryExport: 'default',
//         libraryTarget: 'commonjs2'
//     },
//     performance: {
//       hints: false
//     },
//     stats: {
//       children: false
//     },
//     externals: {
//         vue: 'vue'
//     },
//     optimization: {
//         minimize: false,
//         // minimizer: [
//         //     new TerserPlugin({
//         //         extractComments: false,
//         //         terserOptions: {
//         //             format: {
//         //                 comments: false,
//         //             },
//         //         },
//         //     }),
//         // ],
//     },
//     plugins: [
//         new ProgressBarPlugin(),
//         new VueLoaderPlugin(),
//         new CleanWebpackPlugin(),
//         // new MiniCssExtractPlugin({
//         //     filename: '../lib/css/[name].css'
//         // })
//     ]
// }
//
// module.exports = merge(config, baseConfig)



module.exports = {
    mode: 'production',
    entry: modules,
    output: {
        path:  path.resolve(__dirname, '../lib'),
        // publicPath: '/dist/',
        filename: '[name].js',
        chunkFilename: '[id].js',
        libraryExport: 'default',
        library: 'vue-element-ui-plus',
        libraryTarget: 'umd'
    },
    resolve: {
        extensions: ['.js', '.vue', '.json'],
        alias: {
            '@': path.join(__dirname, '../'),
            'element-ui': path.join(__dirname, '../')
        },
        modules: ['node_modules']
    },
    externals: [
        {
            vue: 'vue'
        },
        nodeExternals()
    ],
    performance: {
        hints: false
    },
    stats: {
        children: false
    },
    optimization: {
        minimize: true
    },
    module: {
        rules: [
            // babel-loader 解析js
            {
                test: /\.(js|jsx)$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env', '@vue/babel-preset-jsx']
                    }
                }
            },
            // vue-loader 解析 .vue 文件
            {
                test: /.vue$/,
                use: ['vue-loader']
            },
            //
            {
                test: /.(sa|sc|c)ss$/,
                use: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader']
            },
            /**
             * file-loader 解析 js引入图片、字体
             *      import logo from './images/logo.png'
             */
            {
                test: /.(png|jpg|jpeg|gif|webp|PNG)$/,
                loader: 'url-loader',
            },
            {
                test: /.(eot|woff|ttf|svg)$/,
                loader: 'file-loader',
            }

        ],

    },
    plugins: [
        new ProgressBarPlugin(),
        new VueLoaderPlugin()
    ]
};
