1

我有一个使用 create-react-app 构建的单页应用程序。我正在使用各种 npm 包,例如antdreactstrap。有很多未使用的 CSS 和 javascript 正在减慢我的应用程序的速度。我阅读了有关purgeCSS实现的内容以响应删除它们。根据该文件,我无法config/webpack.prod.conf.js在我的申请中找到并且无法继续前进。我可以只使用CLI而不添加任何配置吗?是否有任何类似且可靠的 npm 包来做同样的事情。

我尝试实现第一个答案,我的配置如下:

    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] : []),
    ])
    );

仍然得到未使用的javascript,如下所示: 未使用的 JS 我没有弹出,因为它没有在提供的链接中给出。

4

2 回答 2

3

您需要弹出应用程序才能找到 webpack 文件。参考:https ://facebook.github.io/create-react-app/docs/available-scripts#npm-run-eject

相反,您可以postbuild在 package.json 中使用脚本

"scripts": {
 ... 
  "postbuild": "purgecss --css build/static/css/*.css --content build/index.html build/static/js/*.js --output build/static/css"
},

参考:https ://purgecss.com/guides/react.html#run-purgecss-cli-in-postbuild

于 2021-03-14T08:17:59.480 回答
0

最简单的方法是使用craco本文建议的配置层:

在 CRA 环境中使用 PurgeCSS

于 2021-05-22T15:25:41.200 回答