Skip to content

Instantly share code, notes, and snippets.

@kylerphillips
Created January 15, 2023 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylerphillips/ff8b41e1d45270e197d3d5527101f4b2 to your computer and use it in GitHub Desktop.
Save kylerphillips/ff8b41e1d45270e197d3d5527101f4b2 to your computer and use it in GitHub Desktop.
webpack.config.js
/* eslint-disable no-undef */
const path = require('path')
const webpack = require('webpack'); //to access built-in plugins
const CopyWebpackPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const TerserPlugin = require("terser-webpack-plugin");
const IS_DEVELOPMENT = process.env.NODE_ENV === 'dev'
const dirApp = path.join(__dirname, 'app')
const dirShared= path.join(__dirname, 'shared')
const dirStyles = path.join(__dirname, 'styles')
const dirNode = 'node_modules'
console.log(dirApp, dirStyles)
module.exports = {
entry:[
path.join(dirApp, 'index.js'),
path.join(dirStyles, 'index.scss')
],
resolve: {
modules: [
dirApp,
dirShared,
dirStyles,
dirNode
]
},
plugins: [
new webpack.DefinePlugin({IS_DEVELOPMENT}),
new CleanWebpackPlugin,
new CopyWebpackPlugin({
patterns: [
{
from: './shared',
to: ''
}
]
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
}),
new ImageMinimizerPlugin({
minimizer: {
implementation: ImageMinimizerPlugin.imageminMinify,
options: {
// Lossless optimization with custom option
// Feel free to experiment with options for better result for you
plugins: [
["gifsicle", { interlaced: true }],
["jpegtran", { progressive: true }],
["optipng", { optimizationLevel: 5 }],
],
},
},
})
],
module: {
rules: [
{
test:/\.js$/,
use: {
loader: 'babel-loader'
}
},
{
test:/\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: ''
}
},
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
},
{
loader: 'sass-loader'
}
]
},
{
test: /\.(jpe?g|png|gif|svg|woff2?|fnt|webp)$/,
loader: 'file-loader',
options: {
// eslint-disable-next-line no-unused-vars
name(file){
return '[hash].[ext]'
}
}
},
{
test: /\.(jpe?g|png|gif|svg|webp)$/,
loader: ImageMinimizerPlugin.loader,
options: {
minimizer: {
implementation: ImageMinimizerPlugin.imageminMinify,
},
},
}
],
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment