在React中使用sass进行蚂蚁设计,并使用create-react-app。

问题描述 投票:4回答:1

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

我已经用npm安装了antd和node-sass-chokidar,并按照配置进行了操作。所有工作都正常。我可以导入 ant 组件并使用它们,也可以使用 sass。使用watch-css脚本,我的css会自动编译,我可以实时看到修改情况。

现在我想用我的sass文件覆盖蚂蚁设计的少组件,但正如我所说的,我在网上能找到的唯一帮助,也是我知道的唯一一个包是用于webpack项目的。

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

下面是我的一些配置文件。

.babelrc .config-overrides.js

{
  "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 ."
  }
}

谢谢你将来的回答;) !

reactjs sass less create-react-app antd
1个回答
0
投票

我有我的老项目运行与SASS,CRA,和Antd使用下面的配置设置。但如果你去Antd官方网站,他们现在有一个更新的 教程 用于配置Antd与Create React App。但无论如何,他们所解释的是它的用法,而不是sass,所以要在你的项目中使用sass,你必须将两者结合起来。CRA sass设置和 Antd 主题定制,使事情的工作。

下面是我的项目中的一个例子,我使用少插件来更新定制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';
© www.soinside.com 2019 - 2024. All rights reserved.