Skip to content

Commit

Permalink
[1.0] Fix bug where plugins as object with resolve but no options key…
Browse files Browse the repository at this point in the history
… fails to load (#1040)

* Fix plugin loading bug

* Simplify test to essence and don't try to require anything during tests

* doh
  • Loading branch information
KyleAMathews committed May 25, 2017
1 parent a66989d commit 4784b06
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Load plugins Loads plugins defined with an object but without an option key 1`] = `
Array [
Object {
"name": "component-page-creator",
"pluginOptions": Object {
"plugins": Array [],
},
"resolve": "",
"version": "1.0.0",
},
Object {
"name": "internal-data-bridge",
"pluginOptions": Object {
"plugins": Array [],
},
"resolve": "",
"version": "1.0.0",
},
Object {
"name": "TEST",
"pluginOptions": Object {
"plugins": Array [],
},
"resolve": "",
"version": undefined,
},
Object {
"name": "defaultSitePlugin",
"pluginOptions": Object {
"plugins": Array [],
},
"resolve": "",
"version": "n/a",
},
]
`;

exports[`Load plugins load plugins for a site 1`] = `
Array [
Object {
Expand Down
33 changes: 29 additions & 4 deletions packages/gatsby/src/bootstrap/__tests__/load-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,35 @@ describe(`Load plugins`, () => {

// Delete the resolve path as that varies depending
// on platform so will cause snapshots to differ.
plugins = plugins.map(plugin => {return ({
...plugin,
resolve: ``,
})})
plugins = plugins.map(plugin => {
return {
...plugin,
resolve: ``,
}
})

expect(plugins).toMatchSnapshot()
})

it(`Loads plugins defined with an object but without an option key`, async () => {
const config = {
plugins: [
{
resolve: `___TEST___`,
},
],
}

let plugins = await loadPlugins(config)

// Delete the resolve path as that varies depending
// on platform so will cause snapshots to differ.
plugins = plugins.map(plugin => {
return {
...plugin,
resolve: ``,
}
})

expect(plugins).toMatchSnapshot()
})
Expand Down
17 changes: 12 additions & 5 deletions packages/gatsby/src/bootstrap/load-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ module.exports = async config => {
plugin.options.plugins.forEach(p => {
subplugins.push(processPlugin(p))
})

plugin.options.plugins = subplugins
}
plugin.options.plugins = subplugins

const resolvedPath = slash(path.dirname(require.resolve(plugin.resolve)))
const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
// Add some default values for tests as we don't actually
// want to try to load anything during tests.
let resolvedPath
let packageJSON = { name: `TEST` }
if (plugin.resolve !== `___TEST___`) {
resolvedPath = slash(path.dirname(require.resolve(plugin.resolve)))
packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
}
return {
resolve: resolvedPath,
name: packageJSON.name,
Expand Down

0 comments on commit 4784b06

Please sign in to comment.