Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfe9c08

Browse files
gillkyleLB
and
LB
authored
docs: add new guide and example site on plugin options (gatsbyjs#21708)
* add example for plugin options and a new guide on them * add example site to guides resources * Apply suggestions from code review Co-Authored-By: LB <[email protected]> * hopefully fix linting errors * Apply suggestions from code review Co-Authored-By: LB <[email protected]> Co-authored-by: LB <[email protected]>
1 parent e42d3ea commit bfe9c08

File tree

13 files changed

+359
-0
lines changed

13 files changed

+359
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: Configuring Plugin Usage with Plugin Options
3+
---
4+
5+
Plugins loaded into a Gatsby site can have options passed in to customize how a plugin operates.
6+
7+
_This guide refers to creating plugins, if you are looking for general information on using options with plugins refer to ["Using a Plugin in Your Site"](/docs/using-a-plugin-in-your-site/). If you are looking for options of a specific plugin, refer to its README._
8+
9+
## Where to access plugin options
10+
11+
A Gatsby plugin with options included makes those options available in the second argument of Gatsby [Node](/docs/node-apis/), [Browser](/docs/browser-apis/), and [SSR APIs](/docs/ssr-apis/). Consider the following `gatsby-config` with a plugin called `gatsby-plugin-console-log`:
12+
13+
```javascript:title=gatsby-config.js
14+
module.exports = {
15+
plugins: [
16+
{
17+
resolve: `gatsby-plugin-console-log`,
18+
options: { optionA: true, optionB: false, message: "Hello world" },
19+
},
20+
],
21+
}
22+
```
23+
24+
With the `optionA`, `optionB`, and `message` options passed into the plugin, the code for `gatsby-plugin-console-log` is able to access the values `true`, `false`, and `Hello world` by their keys.
25+
26+
For example, `gatsby-plugin-console-log` can access the `message` in order to log its value to the console inside of the `onPreInit` API:
27+
28+
```javascript:title=plugins/gatsby-plugin-console-log/gatsby-node.js
29+
exports.onPreInit = (_, pluginOptions) => {
30+
console.log(
31+
`logging: "${pluginOptions.message || `default message`}" to the console` // highlight-line
32+
)
33+
}
34+
```
35+
36+
The code above is called when `gatsby develop` or `gatsby build` is run. It takes the `message` from the `options` object in the config and logs it from `pluginOptions.message` when the `onPreInit` method is called.
37+
38+
The second argument passed into the function is where the options are held.
39+
40+
_Like arguments in any JavaScript function, you can use a different (more specific) name like `themeOptions` if you are building a plugin that will be used as a theme._
41+
42+
## What can be passed in as options
43+
44+
Any JavaScript data type can be passed in as an option.
45+
46+
The following table lists possible options values and an example plugin that makes use of them.
47+
48+
| Data Type | Sample Value | Example Plugin |
49+
| --------- | -------------------------------- | ----------------------------------------------------------------- |
50+
| Boolean | `true` | [`gatsby-plugin-sharp`](/packages/gatsby-plugin-sharp/) |
51+
| String | `/src/data/` | [`gatsby-source-filesystem`](/packages/gatsby-source-filesystem/) |
52+
| Array | `["/about-us/", "/projects/*"]` | [`gatsby-plugin-offline`](/packages/gatsby-plugin-offline/) |
53+
| Object | `{ default: "./src/layout.js" }` | [`gatsby-plugin-mdx`](/packages/gatsby-plugin-mdx/) |
54+
55+
**Note**: Themes (which are a type of plugin) are able to receive options from a site's `gatsby-config` to be used in its `gatsby-config` in order to allow themes to be composed together. This is done by exporting the `gatsby-config` as a function instead of an object. You can see an example of this in the [`gatsby-theme-blog`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-theme-blog) and [`gatsby-theme-blog-core`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-theme-blog-core) repositories. Plugins are not capable of this functionality.
56+
57+
## Additional resources
58+
59+
- [Example Gatsby site using plugin options with a local plugin](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-plugin-options)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Adding Options to Plugins
2+
3+
This is an example repository demonstrating how to add options to a plugin you've authored.
4+
5+
## Running the example
6+
7+
In this directory, be sure to install dependencies for Gatsby:
8+
9+
```sh
10+
npm install
11+
```
12+
13+
Then run `gatsby develop`:
14+
15+
```sh
16+
gatsby develop
17+
```
18+
19+
In your command line output, you should then see the text listed below. This text is showing how the code for each plugin is run sequentially thanks to the Node API implemented.
20+
21+
```sh
22+
$ gatsby develop
23+
success open and validate gatsby-configs - 0.034s
24+
success load plugins - 0.050s
25+
logging: "Hello world" to the console
26+
logging: "default message" to the console
27+
success onPreInit - 0.022s
28+
```
29+
30+
## Understanding what is happening
31+
32+
If you refer to the `gatsby-config` in this example site, you'll see two plugins included in the plugins array. In actuality, it is the same plugin, `gatsby-plugin-console-log`, configured in two different ways. The plugin logs a message to the console when you run `gatsby develop`.
33+
34+
In the first instance, `gatsby-plugin-console-log` is configured with options passed in and will display the custom message.
35+
36+
The second configuration does not include options, so the plugin will log a default message. You can read about adding local plugins [in the Gatsby docs](https://www.gatsbyjs.org/docs/loading-plugins-from-your-local-plugins-folder/).
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
siteMetadata: {
3+
title: `Using plugin options`,
4+
description: `An example Gatsby site using options with a local plugin`,
5+
author: `@gatsbyjs`,
6+
},
7+
plugins: [
8+
// including a plugin from the plugins folder with options
9+
{
10+
resolve: `gatsby-plugin-console-log`,
11+
options: { message: "Hello world" },
12+
},
13+
// including the same plugin without any options, the plugin will use a default message
14+
`gatsby-plugin-console-log`,
15+
],
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "using-plugin-optons",
3+
"description": "An example with the default starter using a plugin with options",
4+
"version": "0.1.0",
5+
"dependencies": {
6+
"gatsby": "^2.18.12",
7+
"prop-types": "^15.7.2",
8+
"react": "^16.12.0",
9+
"react-dom": "^16.12.0",
10+
"react-helmet": "^5.2.1"
11+
},
12+
"devDependencies": {
13+
"prettier": "^1.19.1"
14+
},
15+
"keywords": [
16+
"gatsby"
17+
],
18+
"license": "MIT",
19+
"scripts": {
20+
"build": "gatsby build",
21+
"develop": "gatsby develop",
22+
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
23+
"start": "npm run develop",
24+
"serve": "gatsby serve",
25+
"clean": "gatsby clean",
26+
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
27+
}
28+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
exports.onPreInit = (_, pluginOptions) => {
2+
// uses the plugin options from the gatsby-config and uses it, or a default value
3+
console.log(
4+
`logging: "${pluginOptions.message || `default message`}" to the console`
5+
)
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// noop
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "gatsby-plugin-console-log",
3+
"version": "1.0.0",
4+
"description": "Log stuff in a Gatsby site's build process",
5+
"main": "index.js",
6+
"license": "MIT"
7+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Link } from "gatsby"
2+
import PropTypes from "prop-types"
3+
import React from "react"
4+
5+
const Header = ({ siteTitle }) => (
6+
<header
7+
style={{
8+
background: `rebeccapurple`,
9+
marginBottom: `1.45rem`,
10+
}}
11+
>
12+
<div
13+
style={{
14+
margin: `0 auto`,
15+
maxWidth: 960,
16+
padding: `1.45rem 1.0875rem`,
17+
}}
18+
>
19+
<h1 style={{ margin: 0 }}>
20+
<Link
21+
to="/"
22+
style={{
23+
color: `white`,
24+
textDecoration: `none`,
25+
}}
26+
>
27+
{siteTitle}
28+
</Link>
29+
</h1>
30+
</div>
31+
</header>
32+
)
33+
34+
Header.propTypes = {
35+
siteTitle: PropTypes.string,
36+
}
37+
38+
Header.defaultProps = {
39+
siteTitle: ``,
40+
}
41+
42+
export default Header
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
html {
2+
font-family: sans-serif;
3+
-ms-text-size-adjust: 100%;
4+
-webkit-text-size-adjust: 100%;
5+
}
6+
body {
7+
margin: 0;
8+
-webkit-font-smoothing: antialiased;
9+
-moz-osx-font-smoothing: grayscale;
10+
}
11+
h1 {
12+
font-size: 2em;
13+
margin: 0.67em 0;
14+
}
15+
html {
16+
font: 112.5%/1.45em georgia, serif;
17+
box-sizing: border-box;
18+
overflow-y: scroll;
19+
}
20+
* {
21+
box-sizing: inherit;
22+
}
23+
*:before {
24+
box-sizing: inherit;
25+
}
26+
*:after {
27+
box-sizing: inherit;
28+
}
29+
body {
30+
color: hsla(0, 0%, 0%, 0.8);
31+
font-family: georgia, serif;
32+
font-weight: normal;
33+
word-wrap: break-word;
34+
font-kerning: normal;
35+
-moz-font-feature-settings: "kern", "liga", "clig", "calt";
36+
-ms-font-feature-settings: "kern", "liga", "clig", "calt";
37+
-webkit-font-feature-settings: "kern", "liga", "clig", "calt";
38+
font-feature-settings: "kern", "liga", "clig", "calt";
39+
}
40+
h1 {
41+
margin-left: 0;
42+
margin-right: 0;
43+
margin-top: 0;
44+
padding-bottom: 0;
45+
padding-left: 0;
46+
padding-right: 0;
47+
padding-top: 0;
48+
margin-bottom: 1.45rem;
49+
color: inherit;
50+
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
51+
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
52+
font-weight: bold;
53+
text-rendering: optimizeLegibility;
54+
font-size: 2.25rem;
55+
line-height: 1.1;
56+
}
57+
h2 {
58+
margin-left: 0;
59+
margin-right: 0;
60+
margin-top: 0;
61+
padding-bottom: 0;
62+
padding-left: 0;
63+
padding-right: 0;
64+
padding-top: 0;
65+
margin-bottom: 1.45rem;
66+
color: inherit;
67+
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
68+
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
69+
font-weight: bold;
70+
text-rendering: optimizeLegibility;
71+
font-size: 1.62671rem;
72+
line-height: 1.1;
73+
}
74+
p {
75+
margin-left: 0;
76+
margin-right: 0;
77+
margin-top: 0;
78+
padding-bottom: 0;
79+
padding-left: 0;
80+
padding-right: 0;
81+
padding-top: 0;
82+
margin-bottom: 1.45rem;
83+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Layout component that queries for data
3+
* with Gatsby's useStaticQuery component
4+
*
5+
* See: https://www.gatsbyjs.org/docs/use-static-query/
6+
*/
7+
8+
import React from "react"
9+
import PropTypes from "prop-types"
10+
import { useStaticQuery, graphql } from "gatsby"
11+
12+
import Header from "./header"
13+
import "./layout.css"
14+
15+
const Layout = ({ children }) => {
16+
const data = useStaticQuery(graphql`
17+
query SiteTitleQuery {
18+
site {
19+
siteMetadata {
20+
title
21+
}
22+
}
23+
}
24+
`)
25+
26+
return (
27+
<>
28+
<Header siteTitle={data.site.siteMetadata.title} />
29+
<div
30+
style={{
31+
margin: `0 auto`,
32+
maxWidth: 960,
33+
padding: `0 1.0875rem 1.45rem`,
34+
}}
35+
>
36+
<main>{children}</main>
37+
<footer>
38+
© {new Date().getFullYear()}, Built with
39+
{` `}
40+
<a href="https://www.gatsbyjs.org">Gatsby</a>
41+
</footer>
42+
</div>
43+
</>
44+
)
45+
}
46+
47+
Layout.propTypes = {
48+
children: PropTypes.node.isRequired,
49+
}
50+
51+
export default Layout
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react"
2+
3+
import Layout from "../components/layout"
4+
5+
const NotFoundPage = () => (
6+
<Layout>
7+
<h1>NOT FOUND</h1>
8+
<p>You just hit a route that doesn&#39;t exist... the sadness.</p>
9+
</Layout>
10+
)
11+
12+
export default NotFoundPage
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from "react"
2+
3+
import Layout from "../components/layout"
4+
5+
const IndexPage = () => (
6+
<Layout>
7+
<h1>Hi people</h1>
8+
<p>
9+
This site is to demonstrate using plugin options, you should see output in
10+
the terminal showing a message you passed in to the plugin options, or a
11+
default Hello world message!
12+
</p>
13+
</Layout>
14+
)
15+
16+
export default IndexPage

www/src/data/sidebars/doc-links.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@
259259
link: /docs/creating-a-source-plugin/
260260
- title: Creating a Transformer Plugin
261261
link: /docs/creating-a-transformer-plugin/
262+
- title: Configuring Usage with Plugin Options
263+
link: /docs/configuring-usage-with-plugin-options/
262264
- title: Submit to Plugin Library
263265
link: /contributing/submit-to-plugin-library/
264266
- title: Pixabay Image Source Plugin Tutorial

0 commit comments

Comments
 (0)