forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-plugins.js
294 lines (262 loc) Β· 8.27 KB
/
load-plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const _ = require(`lodash`)
const slash = require(`slash`)
const fs = require(`fs`)
const path = require(`path`)
const crypto = require(`crypto`)
const glob = require(`glob`)
const { store } = require(`../redux`)
const nodeAPIs = require(`../utils/api-node-docs`)
const testRequireError = require(`../utils/test-require-error`)
const report = require(`gatsby-cli/lib/reporter`)
function createFileContentHash(root, globPattern) {
const hash = crypto.createHash(`md5`)
const files = glob.sync(`${root}/${globPattern}`, { nodir: true })
files.forEach(filepath => {
hash.update(fs.readFileSync(filepath))
})
return hash.digest(`hex`)
}
/**
* @typedef {Object} PluginInfo
* @property {string} resolve The absolute path to the plugin
* @property {string} name The plugin name
* @property {string} version The plugin version (can be content hash)
*/
/**
* resolvePlugin
* @param {string} pluginName
* This can be a name of a local plugin, the name of a plugin located in
* node_modules, or a Gatsby internal plugin. In the last case the pluginName
* will be an absolute path.
* @return {PluginInfo}
*/
function resolvePlugin(pluginName) {
// Only find plugins when we're not given an absolute path
if (!fs.existsSync(pluginName)) {
// Find the plugin in the local plugins folder
const resolvedPath = slash(path.resolve(`./plugins/${pluginName}`))
if (fs.existsSync(resolvedPath)) {
if (fs.existsSync(`${resolvedPath}/package.json`)) {
const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
return {
resolve: resolvedPath,
name: packageJSON.name || pluginName,
id: `Plugin ${packageJSON.name || pluginName}`,
version:
packageJSON.version || createFileContentHash(resolvedPath, `**`),
}
} else {
// Make package.json a requirement for local plugins too
throw new Error(`Plugin ${pluginName} requires a package.json file`)
}
}
}
/**
* Here we have an absolute path to an internal plugin, or a name of a module
* which should be located in node_modules.
*/
try {
const resolvedPath = slash(path.dirname(require.resolve(pluginName)))
const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
return {
resolve: resolvedPath,
id: `Plugin ${packageJSON.name}`,
name: packageJSON.name,
version: packageJSON.version,
}
} catch (err) {
throw new Error(`Unable to find plugin "${pluginName}"`)
}
}
module.exports = async (config = {}) => {
// Instantiate plugins.
const plugins = []
// Create fake little site with a plugin for testing this
// w/ snapshots. Move plugin processing to its own module.
// Also test adding to redux store.
const processPlugin = plugin => {
if (_.isString(plugin)) {
const info = resolvePlugin(plugin)
return {
...info,
pluginOptions: {
plugins: [],
},
}
} else {
// Plugins can have plugins.
const subplugins = []
if (plugin.options && plugin.options.plugins) {
plugin.options.plugins.forEach(p => {
subplugins.push(processPlugin(p))
})
plugin.options.plugins = subplugins
}
// Add some default values for tests as we don't actually
// want to try to load anything during tests.
if (plugin.resolve === `___TEST___`) {
return {
name: `TEST`,
pluginOptions: {
plugins: [],
},
}
}
const info = resolvePlugin(plugin.resolve)
return {
...info,
pluginOptions: _.merge({ plugins: [] }, plugin.options),
}
}
}
// Add internal plugins
plugins.push(
processPlugin(path.join(__dirname, `../internal-plugins/dev-404-page`))
)
plugins.push(
processPlugin(
path.join(__dirname, `../internal-plugins/component-page-creator`)
)
)
plugins.push(
processPlugin(
path.join(__dirname, `../internal-plugins/component-layout-creator`)
)
)
plugins.push(
processPlugin(
path.join(__dirname, `../internal-plugins/internal-data-bridge`)
)
)
plugins.push(
processPlugin(path.join(__dirname, `../internal-plugins/prod-404`))
)
plugins.push(
processPlugin(path.join(__dirname, `../internal-plugins/query-runner`))
)
// Add plugins from the site config.
if (config.plugins) {
config.plugins.forEach(plugin => {
plugins.push(processPlugin(plugin))
})
}
// Add the site's default "plugin" i.e. gatsby-x files in root of site.
plugins.push({
resolve: slash(process.cwd()),
id: `Plugin default-site-plugin`,
name: `default-site-plugin`,
version: createFileContentHash(process.cwd(), `gatsby-*`),
pluginOptions: {
plugins: [],
},
})
// Create a "flattened" array of plugins with all subplugins
// brought to the top-level. This simplifies running gatsby-* files
// for subplugins.
const flattenedPlugins = []
const extractPlugins = plugin => {
plugin.pluginOptions.plugins.forEach(subPlugin => {
flattenedPlugins.push(subPlugin)
extractPlugins(subPlugin)
})
}
plugins.forEach(plugin => {
flattenedPlugins.push(plugin)
extractPlugins(plugin)
})
// Validate plugins before saving. Plugins can only export known APIs. The known
// 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) => {
acc[value] = []
return acc
}, {})
let badExports = []
flattenedPlugins.forEach(plugin => {
let gatsbyNode
plugin.nodeAPIs = []
try {
gatsbyNode = require(`${plugin.resolve}/gatsby-node`)
} catch (err) {
if (!testRequireError(`gatsby-node`, err)) {
// ignore
} else {
report.panic(`Error requiring ${plugin.resolve}/gatsby-node.js`, err)
}
}
if (gatsbyNode) {
const gatsbyNodeKeys = _.keys(gatsbyNode)
// Discover which nodeAPIs this plugin implements and store
// an array against the plugin node itself *and* in a node
// API to plugins map for faster lookups later.
plugin.nodeAPIs = _.intersection(gatsbyNodeKeys, apis)
plugin.nodeAPIs.map(nodeAPI => apiToPlugins[nodeAPI].push(plugin.name))
// Discover any exports from plugins which are not "known"
badExports = badExports.concat(
_.difference(gatsbyNodeKeys, apis).map(e => {
return {
exportName: e,
pluginName: plugin.name,
pluginVersion: plugin.version,
}
})
)
}
})
if (badExports.length > 0) {
const stringSimiliarity = require(`string-similarity`)
const { stripIndent } = require(`common-tags`)
console.log(`\n`)
console.log(
stripIndent`
Your plugins must export known APIs from their gatsby-node.js.
The following exports aren't APIs. Perhaps you made a typo or
your plugin is outdated?
See https://www.gatsbyjs.org/docs/node-apis/ for the list of Gatsby Node APIs`
)
badExports.forEach(bady => {
const similarities = stringSimiliarity.findBestMatch(
bady.exportName,
apis
)
let message = `\n β `
if (bady.pluginName == `default-site-plugin`) {
message += `Your site's gatsby-node.js is exporting a variable named "${
bady.exportName
}" which isn't an API.`
} else {
message += `The plugin "${bady.pluginName}@${
bady.pluginVersion
}" is exporting a variable named "${
bady.exportName
}" which isn't an API.`
}
if (similarities.bestMatch.rating > 0.5) {
message += ` Perhaps you meant to export "${
similarities.bestMatch.target
}"?`
}
console.log(message)
})
process.exit()
}
store.dispatch({
type: `SET_SITE_PLUGINS`,
payload: plugins,
})
store.dispatch({
type: `SET_SITE_FLATTENED_PLUGINS`,
payload: flattenedPlugins,
})
store.dispatch({
type: `SET_SITE_API_TO_PLUGINS`,
payload: apiToPlugins,
})
return flattenedPlugins
}