Skip to content

Commit

Permalink
Fix plugin loader checks (#4023)
Browse files Browse the repository at this point in the history
* Add browser and SSR APIs to the plugin API whitelist

* Add registerServiceWorker to the list of allowed browser APIs
  • Loading branch information
m-allanson authored and KyleAMathews committed Feb 13, 2018
1 parent 725c6e8 commit 7b1792a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
27 changes: 17 additions & 10 deletions packages/gatsby/src/bootstrap/load-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const glob = require(`glob`)

const { store } = require(`../redux`)
const nodeAPIs = require(`../utils/api-node-docs`)
const browserAPIs = require(`../utils/api-browser-docs`)
const ssrAPIs = require(`../../cache-dir/api-ssr-docs`)
const testRequireError = require(`../utils/test-require-error`)
const report = require(`gatsby-cli/lib/reporter`)

Expand Down Expand Up @@ -270,13 +272,18 @@ module.exports = async (config = {}) => {
// APIs that a plugin supports are saved along with the plugin in the store for
// easier filtering later. If there are bad exports (either typos, outdated, or
// plain incorrect), then we output a readable error & quit.
const apis = _.keys(nodeAPIs)
const apiToPlugins = apis.reduce((acc, value) => {
const apis = {}
apis.node = _.keys(nodeAPIs)
apis.browser = _.keys(browserAPIs)
apis.ssr = _.keys(ssrAPIs)

const allAPIs = [...apis.node, ...apis.browser, ...apis.ssr]

const apiToPlugins = allAPIs.reduce((acc, value) => {
acc[value] = []
return acc
}, {})


const badExports = {
node: [],
browser: [],
Expand All @@ -297,23 +304,23 @@ module.exports = async (config = {}) => {
// later.
if (gatsbyNode) {
const gatsbyNodeKeys = _.keys(gatsbyNode)
plugin.nodeAPIs = _.intersection(gatsbyNodeKeys, apis)
plugin.nodeAPIs = _.intersection(gatsbyNodeKeys, apis.node)
plugin.nodeAPIs.map(nodeAPI => apiToPlugins[nodeAPI].push(plugin.name))
badExports.node = getBadExports(plugin, gatsbyNodeKeys, apis) // Collate any bad exports
badExports.node = getBadExports(plugin, gatsbyNodeKeys, apis.node) // Collate any bad exports
}

if (gatsbyBrowser) {
const gatsbyBrowserKeys = _.keys(gatsbyBrowser)
plugin.browserAPIs = _.intersection(gatsbyBrowserKeys, apis)
plugin.browserAPIs = _.intersection(gatsbyBrowserKeys, apis.browser)
plugin.browserAPIs.map(browserAPI => apiToPlugins[browserAPI].push(plugin.name))
badExports.browser = getBadExports(plugin, gatsbyBrowserKeys, apis) // Collate any bad exports
badExports.browser = getBadExports(plugin, gatsbyBrowserKeys, apis.browser) // Collate any bad exports
}

if (gatsbySSR) {
const gatsbySSRKeys = _.keys(gatsbySSR)
plugin.ssrAPIs = _.intersection(gatsbySSRKeys, apis)
plugin.ssrAPIs = _.intersection(gatsbySSRKeys, apis.ssr)
plugin.ssrAPIs.map(ssrAPI => apiToPlugins[ssrAPI].push(plugin.name))
badExports.ssr = getBadExports(plugin, gatsbySSRKeys, apis) // Collate any bad exports
badExports.ssr = getBadExports(plugin, gatsbySSRKeys, apis.ssr) // Collate any bad exports
}
})

Expand All @@ -323,7 +330,7 @@ module.exports = async (config = {}) => {
const [exportType, entries] = bad
if (entries.length > 0) {
bad = true
console.log(getBadExportsMessage(entries, exportType, apis))
console.log(getBadExportsMessage(entries, exportType, apis[exportType]))
}
})

Expand Down
7 changes: 7 additions & 0 deletions packages/gatsby/src/utils/api-browser-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ exports.onRouteUpdate = true
*/
exports.shouldUpdateScroll = true

/**
* Allow a plugin to register a Service Worker. Should be a function that returns true.
* @example
* exports.registerServiceWorker = () => true
*/
exports.registerServiceWorker = true

/**
* Allow a plugin to replace the router component e.g. to use a custom history version.
* @param {object} $0
Expand Down

0 comments on commit 7b1792a

Please sign in to comment.