const VueLoaderPlugin = require('vue-loader/lib/plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserJSPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
module.exports = (env, argv) => {
    var isProd = argv.mode === 'production';
    var entry = {};
    if (isProd) {
        entry['libj-vue-comp-code-editing.min'] = './index.js';
        entry['libj-vue-comp-code-editing.default.min'] = './index.scss';
    } else {
        entry['libj-vue-comp-code-editing'] = './index.js';
        entry['libj-vue-comp-code-editing.default'] = './index.scss';
        entry['showcase'] = './showcase/showcase.js';
    }
    return {
        devtool: !isProd ? 'inline-source-map' : 'none',
        watchOptions: {
            aggregateTimeout: 600,
            poll: 1000,
            ignored: ['dist/*', 'node_modules']
        },
        entry: entry,
        output: {
            libraryTarget: 'umd'
        },
        module: {
            rules: [
                {
                    test: /\.vue$/,
                    loader: 'vue-loader'
                },
                {
                    test: /\.js$/,
                    use: {
                        loader: "babel-loader"
                    }
                },
                {
                    test: /\.(sa|sc|c)ss$/,
                    use: [
                        MiniCssExtractPlugin.loader,
                        {
                            loader: 'css-loader',
                            options: {
                                sourceMap: true,
                            }
                        },
                        {
                            loader: 'resolve-url-loader',
                            options: {
                                removeCR: true,
                                debug: !isProd
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true,
                                sourceMapContents: false
                            }
                        },
                    ]
                },
                {
                    test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
                    use: [
                        {
                            loader: 'file-loader',
                            options: {
                                name: '[name].[ext]', 
                                outputPath: 'img',
                            }
                        }
                    ]
                }
            ]
        },
        optimization: {
            minimizer: [
                new TerserJSPlugin({
                    cache: true,
                    parallel: 4,
                    extractComments: 'all',
                    sourceMap: false,
                    terserOptions: {
                        output: {
                            comments: false,
                        },
                    },
                }),
                new OptimizeCSSAssetsPlugin({})
            ],
        },
        plugins: [
            new VueLoaderPlugin(),
            new MiniCssExtractPlugin({
                filename: "[name].css",
                chunkFilename: "[id].css"
            }),
        ]
    };
};