Skip to content

Commit

Permalink
Adds a rootDir config option used to resolve plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Maël Nison committed Feb 28, 2019
1 parent 383ad49 commit fc855d6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/gatsby/src/bootstrap/get-config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ module.exports = async function getConfigFile(
}
}

return configModule
return Object.assign(configModule, {
rootDir,
})
}
10 changes: 7 additions & 3 deletions packages/gatsby/src/bootstrap/load-plugins/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { warnOnIncompatiblePeerDependency } = require(`./validate`)
const { store } = require(`../../redux`)
const existsSync = require(`fs-exists-cached`).sync
const createNodeId = require(`../../utils/create-node-id`)
const createRequireFromPath = require(`../../utils/create-require-from-path`)

function createFileContentHash(root, globPattern) {
const hash = crypto.createHash(`md5`)
Expand Down Expand Up @@ -46,9 +47,11 @@ const createPluginId = (name, pluginObject = null) =>
* 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.
* @param {string} rootDir
* This is the project location, from which are found the plugins
* @return {PluginInfo}
*/
function resolvePlugin(pluginName) {
function resolvePlugin(pluginName, rootDir) {
// Only find plugins when we're not given an absolute path
if (!existsSync(pluginName)) {
// Find the plugin in the local plugins folder
Expand Down Expand Up @@ -81,7 +84,8 @@ function resolvePlugin(pluginName) {
* which should be located in node_modules.
*/
try {
const resolvedPath = slash(path.dirname(require.resolve(pluginName)))
const projectRequire = createRequireFromPath(`${rootDir}/:internal:`)
const resolvedPath = slash(path.dirname(projectRequire.resolve(pluginName)))

const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
Expand Down Expand Up @@ -110,7 +114,7 @@ module.exports = (config = {}) => {
// Also test adding to redux store.
const processPlugin = plugin => {
if (_.isString(plugin)) {
const info = resolvePlugin(plugin)
const info = resolvePlugin(plugin, config.rootDir)

return {
...info,
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/joi-schemas/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Joi = require(`joi`)

export const gatsbyConfigSchema = Joi.object().keys({
__experimentalThemes: Joi.array(),
rootDir: Joi.string(),
polyfill: Joi.boolean(),
siteMetadata: Joi.object(),
pathPrefix: Joi.string(),
Expand Down
15 changes: 15 additions & 0 deletions packages/gatsby/src/utils/create-require-from-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Module = require(`module`)
const path = require(`path`)

// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0)
module.exports =
Module.createRequireFromPath ||
function(filename) {
const mod = new Module(filename, null)

mod.filename = filename
mod.paths = Module._nodeModulePaths(path.dirname(filename))
mod._compile(`module.exports = require;`, filename)

return mod.exports
}

0 comments on commit fc855d6

Please sign in to comment.