import { join } from 'path';
import CleanWebpackPlugin from 'clean-webpack-plugin';
import { optimize, LoaderOptionsPlugin } from 'webpack';
import ProgressBar from 'progress-bar-webpack-plugin';
import { green } from 'chalk';
import nodeModules from 'webpack-node-externals';

export default {
    context: __dirname,
    entry: {
        app: './index.js',
    },
    output: {
        filename: '[name].js',
        path: join(__dirname, 'dist'),
    },
    target: 'node',
    devtool: 'source-map',
    node: {
        __filename: false,
        __dirname: true,
        global: true,
    },
    resolve: {
        extensions: [
            '.js',
        ],
    },
    externals: [
        nodeModules(),
    ],
    module: {
        rules: [
            {
                enforce: 'pre',
                test: /\.js$/,
                exclude: ['node_modules', 'bin'],
                loader: 'eslint-loader',
            },
            {
                test: /\.js$/,
                exclude: 'node_modules',
                loader: 'babel-loader',
            },
        ],
    },
    plugins: [
        new LoaderOptionsPlugin({
            minimize: true,
            debug: false,
            options: {
                eslint: {
                    config: '.eslintrc',
                    fix: true,
                    failOnError: false,
                    failOnWarning: false,
                },
            },
        }),
        new CleanWebpackPlugin([
            'dist',
        ], {
            root: __dirname,
        }),
        new ProgressBar({
            format: ':╢:bar╟ :percent :eta',
            incomplete: '░',
            complete: green('░'),
        }),
        new optimize.OccurrenceOrderPlugin(),
    ],
};
