问题
This GatsbyJS/antd plugin page (https://github.com/bskimball/gatsby-plugin-antd/issues/2) makes it seem that there is a way to edit ant.design (antd) themes when using GatsbyJS. The code that is provided is
plugins: [
{
resolve: 'gatsby-plugin-antd',
options: {
style: true
}
}
]
But there is no additional information. Where would one make changes to things like the theme primary color (as described: https://ant.design/docs/react/customize-theme). The ant.design page (https://ant.design/docs/react/customize-theme) says to make the primary color change by doing the following:
"theme": {
"primary-color": "#1DA57A",
},
It is not clear in what file such a variable would be set in GatbsyJS.
回答1:
GitHub repo with an example: https://github.com/uciska/gatsby-less-v2. To get Antd working changes must be made to three Gatsby files.
Example 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'
}
}
}
]
}
Example gastby-node.js:
exports.onCreateBabelConfig = ({ actions }) => {
actions.setBabelPlugin({
name: 'babel-plugin-import',
options: {
libraryName: 'antd',
style: true
}
})
}
Example 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"
}
}
回答2:
This configuration worked for me.
I haven't added babel-plugin-import
& haven't modified gatsby-node.js
file.
Here is my package.json
file
{
"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"
}
}
My gatsby-config.js
file
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",
},
},
},
],
}
Look out for the plugins gatsby-plugin-antd
and gatsby-plugin-less
configuration.
That's it and it works.
回答3:
Kudos to @cannin for his original answer. I have updated it with the latest lib name and reorg the text.
To use ANTD with Gatsby and overwrite original styling
- Add dependencies
yarn add babel-plugin-import less-loader antd gatsby-plugin-less
Update Gatsby's setting:
gastby-config.js
add to theplugins
array{ 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 } }) }
Example of using
antd
in component. Thisprimary
button shall display in Gatsby purple#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> )
来源:https://stackoverflow.com/questions/51831582/change-the-theme-of-antd-when-using-gatsbyjs