Skip to content

Commit

Permalink
feat: reduce size CSS output file ~70%
Browse files Browse the repository at this point in the history
 The added plugin, reduce the CSS file by removing unused CSS
 The plugin is kinda recommended by bootstrap[^1], reducing
 unused CSS is generally recommended as well enchance web
 perforamce[^2]

 [^1]: https://getbootstrap.com/docs/5.2/customize/optimize/#unused-css
 [^2]: https://web.dev/css-web-vitals/#critical-css

 Related issue: openedx/wg-frontend/issues/138
  • Loading branch information
ghassanmas committed Jan 18, 2023
1 parent ceea036 commit 450c6f1
Show file tree
Hide file tree
Showing 3 changed files with 524 additions and 13 deletions.
15 changes: 15 additions & 0 deletions config/webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const path = require('path');
const PostCssAutoprefixerPlugin = require('autoprefixer');
const PostCssRTLCSS = require('postcss-rtlcss');

// Reduce CSS file size by ~70%
const { PurgeCSSPlugin } = require('purgecss-webpack-plugin');
const glob = require('glob');

const HtmlWebpackNewRelicPlugin = require('../lib/plugins/html-webpack-new-relic-plugin');
const commonConfig = require('./webpack.common.config');
const presets = require('../lib/presets');
Expand All @@ -23,6 +27,10 @@ dotenv.config({
path: path.resolve(process.cwd(), '.env'),
});

const PATHS = {
src: path.join(__dirname, 'src'),
};

const extraPlugins = [];
if (process.env.ENABLE_NEW_RELIC !== 'false') {
// Enable NewRelic logging only if the account ID is properly defined
Expand Down Expand Up @@ -183,6 +191,13 @@ module.exports = merge(commonConfig, {
new MiniCssExtractPlugin({
filename: '[name].[chunkhash].css',
}),

// The recommend usage by official docs
// https://purgecss.com/getting-started.html
new PurgeCSSPlugin({
paths: glob.sync(`${PATHS.src}/**/*`, { nodir: true }),
}),

// Generates an HTML file in the output directory.
new HtmlWebpackPlugin({
inject: true, // Appends script tags linking to the webpack bundles at the end of the body
Expand Down
Loading

0 comments on commit 450c6f1

Please sign in to comment.