Skip to content

Commit

Permalink
fix: load plugin object style without options object (#9647)
Browse files Browse the repository at this point in the history
<!--
  Q. Which branch should I use for my pull request?
  A. Use `master` branch (probably).

  Q. Which branch if my change is a bug fix for Gatsby v1?
  A. In this case, you should use the `v1` branch

  Q. Which branch if I'm still not sure?
  A. Use `master` branch. Ask in the PR if you're not sure and a Gatsby maintainer will be happy to help :)

  Note: We will only accept bug fixes for Gatsby v1. New features should be added to Gatsby v2.

  Learn more about contributing: https://www.gatsbyjs.org/docs/how-to-contribute/
-->

Fixes issue at the end of #9559, e.g.:
```javascript
module.exports = {
    plugins: [
        {
            resolve: 'gatsby-plugin-react-helmet'
        }
    ]
}
```
where the plugin is loaded using object syntax but no `options` object is defined.

This wasn't caught by the unit test because the issue was occurring after the early out when it's the `___TEST___` plugin.
  • Loading branch information
zvinless authored and pieh committed Nov 2, 2018
1 parent 431da37 commit 42a2d07
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
Expand Up @@ -87,7 +87,7 @@ Array [
},
Object {
"browserAPIs": Array [],
"id": "d5fb39bc-5b13-5925-86de-f0e18549d272",
"id": "cc2293db-7635-5675-bb5e-05c41ff28688",
"name": "gatsby-plugin-page-creator",
"nodeAPIs": Array [
"createPagesStatefully",
Expand Down Expand Up @@ -179,6 +179,7 @@ Array [
},
Object {
"browserAPIs": Array [],
"id": "c731052f-1e62-5905-9fb3-edc9057be8f1",
"name": "TEST",
"nodeAPIs": Array [],
"pluginOptions": Object {
Expand All @@ -202,7 +203,7 @@ Array [
},
Object {
"browserAPIs": Array [],
"id": "d5fb39bc-5b13-5925-86de-f0e18549d272",
"id": "cc2293db-7635-5675-bb5e-05c41ff28688",
"name": "gatsby-plugin-page-creator",
"nodeAPIs": Array [
"createPagesStatefully",
Expand Down
38 changes: 24 additions & 14 deletions packages/gatsby/src/bootstrap/load-plugins/load.js
Expand Up @@ -19,6 +19,19 @@ function createFileContentHash(root, globPattern) {
return hash.digest(`hex`)
}

/**
* Make sure key is unique to plugin options. E.g there could
* be multiple source-filesystem plugins, with different names
* (docs, blogs).
* @param {*} name Name of the plugin
* @param {*} pluginObject
*/
const createPluginId = (name, pluginObject = null) =>
createNodeId(
name + (pluginObject ? JSON.stringify(pluginObject.options) : ``),
`Plugin`
)

/**
* @typedef {Object} PluginInfo
* @property {string} resolve The absolute path to the plugin
Expand Down Expand Up @@ -49,7 +62,7 @@ function resolvePlugin(pluginName) {
return {
resolve: resolvedPath,
name,
id: createNodeId(name, `Plugin`),
id: createPluginId(name),
version:
packageJSON.version || createFileContentHash(resolvedPath, `**`),
}
Expand All @@ -73,7 +86,7 @@ function resolvePlugin(pluginName) {

return {
resolve: resolvedPath,
id: createNodeId(packageJSON.name, `Plugin`),
id: createPluginId(packageJSON.name),
name: packageJSON.name,
version: packageJSON.version,
}
Expand Down Expand Up @@ -102,9 +115,11 @@ module.exports = (config = {}) => {
},
}
} else {
plugin.options = plugin.options || {}

// Plugins can have plugins.
const subplugins = []
if (plugin.options && plugin.options.plugins) {
if (plugin.options.plugins) {
plugin.options.plugins.forEach(p => {
subplugins.push(processPlugin(p))
})
Expand All @@ -115,8 +130,11 @@ module.exports = (config = {}) => {
// Add some default values for tests as we don't actually
// want to try to load anything during tests.
if (plugin.resolve === `___TEST___`) {
const name = `TEST`

return {
name: `TEST`,
id: createPluginId(name, plugin),
name,
pluginOptions: {
plugins: [],
},
Expand All @@ -127,15 +145,7 @@ module.exports = (config = {}) => {

return {
...info,
// Make sure key is unique to plugin options. E.g there could
// be multiple source-filesystem plugins, with different names
// (docs, blogs).
id: createNodeId(
plugin.options
? plugin.name + JSON.stringify(plugin.options)
: plugin.name,
`Plugin`
),
id: createPluginId(info.name, plugin),
pluginOptions: _.merge({ plugins: [] }, plugin.options),
}
}
Expand Down Expand Up @@ -164,7 +174,7 @@ module.exports = (config = {}) => {
// Add the site's default "plugin" i.e. gatsby-x files in root of site.
plugins.push({
resolve: slash(process.cwd()),
id: createNodeId(`default-site-plugin`, `Plugin`),
id: createPluginId(`default-site-plugin`),
name: `default-site-plugin`,
version: createFileContentHash(process.cwd(), `gatsby-*`),
pluginOptions: {
Expand Down

0 comments on commit 42a2d07

Please sign in to comment.