3

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: Unused JS I have not ejected as it was not given in the provided link.

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

7 Comments

I tried the post-build script without ejecting but still getting unused JS and CSS.
are you using css modules?
In my application, each component has separate CSS files assign to them. The unused JS and CSS is coming from antd and bootstrap unused code.
Also, I'm using "postbuild": "find /path/to/build/static -type f \\( -name \\*.js -o -name \\*.css \\) -exec gzip {} \\; -exec mv {}.gz {} \\;" for text-compression. I can't have two postbuild.
you can && and add new script in there.
|
1

Most simplest way to do it is to use craco configuration layer as suggested in this article:

Using PurgeCSS in CRA Env

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.