Skip to content

Instantly share code, notes, and snippets.

@intelliapps-io
Created August 1, 2018 01:53
Show Gist options
  • Save intelliapps-io/036761663b2be3025903a3582fd73dd8 to your computer and use it in GitHub Desktop.
Save intelliapps-io/036761663b2be3025903a3582fd73dd8 to your computer and use it in GitHub Desktop.
Webpack 4 compression optimizations
const devMode = process.env.NODE_ENV !== "production" || process.env.NODE_ENV !== "development_prod";
const path = require("path");
const glob = require("glob-all");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const PurgecssPlugin = require("purgecss-webpack-plugin");
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
// const OfflinePlugin = require('offline-plugin');
const PATHS = {
src: path.join(__dirname, "client")
};
module.exports = {
entry: "./client/index.js",
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [devMode ? "style-loader" : MiniCssExtractPlugin.loader, "css-loader", "sass-loader"]
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
"css-loader"
]
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
use: {
loader: "graphql-tag/loader"
}
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: "client/index.html"
}),
new MiniCssExtractPlugin({
filename: devMode ? "[name].css" : "[name].[hash].css",
chunkFilename: devMode ? "[id].css" : "[id].[hash].css"
}),
new OptimizeCssAssetsPlugin({
// assetNameRegExp: /\.optimize\.css$/g,
assetNameRegExp: /\.css$/,
cssProcessor: require("cssnano"),
cssProcessorOptions: { safe: true, discardComments: { removeAll: true } },
canPrint: true
}),
// new PurgecssPlugin({
// paths: glob.sync(`${PATHS.src}/**/*`, { nodir: true })
// }),
new CompressionPlugin({
test: /\.js$|\.css$|\.html$/
})
],
optimization: {
minimizer: [new UglifyJsPlugin({ sourceMap: true, cache: true })],
splitChunks: {
chunks: "all",
cacheGroups: {
styles: {
name: "styles",
test: /\.css$/,
chunks: "all",
enforce: true
}
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment