|
| 1 | +<p align="center"> |
| 2 | + <a href="https://www.gatsbyjs.com"> |
| 3 | + <img alt="Gatsby" src="https://www.gatsbyjs.com/Gatsby-Monogram.svg" width="60" /> |
| 4 | + </a> |
| 5 | +</p> |
| 6 | +<h1 align="center"> |
| 7 | + Starter for a Gatsby Plugin |
| 8 | +</h1> |
| 9 | + |
| 10 | +A minimal boilerplate for the essential files Gatsby looks for in a plugin. |
| 11 | + |
| 12 | +## 🚀 Quick start |
| 13 | + |
| 14 | +To get started creating a new plugin, you can follow these steps: |
| 15 | + |
| 16 | +1. Initialize a new plugin from the starter with `gatsby new` |
| 17 | + |
| 18 | +```shell |
| 19 | +gatsby new my-plugin https://github.com/gatsbyjs/gatsby-starter-plugin |
| 20 | +``` |
| 21 | + |
| 22 | +If you already have a Gatsby site, you can use it. Otherwise, you can [create a new Gatsby site](https://www.gatsbyjs.com/docs/tutorial/getting-started/part-0/#create-a-gatsby-site) to test your plugin. |
| 23 | + |
| 24 | +Your directory structure will look similar to this: |
| 25 | + |
| 26 | +```text |
| 27 | +/my-gatsby-site |
| 28 | +├── gatsby-config.js |
| 29 | +└── /src |
| 30 | + └── /pages |
| 31 | + └── /index.js |
| 32 | +/my-plugin |
| 33 | +├── gatsby-browser.js |
| 34 | +├── gatsby-node.js |
| 35 | +├── gatsby-ssr.js |
| 36 | +├── index.js |
| 37 | +├── package.json |
| 38 | +└── README.md |
| 39 | +``` |
| 40 | + |
| 41 | +With `my-gatsby-site` being your Gatsby site, and `my-plugin` being your plugin. You could also include the plugin in your [site's `plugins` folder](https://www.gatsbyjs.com/docs/loading-plugins-from-your-local-plugins-folder/). |
| 42 | + |
| 43 | +2. Include the plugin in a Gatsby site |
| 44 | + |
| 45 | +Inside of the `gatsby-config.js` file of your site (in this case, `my-gatsby-site`), include the plugin in the `plugins` array: |
| 46 | + |
| 47 | +```javascript |
| 48 | +module.exports = { |
| 49 | + plugins: [ |
| 50 | + // other gatsby plugins |
| 51 | + // ... |
| 52 | + require.resolve(`../my-plugin`), |
| 53 | + ], |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +The line `require.resolve('../my-plugin')` is what accesses the plugin based on its filepath on your computer, and adds it as a plugin when Gatsby runs. |
| 58 | + |
| 59 | +_You can use this method to test and develop your plugin before you publish it to a package registry like npm. Once published, you would instead install it and [add the plugin name to the array](https://www.gatsbyjs.com/docs/using-a-plugin-in-your-site/). You can read about other ways to connect your plugin to your site including using `npm link` or `yarn workspaces` in the [doc on creating local plugins](https://www.gatsbyjs.com/docs/creating-a-local-plugin/#developing-a-local-plugin-that-is-outside-your-project)._ |
| 60 | + |
| 61 | +3. Verify the plugin was added correctly |
| 62 | + |
| 63 | +The plugin added by the starter implements a single Gatsby API in the `gatsby-node` that logs a message to the console. When you run `gatsby develop` or `gatsby build` in the site that implements your plugin, you should see this message. |
| 64 | + |
| 65 | +You can verify your plugin was added to your site correctly by running `gatsby develop` for the site. |
| 66 | + |
| 67 | +You should now see a message logged to the console in the preinit phase of the Gatsby build process: |
| 68 | + |
| 69 | +```shell |
| 70 | +$ gatsby develop |
| 71 | +success open and validate gatsby-configs - 0.033s |
| 72 | +success load plugins - 0.074s |
| 73 | +Loaded gatsby-starter-plugin |
| 74 | +success onPreInit - 0.016s |
| 75 | +... |
| 76 | +``` |
| 77 | + |
| 78 | +4. Rename the plugin in the `package.json` |
| 79 | + |
| 80 | +When you clone the site, the information in the `package.json` will need to be updated. Name your plugin based off of [Gatsby's conventions for naming plugins](https://www.gatsbyjs.com/docs/naming-a-plugin/). |
| 81 | + |
| 82 | +## 🧐 What's inside? |
| 83 | + |
| 84 | +This starter generates the [files Gatsby looks for in plugins](https://www.gatsbyjs.com/docs/files-gatsby-looks-for-in-a-plugin/). |
| 85 | + |
| 86 | +```text |
| 87 | +/my-plugin |
| 88 | +├── .gitignore |
| 89 | +├── gatsby-browser.js |
| 90 | +├── gatsby-node.js |
| 91 | +├── gatsby-ssr.js |
| 92 | +├── index.js |
| 93 | +├── LICENSE |
| 94 | +├── package.json |
| 95 | +└── README.md |
| 96 | +``` |
| 97 | + |
| 98 | +- **`.gitignore`**: This file tells git which files it should not track / not maintain a version history for. |
| 99 | +- **`gatsby-browser.js`**: This file is where Gatsby expects to find any usage of the [Gatsby browser APIs](https://www.gatsbyjs.com/docs/browser-apis/) (if any). These allow customization/extension of default Gatsby settings affecting the browser. |
| 100 | +- **`gatsby-node.js`**: This file is where Gatsby expects to find any usage of the [Gatsby Node APIs](https://www.gatsbyjs.com/docs/node-apis/) (if any). These allow customization/extension of default Gatsby settings affecting pieces of the site build process. |
| 101 | +- **`gatsby-ssr.js`**: This file is where Gatsby expects to find any usage of the [Gatsby server-side rendering APIs](https://www.gatsbyjs.com/docs/ssr-apis/) (if any). These allow customization of default Gatsby settings affecting server-side rendering. |
| 102 | +- **`index.js`**: A file that will be loaded by default when the plugin is [required by another application](https://docs.npmjs.com/creating-node-js-modules#create-the-file-that-will-be-loaded-when-your-module-is-required-by-another-application0). You can adjust what file is used by updating the `main` field of the `package.json`. |
| 103 | +- **`LICENSE`**: This plugin starter is licensed under the 0BSD license. This means that you can see this file as a placeholder and replace it with your own license. |
| 104 | +- **`package.json`**: A manifest file for Node.js projects, which includes things like metadata (the plugin's name, author, etc). This manifest is how npm knows which packages to install for your project. |
| 105 | +- **`README.md`**: A text file containing useful reference information about your plugin. |
| 106 | + |
| 107 | +## 🎓 Learning Gatsby |
| 108 | + |
| 109 | +If you're looking for more guidance on plugins, how they work, or what their role is in the Gatsby ecosystem, check out some of these resources: |
| 110 | + |
| 111 | +- The [Creating Plugins](https://www.gatsbyjs.com/docs/creating-plugins/) section of the docs has information on authoring and maintaining plugins yourself. |
| 112 | +- The conceptual guide on [Plugins, Themes, and Starters](https://www.gatsbyjs.com/docs/plugins-themes-and-starters/) compares and contrasts plugins with other pieces of the Gatsby ecosystem. It can also help you [decide what to choose between a plugin, starter, or theme](https://www.gatsbyjs.com/docs/plugins-themes-and-starters/#deciding-which-to-use). |
| 113 | +- The [Gatsby plugin library](https://www.gatsbyjs.com/plugins/) has over 1750 official as well as community developed plugins that can get you up and running faster and borrow ideas from. |
0 commit comments