-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: plugin authoring workflow overview pages (#20847)
* additional examples to loading/creating, and plugin authoring overview pages * correct minor errors and add tweaks to titles * Apply suggestions from code review Co-Authored-By: LB <[email protected]> * added example repo * Update docs/docs/creating-a-local-plugin.md * simplify the site readme * update readme for clearer instructions and explanations * Apply suggestions from code review Co-Authored-By: LB <[email protected]> Co-authored-by: LB <[email protected]> Co-authored-by: GatsbyJS Bot <[email protected]>
- Loading branch information
1 parent
ae6d474
commit adfb867
Showing
28 changed files
with
614 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Using Gatsby with Local Plugins | ||
|
||
This is an example repository demonstrating different ways to load plugins in a Gatsby site. | ||
|
||
## Running the Example | ||
|
||
The actual Gatsby site to run is in the `gatsby-site-using-local-plugins` folder. | ||
|
||
Navigate into the `gatsby-site-using-local-plugins` project directory with this command: | ||
|
||
```sh | ||
cd gatsby-site-using-local-plugins | ||
``` | ||
|
||
You'll need to install dependencies for the site by running: | ||
|
||
```sh | ||
npm install | ||
``` | ||
|
||
Then run `gatsby develop`: | ||
|
||
```sh | ||
gatsby develop | ||
``` | ||
|
||
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. | ||
|
||
```sh | ||
$ gatsby develop | ||
success open and validate gatsby-configs - 0.051s | ||
success load plugins - 1.047s | ||
logging to the console from plugins folder | ||
logging to the console from a plugin in another project with require.resolve | ||
logging to the console from site's gatsby-node | ||
success onPreInit - 0.023s | ||
``` | ||
For the config to load all the plugins you'll need to uncomment the line in the `gatsby-site-using-multiple-local-plugins/gatsby-config.js` file for `gatsby-plugin-console-log-c`: | ||
|
||
```javascript:title=gatsby-site-using-multiple-local-plugins/gatsby-config.js | ||
module.exports = { | ||
siteMetadata: { | ||
title: `Using Multiple Local Plugins`, | ||
description: `An example Gatsby site utilizing multiple local plugins`, | ||
author: `@gatsbyjs`, | ||
}, | ||
plugins: [ | ||
`gatsby-plugin-react-helmet`, | ||
// including a plugin from the plugins folder | ||
`gatsby-plugin-console-log-a`, | ||
{ | ||
// including a plugin from outside the plugins folder needs the path to it | ||
resolve: require.resolve(`../gatsby-plugin-console-log-b`), | ||
}, | ||
// including a plugin with yarn or npm link | ||
// in order for this plugin to be found when you run gatsby develop | ||
// you first need to run `npm link ../gatsby-plugin-console-log-c` in the `gatsby-site-using-local-plugins` root folder | ||
`gatsby-plugin-console-log-c`, // highlight-line | ||
], | ||
} | ||
``` | ||
|
||
And then run: | ||
|
||
```sh:title=gatsby-site-using-multiple-local-plugins | ||
npm link ../gatsby-plugin-console-log-c | ||
``` | ||
|
||
When you run `gatsby develop` now, you should see the last plugin logging another line: | ||
|
||
```diff | ||
$ gatsby develop | ||
success open and validate gatsby-configs - 0.051s | ||
success load plugins - 1.047s | ||
logging to the console from plugins folder | ||
logging to the console from a plugin in another project with require.resolve | ||
+ logging to the console from a plugin in another project with npm/yarn link | ||
logging to the console from site's gatsby-node | ||
success onPreInit - 0.023s | ||
``` | ||
## Context | ||
The same `gatsby-plugin-console-log` plugin is implemented 3 times (and 1 additional time in the site's `gatsby-node`), each one hooking into the `onPreInit` Gatsby Node API to log a simple message to the console when the site is run in develop or build mode. | ||
The code that is implemented looks similar to this: | ||
```javascript | ||
exports.onPreInit = () => { | ||
console.log("logging to the console...") | ||
} | ||
``` | ||
|
||
### 4 patterns implemented | ||
|
||
The 4 ways the code is run are: | ||
|
||
1. Inside the site's `gatsby-node.js` | ||
2. In a plugin in the plugins folder (`gatsby-plugin-console-log-a`) | ||
3. In a separate project folder but included with `require.resolve` in the config (`gatsby-plugin-console-log-b`) | ||
4. In a separate project folder but included via `npm link` (`gatsby-plugin-console-log-c`) | ||
|
||
You can read about these methods in the [loading local plugins doc](https://www.gatsbyjs.org/docs/loading-plugins-from-your-local-plugins-folder/). | ||
|
||
## More advanced local plugin example | ||
|
||
For another example featuring a more sophisticated local plugin, you can refer to the [`using-local-plugins` example repository](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-local-plugins). |
3 changes: 3 additions & 0 deletions
3
examples/using-multiple-local-plugins/gatsby-plugin-console-log-b/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports.onPreInit = () => { | ||
console.log("logging to the console from a plugin in another project with require.resolve") | ||
} |
1 change: 1 addition & 0 deletions
1
examples/using-multiple-local-plugins/gatsby-plugin-console-log-b/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// noop |
7 changes: 7 additions & 0 deletions
7
examples/using-multiple-local-plugins/gatsby-plugin-console-log-b/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "gatsby-plugin-console-log-b", | ||
"version": "1.0.0", | ||
"description": "Log stuff in a Gatsby site's build process", | ||
"main": "index.js", | ||
"license": "MIT" | ||
} |
3 changes: 3 additions & 0 deletions
3
examples/using-multiple-local-plugins/gatsby-plugin-console-log-c/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports.onPreInit = () => { | ||
console.log("logging to the console from a plugin in another project with npm/yarn link") | ||
} |
1 change: 1 addition & 0 deletions
1
examples/using-multiple-local-plugins/gatsby-plugin-console-log-c/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// noop |
7 changes: 7 additions & 0 deletions
7
examples/using-multiple-local-plugins/gatsby-plugin-console-log-c/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "gatsby-plugin-console-log-c", | ||
"version": "1.0.0", | ||
"description": "Log stuff in a Gatsby site's build process", | ||
"main": "index.js", | ||
"license": "MIT" | ||
} |
69 changes: 69 additions & 0 deletions
69
examples/using-multiple-local-plugins/gatsby-site-using-local-plugins/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Typescript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# dotenv environment variable files | ||
.env* | ||
|
||
# gatsby files | ||
.cache/ | ||
public | ||
|
||
# Mac files | ||
.DS_Store | ||
|
||
# Yarn | ||
yarn-error.log | ||
.pnp/ | ||
.pnp.js | ||
# Yarn Integrity file | ||
.yarn-integrity |
Oops, something went wrong.