question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot be used correctly on ant-design

See original GitHub issue

Cannot be used correctly on ant-design. Will delete the associated style.

Describe the bug

before use:

qq 20190224105316

after use:

qq 20190224105450

code:

import 'antd/dist/antd.min.css';
import { Button } from 'antd';
...
<p>
    <Button type='danger'>buy</Button>
</p>

config: ( use react-app-rewired on create-react-app )

const PurgecssPlugin = require('purgecss-webpack-plugin');
const glob = require('glob-all');

module.exports = function override(config, env) {
    const purgecssPlugin = new PurgecssPlugin({
        paths: ['./public/index.html', ...glob.sync(`./src/*`)],
    });

    if (env !== 'development') {
        config.plugins = [...config.plugins, purgecssPlugin];
    }

    return config;
};

To Reproduce Steps to reproduce the behavior:

  1. Go to ‘…’
  2. Click on ‘…’
  3. Scroll down to ‘…’
  4. See error

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. Windows]
  • Version of Purgecss [latest]
    "glob-all": "^3.1.0",
    "purgecss-webpack-plugin": "^1.4.0",

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Fflorielcommented, Jun 1, 2020

Thanks a lot for the repo @Lizhooh , helped a lot.

This is what I came up with to make it work with ant-design, it is not an ideal solution but it should work.

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}/**/*.js`, { nodir: true }),
    ...glob.sync(`${paths.appNodeModules}/antd/es/button/**/*.css`, {
      nodir: true,
    }),
  ],
  extractors: [
    {
      extractor: (content) => content.match(/([a-zA-Z-]+)(?= {)/g) || [],
      extensions: ["css"],
    },
  ],
});

module.exports = override(
  addPostcssPlugins([
    ...(process.env.NODE_ENV === "production" ? [purgecss] : []),
  ])
);

I essentially added a path to the antd css file that I want to keep. in the example below, button.

...glob.sync(`${paths.appNodeModules}/antd/es/button/**/*.css`,

To keep antd entirely, you could replace by

...glob.sync(`${paths.appNodeModules}/antd/es/**/*.css`,

and wrote an extractor for css file that intend to get the selectors from the file:

  extractors: [
    {
      extractor: (content) => content.match(/([a-zA-Z-]+)(?= {)/g) || [],
      extensions: ["css"],
    },
  ],
2reactions
joeyorlandocommented, Feb 19, 2022

I wonder what this would look like for a Next.js project 🤔

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ant design Form custom controls cannot be displayed correctly
I try to customize a form control named CheckGroup. According to the official document, I define two interfaces, value and onChange. When I...
Read more >
FAQ - Ant Design
Here are the frequently asked questions about Ant Design and antd that you should look up before you ask in the community or...
Read more >
show message validate error input once at a time ant design ...
How can I use AntD FormInstance functions correctly? Asked Sep 18, 2020 • 0 votes 1 answer. QUESTION ANSWERS.
Read more >
ant-design/ant-design-english - Gitter
It feels like your rules prop is not defined correctly. prarjuna ... Hi, I am using https://ant.design/components/date-picker/ in my react application.
Read more >
[Solved]-Ant design Form custom controls cannot be displayed ...
[Solved]-Ant design Form custom controls cannot be displayed correctly-Reactjs. Search. score:0. Accepted answer. <Form layout="vertical" initialValues={{ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found