Skip to content

Commit a5c0871

Browse files
jlengstorfKyleAMathews
authored andcommitted
docs: improve documentation for plugin authors (#4272)
* docs: add links for plugin authors - Link to plugin docs - Link to API spec re #4266 * docs: create a “plugin authoring” page re #4266 * docs: move authoring docs to authoring page - removes local plugin docs - adds link to plugin authoring (where local plugin docs were moved) re #4266 * docs: write plugin authoring docs - add naming convention explanations and examples - add core plugin concepts as bullet points - add list of files Gatsby tries to find in a plugin - explain local plugin development re #4266 * docs: clean up plugin authoring docs - Add link to “create a source plugin” docs - Clarify optional vs. required files - Clarify how `package.json` works and what default values are - Clean up local plugin `gatsby-config.js` note - Remove duplicate explanation of `package.json` re #4266 * docs: clarify npm package core concept - Clarify that plugins can run as npm packages or local files re #4266 * Add missing word * docs: improve opening paragraph - remove reference to plugins being “easy” - build off @robwierzbowski’s wording - add examples of what plugins can do re #4266 * docs: clarify and clean up - update wording per @KyleAMathews feedback - clarify transformer description - link to example plugins - fix grammar mistakes re #4266 * docs: improve wording around plugin documentation re #4266 * docs: minor tweaks * Reverse order as extending far more common
1 parent 7530c9b commit a5c0871

File tree

4 files changed

+72
-38
lines changed

4 files changed

+72
-38
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Looking to speak about Gatsby? We'd love to review your talk abstract/CFP! You c
2323

2424
### Creating your own plugins and loaders
2525

26-
If you create a loader or plugin, we would <3 for you to open source it, and put it on npm.
26+
If you create a loader or plugin, we would <3 for you to open source it, and put it on npm. For more information on creating custom plugins, please see the documentation for [plugins](/docs/plugins/) and the [API specification](/docs/api-specification/).
2727

2828
### Contributing to the repo
2929

docs/docs/plugin-authoring.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: Plugin Authoring
3+
---
4+
5+
One of the best ways to add functionality to Gatsby is through our plugin system. Gatsby is designed to be extensible, which means plugins are able to extend and modify just about everything Gatsby does.
6+
7+
Of the many possibilities, plugins can:
8+
9+
- add external data or content (e.g. your CMS, static files, a REST API) to your Gatsby GraphQL data
10+
- transform data from other formats (e.g. YAML, CSV) to JSON objects
11+
- add third-party services (e.g. Google Analytics, Instagram) to your site
12+
- anything you can dream up!
13+
14+
## Core Concepts
15+
16+
- Each Gatsby plugin can be created as an npm package or as a [local plugin](#local-plugins)
17+
- A `package.json` is required
18+
- Plugin implement the Gatsby APIs for [Node](/docs/node-apis/), [server-side rendering](/docs/ssr-apis/), and the [browser](/docs/browser-apis/)
19+
20+
## Plugin naming conventions
21+
22+
There are four standard plugin naming conventions for Gatsby:
23+
24+
- **`gatsby-source-*`** — a source plugin loads data from a given source (e.g. WordPress, MongoDB, the file system). Use this plugin type if you are connecting a new source of data to Gatsby.
25+
- Example: [`gatsby-source-contentful`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-contentful)
26+
- Docs: [create a source plugin](/docs/create-source-plugin/)
27+
- **`gatsby-transformer-*`** — a transformer plugin converts data from one format (e.g. CSV, YAML) to a JavaScript object. Use this naming convention if your plugin will be transforming data from one format to another.
28+
- Example: [`gatsby-transformer-yaml`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-yaml)
29+
- **`gatsby-[plugin-name]-*`** — if a plugin is a plugin for another plugin 😅, it should be prefixed with the name of the plugin it extends (e.g. if it adds emoji to the output of `gatsby-transformer-remark`, call it `gatsby-remark-add-emoji`). Use this naming convention whenever your plugin will be included as a plugin in the `options` object of another plugin.
30+
- Example: [`gatsby-remark-images`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-remark-images)
31+
- **`gatsby-plugin-*`** — this is the most general plugin type. Use this naming convention if your plugin doesn’t meet the requirements of any other plugin types.
32+
- Example: [`gatsby-plugin-sharp`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-sharp)
33+
34+
## What files does Gatsby look for in a plugin?
35+
36+
All files are optional unless specifically marked as required.
37+
38+
- `package.json`[required] this can be an empty object (`{}`) for local plugins
39+
- `name` is used to identify the plugin when it mutates Gatsby’s GraphQL data structure
40+
- if `name` isn’t set, the folder name for the plugin is used
41+
- `version` is used to manage the cache — if it changes, the cache is cleared
42+
- if `version` isn’t set, an MD5 hash of the `gatsby-*` file contents is used to invalidate the cache
43+
- omitting the `version` field is recommended for local plugins
44+
- `gatsby-browser.js` — usage details are in the [browser API reference](/docs/browser-apis/)
45+
- `gatsby-node.js` — usage details are in the [Node API reference](/docs/node-apis/)
46+
- `gatsby-ssr.js` — usage details are in the [SSR API reference](/docs/ssr-apis/)
47+
48+
## Local plugins
49+
50+
If a plugin is only relevant to your specific use-case, or if you’re developing a plugin and want a simpler workflow, a locally defined plugin is a convenient way to create and manage your plugin code.
51+
52+
Place the code in the `plugins` folder in the root of your project like this:
53+
54+
```
55+
plugins
56+
└── my-own-plugin
57+
└── package.json
58+
```
59+
60+
**NOTE:** You still need to add the plugin to your `gatsby-config.js`. There is no auto-detection of local plugins.
61+
62+
Like all `gatsby-*` files, the code is not processed by Babel. If you want
63+
to use JavaScript syntax which isn't supported by your version of Node.js, you
64+
can place the files in a `src` subfolder and build them to the plugin folder
65+
root.

docs/docs/plugins.md

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,14 @@ module.exports = {
3737

3838
Plugins can take options. Note that plugin options will be stringified by Gatsby, so they cannot be functions.
3939

40-
See each plugin page below for more detailed
41-
documentation on using each plugin.
40+
## Creating your own plugins
4241

43-
## Locally defined plugins
44-
45-
When you want to work on a new plugin, or maybe write one that is only relevant
46-
to your specific use-case, a locally defined plugin is more convenient than
47-
having to create an NPM package for it.
48-
49-
You can place the code in the `plugins` folder in the root of your project like
50-
this:
51-
52-
```
53-
plugins
54-
└── my-own-plugin
55-
├── gatsby-node.js
56-
└── package.json
57-
```
58-
59-
You still need to add the plugin to your `gatsby-config.js` like for plugins
60-
installed from NPM.
61-
62-
Each plugin requires a package.json file, but the minimum content is just an
63-
empty object `{}`. The `name` and `version` fields are read from the package
64-
file. The name is used to identify the plugin when it mutates the GraphQL data
65-
structure. The version is used to clear the cache when it changes.
66-
67-
For local plugins it is best to leave the version field empty. Gatsby will
68-
generate an md5-hash from all gatsby-\* file contents and use that as the
69-
version. This way the cache is automatically flushed when you change the code of
70-
your plugin.
71-
72-
If the name is empty it is inferred from the plugin folder name.
73-
74-
Like all gatsby-\* files, the code is not being processed by Babel. If you want
75-
to use JavaScript syntax which isn't supported by your version of Node.js, you
76-
can place the files in a `src` subfolder and build them to the plugin folder
77-
root.
42+
If you’d like to create a custom Gatsby plugin, check out the [plugin authoring guide](/docs/plugin-authoring/).
7843

7944
## Official plugins
8045

46+
For usage instructions and options, see the plugin repo (linked below).
47+
8148
* [gatsby-plugin-aphrodite](/packages/gatsby-plugin-aphrodite/)
8249
* [gatsby-plugin-canonical-urls](/packages/gatsby-plugin-canonical-urls/)
8350
* [gatsby-plugin-catch-links](/packages/gatsby-plugin-catch-links/)

www/src/pages/docs/doc-links.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
link: /docs/migrating-from-v0-to-v1/
5757
- title: Path Prefix
5858
link: /docs/path-prefix/
59+
- title: Plugin Authoring
60+
link: /docs/plugin-authoring/
5961
- title: Proxying API Requests
6062
link: /docs/api-proxy/
6163
- title: Using CSS-in-JS library Glamor

0 commit comments

Comments
 (0)