Get started writing a Gatsby plugin using the plugin starter
Get setup to write a Gatsby plugin using the official plugin starter. Run the gatsby new command and connect the plugin to a site to test with.
Duration
1 min
Access
Free
Transcript
Available
Get setup to write a Gatsby plugin using the official plugin starter. Run the gatsby new command and connect the plugin to a site to test with.
The plugin starter includes a message that will be logged to the console of your site when it is hooked up correctly, helping you verify that your plugin is being loaded.
Kyle Gill: To develop a Gatsby plugin, you need a site to run it in. You can quickly generate a new site running gatsby new using the default name my-gatsby-project and choosing a starter. I chose the hello-world starter. You should see a new directory created, called my-gatsby-project, that contains the example site.
Gatsby maintains a starter that can be used as an initial boilerplate for creating a new plugin. Copy the link to clone it from GitHub and run gatsby new again, this time passing in my-plugin as the first argument and the Git URL as the second argument. Another directory will be generated containing the plugin starter code.
You need to include your plugin in the example site's config so it knows to run the plugin. Navigate to the gatsby-config file. If you don't have one already, add a plugins array and include an entry for your plugin with require.resolve and the path to your plugin. In my case, in the my-plugin folder.
You could also link your plugin using npm link or yarn workspace, if you're familiar with them. Your plugin's code that implement Gatsby APIs, like in the gatsby-node file, will now be run when you start the site. This code in the onPreInit hook will log "Loaded gatsby-starter-plugin" to the console. Run the example site with gatsby develop. You should see the message in the log output.