const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const optimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const webpack = require('webpack');
const EncodingPlugin = require('webpack-encoding-plugin');
const nodeExternals = require('webpack-node-externals');

let plugins = [];

module.exports = (env, options) => {

    $APP_NAME = JSON.stringify('ui.grid.st');
    $APP_VERSION = JSON.stringify(require('./package.json').version);
    $ENV_MODE = JSON.stringify('DEV');

    let output = {
        filename: 'StGrid.js',
        path: path.resolve(__dirname, 'dist')
    },
    miniCss = {
        filename: 'StGrid.css'
    };

    if (options.mode == 'production') {
        $ENV_MODE = JSON.stringify('PROD');
        output.filename = 'StGrid.min.js';
        miniCss.filename = 'StGrid.min.css';
    }

    plugins.push(
        new EncodingPlugin({
            encoding: 'utf-8'
        }),
        new MiniCssExtractPlugin(miniCss),
        new optimizeCSSAssetsPlugin({}),
        new webpack.optimize.ModuleConcatenationPlugin(),
        new webpack.DefinePlugin({ $APP_NAME, $ENV_MODE, $APP_VERSION })
    );

    return {
        entry: './src/index.js',
        externals: [nodeExternals()],
        output,
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            "presets": [
                                ["@babel/preset-env", {
                                    "targets": "last 2 versions and not dead and > 4%"
                                }]
                            ],
                            "plugins": [
                                [
                                    "@babel/plugin-proposal-decorators", { 
                                        "legacy": true 
                                    }
                                ],
                                [
                                    "@babel/plugin-proposal-class-properties", { 
                                        "loose": true 
                                    }
                                ],
                                [
                                    "angularjs-annotate", { 
                                        "explicitOnly" : true
                                    }
                                ]
                            ]
                        }
                    }
                },
                {
                    test: /\.css$/,
                    use: [
                        {
                            loader: 'style-loader'
                        },
                        {
                            loader: MiniCssExtractPlugin.loader
                        },
                        {
                            loader: 'css-loader'
                        }/*,
                        {
                            loader: 'postcss-loader',
                            options: {
                                ident: 'postcss',
                                plugins: [
                                    require('cssnano')({
                                        preset: ['default', {
                                            discardComments: {
                                                removeAll: true
                                            }
                                        }]
                                    })
                                ]
                            }
                        }*/
                    ]
                },
                {
                    test: /\.html$|\.jsp$/,
                    exclude: /node_modules/,
                    loader: 'html-loader',
                    options: {
                        minimize: true
                    }
                }
            ]
        },
        plugins
    }
}