const fs = require("fs");
const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const FaviconsWebpackPlugin = require("favicons-webpack-plugin");

const PAGE_DIR = path.resolve(__dirname, "src", "pages");

// Collect page configurations
const page_files = fs.readdirSync(PAGE_DIR);

const pages = page_files.map(page => JSON.parse(fs.readFileSync(
    path.resolve(PAGE_DIR, page),
    {encoding: "UTF-8"}
)));

module.exports = {
    entry: {
        index: "./src/index/index.js",
        mail: "./src/mail/mail.js",
        startpage: "./src/startpage/startpage.js",
        music: "./src/music/music.js"
    },
    output: {
        filename: "[name].bundle.js",
        path: path.resolve(__dirname, "dist", "static")
    },
    plugins: [
        new CleanWebpackPlugin("dist"),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        }),
        new MiniCssExtractPlugin(),
        new FaviconsWebpackPlugin({
            logo: path.resolve(__dirname, "src", "lib", "favicon.png"),
            prefix: "../static/",
            title: "tlater.net"
        })
    ].concat(pages.map(page => new HtmlWebpackPlugin(page))),
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /{node_modules}/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ["@babel/preset-env"]
                    }
                }
            },
            {
                test: /.scss$/,
                use: [{
                    loader: MiniCssExtractPlugin.loader,
                    options: {
                        output: {
                            path: path.resolve(__dirname, "dist", "static")
                        }
                    }
                }, {
                    loader: "css-loader"
                }, {
                    loader: "postcss-loader",
                    options: {
                        plugins: () => [
                            require("precss"),
                            require("autoprefixer")
                        ]
                    }
                }, {
                    loader: "sass-loader"
                }]
            },
            {
                test: /.html$/,
                use: [{
                    loader: "html-loader"
                }]
            },
            {
                test: /.pug$/,
                use: [{
                    loader: "pug-loader"
                }]
            },
            {
                test: /\.mp3$/,
                use: [{
                    loader: "file-loader",
                    options: {
                        publicPath: "static/"
                    }
                }]
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                use: [{
                    loader: "file-loader"
                }]
            }
        ]
    }
};