Skip to content

Instantly share code, notes, and snippets.

@DyadicGit
Last active February 23, 2023 21:38
Show Gist options
  • Save DyadicGit/1f39842d6d7ed48a2fe4327691129942 to your computer and use it in GitHub Desktop.
Save DyadicGit/1f39842d6d7ed48a2fe4327691129942 to your computer and use it in GitHub Desktop.
remove unused css (PurgeCss) in Create React App without ejecting
const { PurgeCSS } = require("purgecss");
const fs = require("fs");
(async () => {
const results = await new PurgeCSS().purge("./purgecss.config.js");
results.forEach((result) => {
const { css, file } = result;
fs.stat(file, (error, originalFileStats) => {
// console.log(originalFileStats);
fs.writeFileSync(file, css);
const newFileStats = fs.statSync(file);
console.log("\x1b[32m", `PurgeCSS saved ${Math.round((originalFileStats.size - newFileStats.size) / 1000)}kb on ${file}`);
});
});
})();
//package.json
{
...
"devDependencies": {
...
"purgecss": "^3.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build && npm run purge-css",
"purge-css": "node ./purge-css",
"build:RCA": "react-scripts build",
...
},
...
}
module.exports = {
content: ['./build/index.html', './build/**/*.js', './build/**/*.html'],
css: ['./build/**/*.css'],
fontFace: true,
keyframes: true,
variables: true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment