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 44b6972
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 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
139 changes: 139 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@babel/preset-typescript": "^7.18.6",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.11",
"purgecss-webpack-plugin": "^5.0.0",
"typescript": "^4.8.2"
},
"peerDependencies": {
Expand Down

0 comments on commit 44b6972

Please sign in to comment.