const path = require("path");
const argv = require("yargs").argv;
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const ImageminPlugin = require("imagemin-webpack-plugin").default;
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminPngquant = require('imagemin-pngquant');
const isDev = argv.mode === "development";
const isProd = !isDev;
module.exports = {
entry: {
bundle: "./src/index.js",
},
output: {
path: path.join(__dirname, "prod"),
filename: `./static/js/[name].js${isProd ? "?[contenthash:5]" : ""}`,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: isDev
}
},
"css-loader",
"postcss-loader",
"sass-loader"
]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./src/index.html",
title: isDev ? "Development bundle" : "Production bundle",
filename: "index.html",
inject: true,
minify: {
collapseWhitespace: isProd,
removeComments: isProd,
removeRedundantAttributes: isProd,
removeScriptTypeAttributes: isProd,
removeStyleLinkTypeAttributes: isProd,
useShortDoctype: isProd
}
}),
new MiniCssExtractPlugin({
filename: `./static/css/[name].css${isProd ? "?[contenthash:5]" : ""}`
}),
new CopyWebpackPlugin([
{
from: path.join(__dirname, "src/img"),
to: path.join(__dirname, "prod/static/img"),
},
{
from: path.join(__dirname, "src/fonts"),
to: path.join(__dirname, "prod/static/fonts"),
}
]),
new ImageminPlugin({
disable: isDev,
plugins: [
imageminMozjpeg({
quality: 80
}),
imageminPngquant({
quality: [0.7, 0.9],
speed: 2
})
]
}),
],
devtool: isDev ? "source-map" : "",
devServer: {
contentBase: path.join(__dirname, "./src"),
port: 9000,
open: false,
inline: true
},
};