/* eslint-disable */
import path from 'path';
import webpack from 'webpack';
import process from 'process';

const isProduction = (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'production ');

let config = {

    mode: isProduction ? 'production' : 'development',
    context: path.resolve(__dirname, '.'),
    watch: true,

    entry: [
        '@babel/polyfill',
        path.resolve(__dirname, '../src/assets/js/main.js')
    ],

    output: {
        filename: './js/[name].js',
        path: path.resolve(__dirname, '../dist/assets'),
        chunkFilename: './js/[name].js',
    },

    resolve: {
        modules: [
            path.resolve('node_modules'),
        ],
        extensions: ['.js', '.json'],
    },

    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            }
        ]
    }
};

const javascripts = () => {

    return new Promise(resolve => webpack(config, (err) => {
        if (err) console.log('Webpack', err);

        resolve();
    }));
};

module.exports = { config, javascripts };
