Skip to content

Commit 44b6972

Browse files
committed
feat: reduce size CSS output file ~70%
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
1 parent ceea036 commit 44b6972

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

config/webpack.prod.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const path = require('path');
1414
const PostCssAutoprefixerPlugin = require('autoprefixer');
1515
const PostCssRTLCSS = require('postcss-rtlcss');
1616

17+
// Reduce CSS file size by ~70%
18+
const { PurgeCSSPlugin } = require('purgecss-webpack-plugin')
19+
const glob = require('glob')
20+
1721
const HtmlWebpackNewRelicPlugin = require('../lib/plugins/html-webpack-new-relic-plugin');
1822
const commonConfig = require('./webpack.common.config');
1923
const presets = require('../lib/presets');
@@ -23,6 +27,10 @@ dotenv.config({
2327
path: path.resolve(process.cwd(), '.env'),
2428
});
2529

30+
const PATHS = {
31+
src: path.join(__dirname, 'src')
32+
}
33+
2634
const extraPlugins = [];
2735
if (process.env.ENABLE_NEW_RELIC !== 'false') {
2836
// Enable NewRelic logging only if the account ID is properly defined
@@ -183,6 +191,13 @@ module.exports = merge(commonConfig, {
183191
new MiniCssExtractPlugin({
184192
filename: '[name].[chunkhash].css',
185193
}),
194+
195+
// The recommend usage by official docs
196+
// https://purgecss.com/getting-started.html
197+
new PurgeCSSPlugin({
198+
paths: glob.sync(`${PATHS.src}/**/*`, { nodir: true })
199+
}),
200+
186201
// Generates an HTML file in the output directory.
187202
new HtmlWebpackPlugin({
188203
inject: true, // Appends script tags linking to the webpack bundles at the end of the body

package-lock.json

Lines changed: 139 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"@babel/preset-typescript": "^7.18.6",
8282
"@types/react": "^17.0.0",
8383
"@types/react-dom": "^17.0.11",
84+
"purgecss-webpack-plugin": "^5.0.0",
8485
"typescript": "^4.8.2"
8586
},
8687
"peerDependencies": {

0 commit comments

Comments
 (0)