Skip to content

Commit 603e376

Browse files
gillkyleLBgatsbybot
authored
docs: add starter for plugins with new plugin recipes (#22258)
* add starter for plugins * add recipes for using the plugin starter and installing a plugin * Apply suggestions from code review Co-Authored-By: LB <[email protected]> Co-authored-by: LB <[email protected]> Co-authored-by: gatsbybot <[email protected]>
1 parent 1072258 commit 603e376

File tree

12 files changed

+352
-14
lines changed

12 files changed

+352
-14
lines changed

docs/docs/creating-a-local-plugin.md

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ Then the plugin can begin to hook into Gatsby through [Node](/docs/node-apis/) a
3636

3737
Your plugin doesn't have to be in your project in order to be tested or worked on. If you'd like to [decouple](/docs/glossary#decoupled) your plugin from your site you can follow one of the methods described below. This is a useful thing to do if you want to publish the plugin as its own package, or test/develop a forked version of a community authored plugin.
3838

39+
To get started developing a plugin outside of your site's root folder, you can quickly generate one using `gatsby new` with the [starter for plugins](https://github.com/gatsbyjs/gatsby/tree/master/starters/gatsby-starter-plugin):
40+
41+
```shell
42+
gatsby new gatsby-plugin-foo https://github.com/gatsbyjs/gatsby-starter-plugin
43+
```
44+
3945
### Using `require.resolve` and a filepath
4046

4147
Including a `plugins` folder is not the only way to reference a local plugin. Alternatively, you can include a plugin in your `gatsby-config.js` file by directly referencing its path (relative to the `gatsby-config.js` file) with `require`.

docs/docs/recipes.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,28 @@ There are so many ways to add styles to your website; Gatsby supports almost eve
5555
- [Using Google Fonts](/docs/recipes/styling-css#using-google-fonts)
5656
- [Using Font Awesome](/docs/recipes/styling-css#using-fontawesome)
5757

58-
## [3. Working with starters](/docs/recipes/working-with-starters)
58+
## [3. Working with plugins](/docs/recipes/working-with-plugins)
59+
60+
[Plugins](/docs/plugins/) are Node.js packages that implement Gatsby APIs that are maintained officially, or by the community.
61+
62+
- [Using a plugin](/docs/recipes/working-with-plugins#using-a-plugin)
63+
- [Creating a new plugin using a plugin starter](/docs/recipes/working-with-plugins#creating-a-new-plugin-using-a-plugin-starter)
64+
65+
## [4. Working with starters](/docs/recipes/working-with-starters)
5966

6067
[Starters](/docs/starters/) are boilerplate Gatsby sites maintained officially, or by the community.
6168

6269
- [Using a starter](/docs/recipes/working-with-starters#using-a-starter)
6370

64-
## [4. Working with themes](/docs/recipes/working-with-themes)
71+
## [5. Working with themes](/docs/recipes/working-with-themes)
6572

6673
A Gatsby theme lets you centralize the look-and-feel of your site. You can seamlessly update a theme, compose themes together, and even swap out one compatible theme for another.
6774

6875
- [Creating a new site using a theme](/docs/recipes/working-with-themes#creating-a-new-site-using-a-theme)
6976
- [Creating a new site using a theme starter](/docs/recipes/working-with-themes#creating-a-new-site-using-a-theme-starter)
7077
- [Building a new theme](/docs/recipes/working-with-themes#building-a-new-theme)
7178

72-
## [5. Sourcing data](/docs/recipes/sourcing-data)
79+
## [6. Sourcing data](/docs/recipes/sourcing-data)
7380

7481
Pull data from multiple locations, like the filesystem or database, into your Gatsby site.
7582

@@ -80,7 +87,7 @@ Pull data from multiple locations, like the filesystem or database, into your Ga
8087
- [Pulling data from an external source and creating pages without GraphQL](/docs/recipes/sourcing-data#pulling-data-from-an-external-source-and-creating-pages-without-graphql)
8188
- [Sourcing content from Drupal](/docs/recipes/sourcing-data#sourcing-content-from-drupal)
8289

83-
## [6. Querying data](/docs/recipes/querying-data)
90+
## [7. Querying data](/docs/recipes/querying-data)
8491

8592
Gatsby lets you access your data across all sources using a single GraphQL interface.
8693

@@ -94,7 +101,7 @@ Gatsby lets you access your data across all sources using a single GraphQL inter
94101
- [GraphQL Query Fragments](/docs/recipes/querying-data#graphql-query-fragments)
95102
- [Querying data client-side with fetch](/docs/recipes/querying-data#querying-data-client-side-with-fetch)
96103

97-
## [7. Working with images](/docs/recipes/working-with-images)
104+
## [8. Working with images](/docs/recipes/working-with-images)
98105

99106
Access images as static resources, or automate the process of optimizing them through powerful plugins.
100107

@@ -103,14 +110,14 @@ Access images as static resources, or automate the process of optimizing them th
103110
- [Optimizing and querying local images with gatsby-image](/docs/recipes/working-with-images#optimizing-and-querying-local-images-with-gatsby-image)
104111
- [Optimizing and querying images in post frontmatter with gatsby-image](/docs/recipes/working-with-images#optimizing-and-querying-images-in-post-frontmatter-with-gatsby-image)
105112

106-
## [8. Transforming data](/docs/recipes/transforming-data)
113+
## [9. Transforming data](/docs/recipes/transforming-data)
107114

108115
Transforming data in Gatsby is plugin-driven. Transformer plugins take data fetched using source plugins, and process it into something more usable (e.g. JSON into JavaScript objects, and more).
109116

110117
- [Transforming Markdown into HTML](/docs/recipes/transforming-data#transforming-markdown-into-html)
111118
- [Transforming images into grayscale using GraphQL](/docs/recipes/transforming-data#transforming-images-into-grayscale-using-graphql)
112119

113-
## [9. Deploying your site](/docs/recipes/deploying-your-site)
120+
## [10. Deploying your site](/docs/recipes/deploying-your-site)
114121

115122
Showtime. Once you are happy with your site, you are ready to go live with it!
116123

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: "Recipes: Working with Plugins"
3+
tableOfContentsDepth: 1
4+
---
5+
6+
A [Gatsby plugin](/docs/what-is-a-plugin/) abstracts Gatsby APIs into an installable package. This means that modular chunks of Gatsby functionality aren’t directly written into your project, but rather versioned, centrally managed, and installed as a dependency. You can add external data, transform data, add third-party services (e.g. Google Analytics, Stripe), and more.
7+
8+
## Using a plugin
9+
10+
Found a plugin you'd like to use in your project? Awesome! You can configure it for use by following the steps below. This recipe uses the [`gatsby-source-filesystem` plugin](/plugins/gatsby-source-filesystem) as an example.
11+
12+
> If you'd like to take a look at available plugins, check out the [plugin library](/plugins).
13+
14+
### Prerequisites
15+
16+
- An existing [Gatsby site](/docs/quick-start/) with a `gatsby-config.js` file
17+
18+
### Directions
19+
20+
1. Install the `gatsby-source-filesystem` plugin by running the following command:
21+
22+
```shell
23+
npm install gatsby-source-filesystem
24+
```
25+
26+
2. Add the plugin to your `gatsby.config.js`, and set any options it needs, the filesystem source plugin takes a `name` and `path` as options:
27+
28+
```javascript:title=gatsby-config.js
29+
module.exports = {
30+
plugins: [
31+
// highlight-start
32+
{
33+
resolve: `gatsby-source-filesystem`,
34+
options: {
35+
name: `images`,
36+
path: `${__dirname}/src/images`,
37+
},
38+
},
39+
// highlight-end
40+
],
41+
}
42+
```
43+
44+
_The instructions found in the README of the plugin you're using can you help you determine specifics about what configurations (if any) it requires. For this example to have an effect in your own site, you would need to create the `src/images` folder and place files inside of it._
45+
46+
3. Run `gatsby develop`, your plugin should run as your site builds.
47+
48+
### Additional resources
49+
50+
- Learn more about configuring options or using default options in the [Using a Plugin in Your Site](/docs/using-a-plugin-in-your-site/) guide.
51+
- See an example Gatsby site using this configuration in [the repo for the default Gatsby starter](https://github.com/gatsbyjs/gatsby-starter-default/blob/master/gatsby-config.js).
52+
53+
## Creating a new plugin using a plugin starter
54+
55+
If you want to create your own plugin you can get started with the Gatsby plugin starter.
56+
57+
### Prerequisites
58+
59+
- An existing [Gatsby site](/docs/quick-start/) with a `gatsby-config.js` file
60+
61+
### Directions
62+
63+
1. _Outside_ of your Gatsby site, generate a new plugin based on the starter using the `gatsby new` command:
64+
65+
```shell
66+
gatsby new my-plugin https://github.com/gatsbyjs/gatsby-starter-plugin
67+
```
68+
69+
The directory structure should look something like this:
70+
71+
```
72+
gatsby-site
73+
└── gatsby-config.js
74+
└── src
75+
├── components
76+
└── pages
77+
my-plugin
78+
├── gatsby-browser.js
79+
├── gatsby-node.js
80+
├── gatsby-ssr.js
81+
├── index.js
82+
└── package.json
83+
```
84+
85+
2. Add the plugin to your site's `gatsby-config.js`, linking it to your local plugin's root folder with `require.resolve`. The path (or name of the plugin) should be to the directory name you used when generating the plugin:
86+
87+
```javascript:title=gatsby-site/gatsby-config.js
88+
module.exports = {
89+
plugins: [
90+
require.resolve(`../my-plugin), // highlight-line
91+
],
92+
}
93+
```
94+
95+
3. Run `gatsby develop`. To verify the plugin starter loaded correctly in your site it will log a message to the console saying it "Loaded" before the `onPreInit` step finishes.
96+
97+
4. Now you can implement [browser](/docs/browser-apis/), [server-side rendering](/docs/ssr-apis/), or [node APIs](/docs/node-apis/) and your site will run them each time it loads your plugin!
98+
99+
### Additional resources
100+
101+
- Read about creating and loading your own plugins in development in the [Creating a Local Plugin](/docs/creating-a-local-plugin/) guide

docs/docs/recipes/working-with-themes.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A [Gatsby theme](/docs/themes/what-are-gatsby-themes) abstracts Gatsby configura
99

1010
Found a theme you'd like to use on your project? Awesome! You can configure it for use by following the steps below.
1111

12-
> If you'd like to take a look at more theme options, check out [list of themes](https://www.npmjs.com/search?q=gatsby-theme).
12+
> If you'd like to take a look at more theme options, check out this [list of themes](/plugins?=gatsby-theme).
1313
1414
### Prerequisites
1515

@@ -32,11 +32,11 @@ cd {your-project-name}
3232
npm install gatsby-theme-blog
3333
```
3434

35-
3. Add theme to `gatsby.config.js`
35+
3. Add theme to `gatsby-config.js`
3636

3737
Follow the instructions found in the README of the theme you're using to determine what configuration it requires.
3838

39-
```shell
39+
```javascript:title=gatsby-config.js
4040
module.exports = {
4141
plugins: [
4242
{
@@ -55,15 +55,15 @@ module.exports = {
5555
}
5656
```
5757

58-
4. Run `gatsby develop`, the theme should be available at `http://localhost:8000/{basePath}`
58+
4. Run `gatsby develop`, the theme should now be running on your site. In this case with the blog theme configured with the `basePath` to "/blog", it should be available at `http://localhost:8000/blog`.
5959

60-
> To learn how to further customize a theme, check out the available paths on [Gatsby-theme-blog Documentation](https://www.npmjs.com/package/gatsby-theme-blog).
60+
> To learn how to further customize a theme, check out the available paths on [Gatsby-theme-blog Documentation](/packages/gatsby-theme-blog/#theme-options).
6161
6262
### Additional resources
6363

64-
- To learn how to further customize a theme, check out the docs on [Gatsby theme shadowing.](https://www.gatsbyjs.org/docs/themes/shadowing/)
64+
- To learn how to further customize a theme, check out the docs on [Gatsby theme shadowing.](/docs/themes/shadowing/)
6565

66-
- You can also [use multiple themes](https://www.gatsbyjs.org/docs/themes/using-multiple-gatsby-themes/) on a project.
66+
- You can also [use multiple themes](/docs/themes/using-multiple-gatsby-themes/) on a project.
6767

6868
## Creating a new site using a theme starter
6969

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# dotenv environment variable files
55+
.env*
56+
57+
# gatsby files
58+
.cache/
59+
public
60+
61+
# Mac files
62+
.DS_Store
63+
64+
# Yarn
65+
yarn-error.log
66+
.pnp/
67+
.pnp.js
68+
# Yarn Integrity file
69+
.yarn-integrity

0 commit comments

Comments
 (0)