const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");

module.exports = (env) => {
	const outputDir = path.resolve(__dirname, "build");
	return {
		mode: env.production ? "production" : "development",
		entry: {
			styles: "./src/scss/styles.scss",
		},
		output: {
			path: outputDir,
			clean: true,
			publicPath: "/",
		},
		module: {
			rules: [
				{
					test: /.pug$/i,
					loader: "pug-loader",
				},
				{
					test: /.scss$/i,
					use: [
						MiniCssExtractPlugin.loader,
						"css-loader",
						"sass-loader",
					],
				},
			],
		},
		plugins: [
			new HtmlWebpackPlugin({
				template: "src/index.pug",
				favicon: "src/assets/favicon.ico",
			}),
			new HtmlWebpackPlugin({
				template: "src/post.pug",
				filename: "post.html",
			}),
			new HtmlWebpackPlugin({
				template: "src/404.pug",
				filename: "404.html",
			}),
			new MiniCssExtractPlugin(),
			new CopyPlugin({
				patterns: [
					{ from: "src/assets/*", to: `${outputDir}/assets` },
				],
			}),
			...(env.production ? [] : []),
		],
		optimization: {
			minimizer: [new CssMinimizerPlugin()],
		},
		devtool: "inline-source-map",
		devServer: {
			static: {
				directory: outputDir,
			},
		},
	};
};
