Jér*_* S. 6 sass less reactjs create-react-app antd
我正在尝试使用 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 的较少组件,但正如我所说,我可以在网上找到的唯一帮助以及我所知道的唯一包是用于 webpack 项目的。
有没有人使用 antd-scss-theme-plugin 和 create-react-app 或者知道如何处理配置?
这是我的一些配置文件:
.babelrc :
{
"presets": ["env"],
"plugins": ["transform-class-properties", "transform-object-rest-spread"]
}
Run Code Online (Sandbox Code Playgroud)
配置覆盖.js:
const { injectBabelPlugin } = require('react-app-rewired')
module.exports = function override(config) {
injectBabelPlugin(
['import', { libraryName: 'antd', libraryDirectory: 'es', style: true }],
config,
)
return config
}
Run Code Online (Sandbox Code Playgroud)
包.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 ."
}
}
Run Code Online (Sandbox Code Playgroud)
感谢您未来的所有答案;)!
小智 1
我使用下面的配置设置使用 SASS、CRA 和 Antd 运行我的旧项目。但是如果你访问 Antd 官方网站,他们现在有一个更新的教程,用于使用 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',
},
}),
);
Run Code Online (Sandbox Code Playgroud)
然后你只需要像在基于 sass 的 CRA 项目中那样以正常方式使用 scss 即可。
要使用 Sass,首先安装 node-sass:
npm install node-sass --save
# or
yarn add node-sass
Run Code Online (Sandbox Code Playgroud)
然后只需更新您的代码并将 App.css 重命名为 App.scss 并将其导入 main(app.js) 文件中。您甚至可以通过执行 sass 导入/定义变量等来扩展代码(您可以使用 sass 执行的所有操作)。
这是我的快照app.scss,其中所有样式都在 styles 文件夹中定义。
/*
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';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4373 次 |
| 最近记录: |