I have a single-page application build with create-react-app. I'm using various npm packages such as antd
, reactstrap
etc. There is a lot of unused CSS and javascript which is slowing my application down. I read about purgeCSS implementation in react to remove them. As per the document I'm unable to find config/webpack.prod.conf.js
in my application and cannot move forward. Can I just use the CLI without adding any configuration? Is there any similar and reliable npm packages to do the same.
I tried implementing the first answer and my config is as follows:
const glob = require("glob-all");
const paths = require("react-scripts/config/paths");
const { override, addPostcssPlugins } = require("customize-cra");
const purgecss = require("@fullhuman/postcss-purgecss")({
content: [
paths.appHtml,
...glob.sync(`${paths.appSrc}/antd/es/button/**/*.js`, { nodir: true }),
...glob.sync(`${paths.appSrc}/antd/es/collapse/**/*.js`, { nodir: true }),
...glob.sync(`${paths.appSrc}/antd/es/dropdown/**/*.js`, { nodir: true }),
...glob.sync(`${paths.appSrc}/antd/es/form/**/*.js`, { nodir: true }),
...glob.sync(`${paths.appSrc}/antd/es/menu/**/*.js`, { nodir: true }),
...glob.sync(`${paths.appSrc}/antd/es/modal/**/*.js`, { nodir: true }),
...glob.sync(`${paths.appSrc}/antd/es/input/**/*.js`, { nodir: true }),
...glob.sync(`${paths.appSrc}/antd/es/tabs/**/*.js`, { nodir: true }),
...glob.sync(`${paths.appSrc}/antd/es/select/**/*.js`, { nodir: true }),
...glob.sync(`${paths.appNodeModules}/antd/es/button/**/*.css`, {
nodir: true,
}),
...glob.sync(`${paths.appNodeModules}/antd/es/collapse/**/*.css`, {
nodir: true,
}),
...glob.sync(`${paths.appNodeModules}/antd/es/dropdown/**/*.css`, {
nodir: true,
}),
...glob.sync(`${paths.appNodeModules}/antd/es/form/**/*.css`, {
nodir: true,
}),
...glob.sync(`${paths.appNodeModules}/antd/es/menu/**/*.css`, {
nodir: true,
}),...glob.sync(`${paths.appNodeModules}/antd/es/modal/**/*.css`, {
nodir: true,
}),
...glob.sync(`${paths.appNodeModules}/antd/es/input/**/*.css`, {
nodir: true,
}),...glob.sync(`${paths.appNodeModules}/antd/es/tabs/**/*.css`, {
nodir: true,
}),
...glob.sync(`${paths.appNodeModules}/antd/es/select/**/*.css`, {
nodir: true,
}),
...glob.sync(`${paths.appNodeModules}/antd/dist/antd.css`, {
nodir: true,
})
],
extractors: [
{
extractor: (content) => content.match(/([a-zA-Z-]+)(?= {)/g) || [],
extensions: ["css"],
},
],
});
module.exports = override(
addPostcssPlugins([
...(process.env.NODE_ENV === "production" ? [purgecss] : []),
])
);
still getting unused javascript as follows: I have not ejected as it was not given in the provided link.
you need to eject the app in order to find webpack files. Refer: https://facebook.github.io/create-react-app/docs/available-scripts#npm-run-eject
Instead you can use postbuild
script in package.json
"scripts": {
...
"postbuild": "purgecss --css build/static/css/*.css --content build/index.html build/static/js/*.js --output build/static/css"
},
Refer: https://purgecss.com/guides/react.html#run-purgecss-cli-in-postbuild