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 Mar 28, 2023
1 parent d8c80e5 commit e6fd704
Show file tree
Hide file tree
Showing 4 changed files with 1,250 additions and 808 deletions.
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ or to test with an existing project you can do the following:
3. Install the development version of frontend-build
``npm i --save-dev @edx/frontend-build@file:./frontend-build``.

Optimization
-----------
To increase optimization by reducing unused CSS, you can set ``USE_PURGECSS=true`` in ``.env`` or as ENV var in the corresponding MFE.
However, note that doing this will increase build time by 30%. It's thus not recommended to use this option during development.
On the other hand, enabling PurgeCSS will increase browser performance for the end user by as much as 20% (as measured by `lighthouse`_). Operators are encouraged to do so for production deployments.

For more information about optimizing MFEs, refer to the `issue #138`_ in the wg-frontend repository.

.. _lighthouse: https://developer.chrome.com/docs/lighthouse/overview/
.. _issue #138: https://github.com/openedx/wg-frontend/issues/138
.. |Build Status| image:: https://api.travis-ci.com/edx/frontend-build.svg?branch=master
:target: https://travis-ci.com/edx/frontend-build
.. |Codecov| image:: https://img.shields.io/codecov/c/github/edx/frontend-build
Expand Down
10 changes: 10 additions & 0 deletions config/webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const PostCssAutoprefixerPlugin = require('autoprefixer');
const PostCssRTLCSS = require('postcss-rtlcss');
const PostCssCustomMediaCSS = require('postcss-custom-media');

// Reduce CSS file size by ~70%
const purgecss = require('@fullhuman/postcss-purgecss');

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

const extraPostCssPlugins = [];
if (process.env.USE_PURGECSS) { // If USE_PURGECSS is set we append it.
extraPostCssPlugins.push(purgecss({
content: ['./**/*.html', './**/*.js', './**/*.jsx', './**/*.ts', './**/*.tsx'],
}));
}
const extraPlugins = [];
if (process.env.ENABLE_NEW_RELIC !== 'false') {
// Enable NewRelic logging only if the account ID is properly defined
Expand Down Expand Up @@ -108,6 +117,7 @@ module.exports = merge(commonConfig, {
PostCssRTLCSS(),
CssNano(),
PostCssCustomMediaCSS(),
...extraPostCssPlugins,
],
},
},
Expand Down
Loading

0 comments on commit e6fd704

Please sign in to comment.