【问题标题】:Ant Design with sass in React with create-react-appAnt Design with sass in React with create-react-app
【发布时间】:2019-01-16 14:50:41
【问题描述】:

我正在尝试使用 antd-scss-theme-plugin 在我的 React 项目中使用带有 sass 的 Ant 设计,但我在网上只能找到使用 Webpack 的配置。我的项目是用 create-react-app 制作的,我不应该担心 webpack 配置。事实上,我没有任何自己管理的 webpack 配置文件。

我已经使用 npm 安装了 antd 和 node-sass-chokidar 并遵循了配置。所有工作。我可以导入 ant 组件并使用它们,我也可以使用 sass。使用 watch-css 脚本,我的 css 被自动编译,我可以实时看到修改。

现在我想用我的 sass 文件覆盖 ant design 的 less 组件,但正如我所说,我能在网上找到的唯一帮助以及我所知道的唯一包是 webpack 项目。

有没有人使用 antd-scss-theme-plugin 和 create-react-app 或者知道如何处理配置?

这是我的一些配置文件:

.babelrc:

{
  "presets": ["env"],
  "plugins": ["transform-class-properties", "transform-object-rest-spread"]
}

config-overrides.js:

const { injectBabelPlugin } = require('react-app-rewired')

module.exports = function override(config) {
  injectBabelPlugin(
    ['import', { libraryName: 'antd', libraryDirectory: 'es', style: true }],
    config,
  )
  return config
}

package.json:

    {
  "name": "xxx",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "antd": "^3.8.0",
    "antd-scss-theme-plugin": "^1.0.7",
    "connected-react-router": "^4.3.0",
    "history": "^4.7.2",
    "material-icons-react": "^1.0.2",
    "node-sass-chokidar": "^1.3.3",
    "npm-run-all": "^4.1.3",
    "prop-types": "^15.6.2",
    "react": "^16.4.2",
    "react-app-rewired": "^1.5.2",
    "react-dom": "^16.4.2",
    "react-redux": "^5.0.7",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "react-router-prop-types": "^1.0.4",
    "react-scripts": "1.1.4",
    "recompose": "^0.27.1",
    "redux": "^4.0.0",
    "redux-thunk": "^2.3.0"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-import": "^1.8.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "eslint": "^4.19.1",
    "eslint-config-airbnb": "^17.0.0",
    "eslint-config-prettier": "^2.9.0",
    "eslint-plugin-import": "^2.13.0",
    "eslint-plugin-jsx-a11y": "^6.1.1",
    "eslint-plugin-react": "^7.10.0",
    "eslint-plugin-react-redux": "^2.3.0",
    "prettier": "^1.14.0"
  },
  "scripts": {
    "build-css": "node-sass-chokidar src/ -o src/",
    "watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
    "start-js": "react-app-rewired start",
    "start": "npm-run-all -p watch-css start-js",
    "build-js": "react-app-rewired build",
    "build": "npm-run-all build-css build-js",
    "test": "react-app-rewired test --env=jsdom",
    "eject": "react-scripts eject",
    "format": "prettier --write \"src/**/*.js\"",
    "lint": "eslint ."
  }
}

感谢您未来的所有回答;)!

【问题讨论】:

  • 我认为你必须退出create-react-app。你找到解决办法了吗?

标签: reactjs sass less create-react-app antd


【解决方案1】:

我的旧项目使用以下配置设置运行 SASS、CRA 和 Antd。但是如果你去 Antd 官方网站,他们现在有一个更新的tutorial 用于使用 Create React App 配置 Antd。但无论如何,他们解释的是它的用法是less而不是sass,所以要在你的项目中使用sass,你必须结合CRA sass设置和Antd主题定制来使事情正常进行。

这是我项目中的一个示例,其中我使用了 less 插件来更新/自定义 Antd 主题。您应该在项目的根目录中创建一个 custom-override.js 文件并添加这些代码行

const { override, fixBabelImports, addLessLoader } = require('customize-cra');
// you can also use craco instead of customize-cra, 
// which is now the most updated way in their current docs.

module.exports = override(
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: true,
  }),

  addLessLoader({
    javascriptEnabled: true,
    modifyVars: {
      '@primary-color': '#6237a0',
      '@link-color': '#6237a0',
      '@success-color': '#0ea70e',
      '@steps-nav-arrow-color': '#d1cdd6',
    },
  }),
);

那么您只需要像在基于 sass 的 CRA 项目中那样使用 scss 即可。

要使用 Sass,首先安装 node-sass:

npm install node-sass --save
# or
yarn add node-sass

然后只需更新您的代码并将 App.css 重命名为 App.scss 并将其导入 main(app.js) 文件中。你甚至可以通过 sass 导入/定义变量等来扩展代码(所有你可以用 sass 做的事情)。

这是我的app.scss 的快照,我的所有样式都在样式文件夹中定义。

/*
Variable definition 
*/
@import 'styles/variable';



/*
common style import 
*/
@import 'styles/common';



/**
*
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # #       Components styles import    # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
*
* below I do imports for all my routes and components.
*/

@import 'styles/dashboard';
@import 'styles/details';

【讨论】:

  • 你能解释一下什么不适合你吗?您是否面临特定的问题?你能分享一下我可以看到什么问题的代码吗?
【解决方案2】:

我遇到了同样的问题,最终我是如何设法解决的:

  1. 全局安装较少

npm install -g less

  1. 创建一个 main.less 文件并在其中包含 antd.less

  2. 重新声明要覆盖的默认变量

main.less:

// main.less 
@import 'node_modules/antd/dist/antd.less 
@primary-color: #CA6027; // primary color for all components
@link-color: #5C987A; // link color
  1. 将 main.less 编译为 css 并将该文件包含在 CSS 中:

如果你的 main.less 在 src/styles 中,那么:

lessc "./src/styles/main.less ./src/styles/css/antd.css" --js

css 输出将是:

./src/styles/css/antd.css

现在只需将生成的 antd.css 包含在您的 app.js 中。 并且您覆盖的新样式将被应用:) scss 也是如此。

【讨论】:

    猜你喜欢
    • 2021-03-04
    • 1970-01-01
    • 2017-06-07
    • 2018-09-06
    • 2021-09-27
    • 2019-06-20
    • 1970-01-01
    • 2021-08-29
    • 2019-06-16
    相关资源
    最近更新 更多