Last active
February 23, 2023 21:38
-
-
Save DyadicGit/1f39842d6d7ed48a2fe4327691129942 to your computer and use it in GitHub Desktop.
remove unused css (PurgeCss) in Create React App without ejecting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | |
}); | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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", | |
... | |
}, | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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