const webpack = require('webpack');
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const fs = require('fs')
const packageJson = fs.readFileSync('./package.json')
const version = JSON.parse(packageJson).version || 0
const libraryHost = "http://192.168.0.55:9090/dist"
const guardianHost = "http://192.168.0.55"

module.exports = {
    mode : "development",
    entry: ["core-js/stable", "./src/index.js"],
    devServer: {
        static: path.join(__dirname, 'dist'),
        compress: true,
        port: 8080
    }
,

    plugins: [
        new webpack.DefinePlugin({
            VERSION: JSON.stringify(version),
            LIBRARY_HOST: JSON.stringify(libraryHost),
            GUARDIAN_HOST: JSON.stringify(guardianHost),
        }),
        new MiniCssExtractPlugin({
            filename: "resources/guardian.css",
        })
    ],
    output: {
        path: path.resolve(__dirname + "/dist"),
        filename: "guardian.js",
        library: "Guardian",
        libraryTarget: "umd",
        libraryExport: 'default',
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            [
                                "@babel/env",
                                {
                                    "useBuiltIns" : "entry",
                                    "corejs": 3,
                                    "targets" : {
                                        "browsers": ["last 3 versions", "ie >= 11"],
                                        "node": "current"
                                    }
                                }
                            ],
                            "@babel/preset-react"
                        ],
                        plugins: [
                            "@babel/plugin-proposal-class-properties",
                            ["@babel/plugin-proposal-decorators", {"legacy": true}],
                            "@babel/plugin-transform-runtime",
                        ]
                    },
                }
            },
            {
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, "css-loader"],
            },
            {
                test : /\.(png|svg)$/,
                loader: 'file-loader',
                options: {
                    publicPath: 'resources/img',
                    outputPath: 'resources/img',
                },
            },
        ]
    },
    devtool: 'source-map',
}
