const webpack = require("webpack");
const path = require("path");

//plugins
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

const root = path.join(__dirname, "/app");


module.exports = {
    context: root,
    stats: false,
    resolve: {
        unsafeCache: true,
        alias: {
            "assets": path.join(__dirname, "/assets"),
            "styles": path.join(__dirname, "../styles"),
            "components": path.join(__dirname, "/app/components"),
            "containers": path.join(__dirname, "/app/containers"),
            "lib": path.join(__dirname, "/app/lib"),
            "translations": path.join(__dirname, "/app/translations"),
            "utils": path.join(__dirname, "/app/utils"),
            "constants": path.join(__dirname, "/app/constants")
        },
        extensions: ['.js', '.jsx', '.json', '.scss', '.css']
    },
    entry: {
        "app": ["babel-polyfill", "eventsource-polyfill", path.join(__dirname, "/app/index.js")]
    },
    output: {
        path: path.join(__dirname, "../backend/src/main/resources/resources"),
        filename: "[name].js",
        publicPath: "/",
    },
    module: {
        rules: [ 
            {
                test: /\.js$/, 
                include: root, 
                exclude: /node_modules/,
                use: ["babel-loader"]
            },
            {
                test: /\.css$/,
                exclude: /node_modules/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: ['css-loader', 'postcss-loader']
                })
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: ['css-loader', 'sass-loader' ]
                })
            },          
            {
                test: /\.(png|jpg)$/, 
                use: {
                    loader: "url-loader",
                    options: {
                        limit: 8192
                    }
                }
            },
            {
                test: /\.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: 'fonts/'
                    }
                }]              
            }           
        ]
    },
    plugins: [ 
        //clean previous production version of the application from WAR 
        new CleanWebpackPlugin([ 'resources'], {
            root: path.join(__dirname, "../backend/resources/resources"),
            exclude: [ 'fonts' ],
            allowExternal: true
        }),
        new webpack.LoaderOptionsPlugin({ minimize: true, debug: false }),
        new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production') }),
        new webpack.optimize.UglifyJsPlugin({
            parallel: {
                cache: true,
                workers: 2
            },
            beautify: false,
            mangle: {
              screw_ie8: true,
              keep_fnames: true
            },
            compress: {
              screw_ie8: true
            },
            comments: false
        }),
        /*new webpack.optimize.LimitChunkCountPlugin({
            maxChunks: 1,
        }),*/
        new ExtractTextPlugin("[name].css")
    ]
};