UNPKG

1.07 kBJavaScriptView Raw
1// During bootstrap, we write requires at top of this file which looks like:
2// var plugins = [
3// {
4// plugin: require("/path/to/plugin1/gatsby-ssr.js"),
5// options: { ... },
6// },
7// {
8// plugin: require("/path/to/plugin2/gatsby-ssr.js"),
9// options: { ... },
10// },
11// ]
12
13const apis = require(`./api-ssr-docs`)
14
15// Run the specified API in any plugins that have implemented it
16module.exports = (api, args, defaultReturn, argTransform) => {
17 if (!apis[api]) {
18 console.log(`This API doesn't exist`, api)
19 }
20
21 // Run each plugin in series.
22 // eslint-disable-next-line no-undef
23 let results = plugins.map(plugin => {
24 if (!plugin.plugin[api]) {
25 return undefined
26 }
27 const result = plugin.plugin[api](args, plugin.options)
28 if (result && argTransform) {
29 args = argTransform({ args, result })
30 }
31 return result
32 })
33
34 // Filter out undefined results.
35 results = results.filter(result => typeof result !== `undefined`)
36
37 if (results.length > 0) {
38 return results
39 } else {
40 return [defaultReturn]
41 }
42}