【问题标题】:Change antd variables at runtime with craco使用 craco 在运行时更改 antd 变量
【发布时间】:2021-02-24 06:15:55
【问题描述】:

我必须能够在运行时更改 ant 设计变量(而不是通过无主题文件)。 我发现很多使用 customize-crareact-app-rewire-less 的例子,但似乎没有一个适用于 craco。我必须使用 craco,因为我也在这个项目中使用 tailwindcss

我尝试过的:

  • antd-theme-webpack-plugin:我可以访问window.less.modifyVars,但它似乎什么也没做(调用它不会引发错误,但antd颜色不会改变);
  • antd-theme-switcher:和上面很相似,window.less.modifyVars好像没有效果;
  • antd-theme:我不知道如何在craco.config.js 中添加AntdThemePlugin.loader,我不确定这是不是问题所在,但我无法让它工作。

这是我craco.config.js的当前状态:

const path = require("path");
const CracoAntDesignPlugin = require("craco-antd");

const AntDesignThemePlugin = require("antd-theme-webpack-plugin");
const options = {
  antDir: path.join(__dirname, "./node_modules/antd"),
  stylesDir: path.join(__dirname, "./src"),
  varFile: path.join(__dirname, "./src/styles/variables.less"),
  themeVariables: ["@primary-color"],
  indexFileName: false,
  generateOnce: false,
};
const ThemePlugin = new AntDesignThemePlugin(options);

module.exports = {
  style: {
    postcss: {
      plugins: [require("tailwindcss"), require("autoprefixer")],
    },
  },
  plugins: [
    {
      plugin: CracoAntDesignPlugin,
      options: {
        customizeThemeLessPath: path.join(
          __dirname,
          "./src/styles/variables.less"
        ),
        lessLoaderOptions: {
          lessOptions: {
            javascriptEnabled: true,
            modifyVars: {
              "@primary-color": "#00375B",
            },
          },
        },
      },
    },
    { plugin: ThemePlugin },
  ],
};

在这一点上,我真的要尝试任何可能的解决方案,这个问题真的很耗时。

【问题讨论】:

    标签: reactjs antd craco react-app-rewired customize-cra


    【解决方案1】:

    到今天为止,我已经开始工作了。第二种解决方案 (antd-theme-switcher) 实际上按预期工作。我的错误是我在我的主文件中添加了 antd 默认变量,但是为了让它工作,我必须在我的公共文件夹中添加 color.less 文件(正如 antd-theme-switcher 中的第二步所说),所以window.less.modifyVars 需要处理的文件更少。

    虽然这似乎不是最具性能的方法,但我将尽快在我的项目中使用 antd,因为似乎没有最佳解决方案可以在运行时使用此特定设置更改变量.

    【讨论】:

      猜你喜欢
      • 2019-11-18
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 2021-12-20
      相关资源
      最近更新 更多