使用 GatsbyJS 时更改 Antd 的主题
Change the Theme of Antd when using GatsbyJS
这个 GatsbyJS/antd 插件页面 (https://github.com/bskimball/gatsby-plugin-antd/issues/2) 似乎可以在使用 GatsbyJS 时编辑 ant.design (antd) 主题。提供的代码是
plugins: [
{
resolve: 'gatsby-plugin-antd',
options: {
style: true
}
}
]
但是没有更多信息。在哪里可以更改主题原色(如所述:https://ant.design/docs/react/customize-theme). The ant.design page (https://ant.design/docs/react/customize-theme)之类的东西说要通过执行以下操作来更改原色:
"theme": {
"primary-color": "#1DA57A",
},
目前尚不清楚在 GatbsyJS 中将在哪个文件中设置这样的变量。
GitHub repo 示例:https://github.com/uciska/gatsby-less-v2。要使 Antd 工作,必须对三个 Gatsby 文件进行更改。
示例 gastby-config.js:
module.exports = {
siteMetadata: {
title: 'My site'
},
plugins: [
{
resolve: `gatsby-plugin-less`,
options: {
javascriptEnabled: true,
modifyVars: {
'primary-color': '#da3043',
'font-family': 'Arial',
'layout-body-background': '#66ff79'
}
}
}
]
}
示例 gastby-node.js:
exports.onCreateBabelConfig = ({ actions }) => {
actions.setBabelPlugin({
name: 'babel-plugin-import',
options: {
libraryName: 'antd',
style: true
}
})
}
示例package.json:
{
"name": "gatsby-starter-hello-world",
"description": "Gatsby hello world starter",
"license": "MIT",
"scripts": {
"develop": "gatsby develop",
"build": "gatsby build",
"serve": "gatsby serve"
},
"dependencies": {
"antd": "^3.6.4",
"babel-plugin-import": "^1.8.0",
"gatsby": "next",
"gatsby-plugin-less": "next",
"less": "^3.0.4",
"less-loader": "^4.1.0",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-apollo": "^2.1.11"
}
}
感谢@cannin 的原始回答。我已经用最新的库名称更新了它并重组了文本。
将 ANTD 与 Gatsby 一起使用并覆盖原始样式
- 添加依赖项
yarn add babel-plugin-import less-loader antd gatsby-plugin-less
更新 Gatsby 的设置:
gastby-config.js
添加到 plugins
数组
{
resolve: `gatsby-plugin-less`,
options: {
javascriptEnabled: true,
modifyVars: {
'primary-color': '#663399',
'font-family': 'Arial',
'layout-body-background': '#66ff79'
}
},
},
gastby-node.js
exports.onCreateBabelConfig = ({ actions }) => {
actions.setBabelPlugin({
name: 'babel-plugin-import',
options: {
libraryName: 'antd',
style: true
}
})
}
组件中使用antd
的例子。此 primary
按钮应显示为 Gatsby 紫色 #663399
import React from "react"
import Layout from '../components/layout'
import {Button} from 'antd'
export default () => (
<Layout>
<div>
<h1>ANTD</h1>
<p>Such wow. Very React.</p>
<Button type="primary">Button</Button>
</div>
</Layout>
)
这个配置对我有用。
我没有添加 babel-plugin-import
也没有修改 gatsby-node.js
文件。
这是我的 package.json
文件
{
"name": "web-master",
"private": true,
"description": "Become a fullstack developer",
"version": "1.0",
"author": "Vishal Shetty",
"dependencies": {
"gatsby": "^2.19.7",
"gatsby-image": "^2.2.39",
"gatsby-plugin-manifest": "^2.2.39",
"gatsby-plugin-offline": "^3.0.32",
"gatsby-plugin-react-helmet": "^3.1.21",
"gatsby-plugin-sharp": "^2.4.3",
"gatsby-source-filesystem": "^2.1.46",
"gatsby-transformer-sharp": "^2.3.13",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-helmet": "^5.2.1"
},
"devDependencies": {
"antd": "^3.26.12",
"gatsby-plugin-antd": "^2.1.0",
"gatsby-plugin-less": "^3.0.19",
"less": "^3.11.1",
"less-loader": "^5.0.0",
"prettier": "^1.19.1"
},
"keywords": [
"gatsby"
],
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean"
}
}
我的gatsby-config.js
文件
module.exports = {
siteMetadata: {
title: `WebMaster`,
description: `Become a fullstack developer`,
author: `@gatsbyjs`,
},
plugins: [
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
{
resolve: `gatsby-plugin-manifest`,
options: {
name: `gatsby-starter-default`,
short_name: `starter`,
start_url: `/`,
background_color: `#663399`,
theme_color: `#663399`,
display: `minimal-ui`,
},
},
{
resolve: "gatsby-plugin-antd",
options: {
style: true,
},
},
{
resolve: "gatsby-plugin-less",
options: {
javascriptEnabled: true,
modifyVars: {
"primary-color": "#E4572E",
},
},
},
],
}
注意插件 gatsby-plugin-antd
和 gatsby-plugin-less
配置。
就是这样,而且有效。
我已经尝试了几年前写的所有上述答案,但没有奏效。经过几个小时的研究,我终于能够用 gatsby
修改 antd
默认变量。这是解决方案:
gatsby-config.js
[
...
...
{
resolve: "gatsby-plugin-antd",
options: {
style: true,
},
},
{
resolve: "gatsby-plugin-less",
options: {
lessOptions: {
modifyVars: { //direct child node of lessOptions
"primary-color": "#C53333", //your preferred color
},
javascriptEnabled: true, //direct child node of lessOptions
},
},
},
]
less
和 less-loader
不需要修改默认 antd
变量。
package.json
"dependencies": {
"antd": "^4.12.2",
"gatsby-plugin-antd": "^2.2.0",
"gatsby-plugin-less": "^4.7.0",
}
旁注:查找所有 ant-design 默认变量 here
这个 GatsbyJS/antd 插件页面 (https://github.com/bskimball/gatsby-plugin-antd/issues/2) 似乎可以在使用 GatsbyJS 时编辑 ant.design (antd) 主题。提供的代码是
plugins: [
{
resolve: 'gatsby-plugin-antd',
options: {
style: true
}
}
]
但是没有更多信息。在哪里可以更改主题原色(如所述:https://ant.design/docs/react/customize-theme). The ant.design page (https://ant.design/docs/react/customize-theme)之类的东西说要通过执行以下操作来更改原色:
"theme": {
"primary-color": "#1DA57A",
},
目前尚不清楚在 GatbsyJS 中将在哪个文件中设置这样的变量。
GitHub repo 示例:https://github.com/uciska/gatsby-less-v2。要使 Antd 工作,必须对三个 Gatsby 文件进行更改。
示例 gastby-config.js:
module.exports = {
siteMetadata: {
title: 'My site'
},
plugins: [
{
resolve: `gatsby-plugin-less`,
options: {
javascriptEnabled: true,
modifyVars: {
'primary-color': '#da3043',
'font-family': 'Arial',
'layout-body-background': '#66ff79'
}
}
}
]
}
示例 gastby-node.js:
exports.onCreateBabelConfig = ({ actions }) => {
actions.setBabelPlugin({
name: 'babel-plugin-import',
options: {
libraryName: 'antd',
style: true
}
})
}
示例package.json:
{
"name": "gatsby-starter-hello-world",
"description": "Gatsby hello world starter",
"license": "MIT",
"scripts": {
"develop": "gatsby develop",
"build": "gatsby build",
"serve": "gatsby serve"
},
"dependencies": {
"antd": "^3.6.4",
"babel-plugin-import": "^1.8.0",
"gatsby": "next",
"gatsby-plugin-less": "next",
"less": "^3.0.4",
"less-loader": "^4.1.0",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-apollo": "^2.1.11"
}
}
感谢@cannin 的原始回答。我已经用最新的库名称更新了它并重组了文本。
将 ANTD 与 Gatsby 一起使用并覆盖原始样式
- 添加依赖项
yarn add babel-plugin-import less-loader antd gatsby-plugin-less
更新 Gatsby 的设置:
gastby-config.js
添加到plugins
数组{ resolve: `gatsby-plugin-less`, options: { javascriptEnabled: true, modifyVars: { 'primary-color': '#663399', 'font-family': 'Arial', 'layout-body-background': '#66ff79' } }, },
gastby-node.js
exports.onCreateBabelConfig = ({ actions }) => { actions.setBabelPlugin({ name: 'babel-plugin-import', options: { libraryName: 'antd', style: true } }) }
组件中使用
antd
的例子。此primary
按钮应显示为 Gatsby 紫色#663399
import React from "react" import Layout from '../components/layout' import {Button} from 'antd' export default () => ( <Layout> <div> <h1>ANTD</h1> <p>Such wow. Very React.</p> <Button type="primary">Button</Button> </div> </Layout> )
这个配置对我有用。
我没有添加 babel-plugin-import
也没有修改 gatsby-node.js
文件。
这是我的 package.json
文件
{
"name": "web-master",
"private": true,
"description": "Become a fullstack developer",
"version": "1.0",
"author": "Vishal Shetty",
"dependencies": {
"gatsby": "^2.19.7",
"gatsby-image": "^2.2.39",
"gatsby-plugin-manifest": "^2.2.39",
"gatsby-plugin-offline": "^3.0.32",
"gatsby-plugin-react-helmet": "^3.1.21",
"gatsby-plugin-sharp": "^2.4.3",
"gatsby-source-filesystem": "^2.1.46",
"gatsby-transformer-sharp": "^2.3.13",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-helmet": "^5.2.1"
},
"devDependencies": {
"antd": "^3.26.12",
"gatsby-plugin-antd": "^2.1.0",
"gatsby-plugin-less": "^3.0.19",
"less": "^3.11.1",
"less-loader": "^5.0.0",
"prettier": "^1.19.1"
},
"keywords": [
"gatsby"
],
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean"
}
}
我的gatsby-config.js
文件
module.exports = {
siteMetadata: {
title: `WebMaster`,
description: `Become a fullstack developer`,
author: `@gatsbyjs`,
},
plugins: [
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
{
resolve: `gatsby-plugin-manifest`,
options: {
name: `gatsby-starter-default`,
short_name: `starter`,
start_url: `/`,
background_color: `#663399`,
theme_color: `#663399`,
display: `minimal-ui`,
},
},
{
resolve: "gatsby-plugin-antd",
options: {
style: true,
},
},
{
resolve: "gatsby-plugin-less",
options: {
javascriptEnabled: true,
modifyVars: {
"primary-color": "#E4572E",
},
},
},
],
}
注意插件 gatsby-plugin-antd
和 gatsby-plugin-less
配置。
就是这样,而且有效。
我已经尝试了几年前写的所有上述答案,但没有奏效。经过几个小时的研究,我终于能够用 gatsby
修改 antd
默认变量。这是解决方案:
gatsby-config.js
[
...
...
{
resolve: "gatsby-plugin-antd",
options: {
style: true,
},
},
{
resolve: "gatsby-plugin-less",
options: {
lessOptions: {
modifyVars: { //direct child node of lessOptions
"primary-color": "#C53333", //your preferred color
},
javascriptEnabled: true, //direct child node of lessOptions
},
},
},
]
less
和 less-loader
不需要修改默认 antd
变量。
package.json
"dependencies": {
"antd": "^4.12.2",
"gatsby-plugin-antd": "^2.2.0",
"gatsby-plugin-less": "^4.7.0",
}
旁注:查找所有 ant-design 默认变量 here