Skip to content

Commit

Permalink
feat(gatsby): warn user about incompatible plugins (#10034)
Browse files Browse the repository at this point in the history
Closes: #7143 #9731
<!--
  Q. Which branch should I use for my pull request?
  A. Use `master` branch (probably).

  Q. Which branch if my change is a bug fix for Gatsby v1?
  A. In this case, you should use the `v1` branch

  Q. Which branch if I'm still not sure?
  A. Use `master` branch. Ask in the PR if you're not sure and a Gatsby maintainer will be happy to help :)

  Note: We will only accept bug fixes for Gatsby v1. New features should be added to Gatsby v2.

  Learn more about contributing: https://www.gatsbyjs.org/docs/how-to-contribute/
-->
  • Loading branch information
oorestisime authored and pieh committed Nov 21, 2018
1 parent cadf9f3 commit 8421707
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"redux": "^4.0.0",
"relay-compiler": "1.5.0",
"request": "^2.85.0",
"semver": "^5.6.0",
"shallow-compare": "^1.2.2",
"sift": "^5.1.0",
"signal-exit": "^3.0.2",
Expand Down
25 changes: 25 additions & 0 deletions packages/gatsby/src/bootstrap/load-plugins/__tests__/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
collatePluginAPIs,
handleBadExports,
handleMultipleReplaceRenderers,
warnOnIncompatiblePeerDependency,
} = require(`../validate`)

describe(`collatePluginAPIs`, () => {
Expand Down Expand Up @@ -198,3 +199,27 @@ describe(`handleMultipleReplaceRenderers`, () => {
expect(result).toMatchSnapshot()
})
})

describe(`warnOnIncompatiblePeerDependency`, () => {
beforeEach(() => {
reporter.warn.mockClear()
})

it(`Does not warn when no peer dependency`, () => {
warnOnIncompatiblePeerDependency(`dummy-package`, { peerDependencies: {} })

expect(reporter.warn).not.toHaveBeenCalled()
})

it(`Warns on incompatible gatsby peer dependency`, async () => {
warnOnIncompatiblePeerDependency(`dummy-package`, {
peerDependencies: {
gatsby: `<2.0.0`,
},
})

expect(reporter.warn).toHaveBeenCalledWith(
expect.stringContaining(`Plugin dummy-package is not compatible`)
)
})
})
4 changes: 4 additions & 0 deletions packages/gatsby/src/bootstrap/load-plugins/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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`)
Expand Down Expand Up @@ -59,6 +60,8 @@ function resolvePlugin(pluginName) {
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
const name = packageJSON.name || pluginName
warnOnIncompatiblePeerDependency(name, packageJSON)

return {
resolve: resolvedPath,
name,
Expand All @@ -83,6 +86,7 @@ function resolvePlugin(pluginName) {
const packageJSON = JSON.parse(
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
)
warnOnIncompatiblePeerDependency(packageJSON.name, packageJSON)

return {
resolve: resolvedPath,
Expand Down
17 changes: 16 additions & 1 deletion packages/gatsby/src/bootstrap/load-plugins/validate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const _ = require(`lodash`)

const semver = require(`semver`)
const { version: gatsbyVersion } = require(`gatsby/package.json`)
const reporter = require(`gatsby-cli/lib/reporter`)
const resolveModuleExports = require(`../resolve-module-exports`)

Expand Down Expand Up @@ -214,8 +215,22 @@ const handleMultipleReplaceRenderers = ({ apiToPlugins, flattenedPlugins }) => {
return flattenedPlugins
}

function warnOnIncompatiblePeerDependency(name, packageJSON) {
// Note: In the future the peer dependency should be enforced for all plugins.
const gatsbyPeerDependency = _.get(packageJSON, `peerDependencies.gatsby`)
if (
gatsbyPeerDependency &&
!semver.satisfies(gatsbyVersion, gatsbyPeerDependency)
) {
reporter.warn(
`Plugin ${name} is not compatible with your gatsby version ${gatsbyVersion} - It requires gatsby@${gatsbyPeerDependency}`
)
}
}

module.exports = {
collatePluginAPIs,
handleBadExports,
handleMultipleReplaceRenderers,
warnOnIncompatiblePeerDependency,
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16746,6 +16746,11 @@ semver@^4.0.3:
resolved "https://registry.yarnpkg.com/semver/-/semver-4.3.6.tgz#300bc6e0e86374f7ba61068b5b1ecd57fc6532da"
integrity sha1-MAvG4OhjdPe6YQaLWx7NV/xlMto=

semver@^5.6.0:
version "5.6.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004"
integrity sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==

semver@~5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
Expand Down

0 comments on commit 8421707

Please sign in to comment.