-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
load.js
202 lines (177 loc) · 5.6 KB
/
load.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
const _ = require(`lodash`)
const slash = require(`slash`)
const fs = require(`fs`)
const path = require(`path`)
const crypto = require(`crypto`)
const glob = require(`glob`)
const { warnOnIncompatiblePeerDependency } = require(`./validate`)
const { store } = require(`../../redux`)
const existsSync = require(`fs-exists-cached`).sync
const createNodeId = require(`../../utils/create-node-id`)
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`)
}
/**
* 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
* @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 (!existsSync(pluginName)) {
// Find the plugin in the local plugins folder
const resolvedPath = slash(path.resolve(`./plugins/${pluginName}`))
if (existsSync(resolvedPath)) {
if (existsSync(`${resolvedPath}/package.json`)) {
const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
const name = packageJSON.name || pluginName
warnOnIncompatiblePeerDependency(name, packageJSON)
return {
resolve: resolvedPath,
name,
id: createPluginId(name),
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`)
)
warnOnIncompatiblePeerDependency(packageJSON.name, packageJSON)
return {
resolve: resolvedPath,
id: createPluginId(packageJSON.name),
name: packageJSON.name,
version: packageJSON.version,
}
} catch (err) {
throw new Error(
`Unable to find plugin "${pluginName}". Perhaps you need to install its package?`
)
}
}
module.exports = (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 {
plugin.options = plugin.options || {}
// Plugins can have plugins.
const subplugins = []
if (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___`) {
const name = `TEST`
return {
id: createPluginId(name, plugin),
name,
pluginOptions: {
plugins: [],
},
}
}
const info = resolvePlugin(plugin.resolve)
return {
...info,
id: createPluginId(info.name, plugin),
pluginOptions: _.merge({ plugins: [] }, plugin.options),
}
}
}
// Add internal plugins
const internalPlugins = [
`../../internal-plugins/dev-404-page`,
`../../internal-plugins/load-babel-config`,
`../../internal-plugins/internal-data-bridge`,
`../../internal-plugins/prod-404`,
`../../internal-plugins/query-runner`,
`../../internal-plugins/webpack-theme-component-shadowing`,
]
internalPlugins.forEach(relPath => {
const absPath = path.join(__dirname, relPath)
plugins.push(processPlugin(absPath))
})
// 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: createPluginId(`default-site-plugin`),
name: `default-site-plugin`,
version: createFileContentHash(process.cwd(), `gatsby-*`),
pluginOptions: {
plugins: [],
},
})
const program = store.getState().program
plugins.push(
processPlugin({
resolve: `gatsby-plugin-page-creator`,
options: {
path: slash(path.join(program.directory, `src/pages`)),
pathCheck: false,
},
})
)
return plugins
}