Skip to content

Commit 8421707

Browse files
oorestisimepieh
authored andcommitted
feat(gatsby): warn user about incompatible plugins (#10034)
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/ -->
1 parent cadf9f3 commit 8421707

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

packages/gatsby/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"redux": "^4.0.0",
103103
"relay-compiler": "1.5.0",
104104
"request": "^2.85.0",
105+
"semver": "^5.6.0",
105106
"shallow-compare": "^1.2.2",
106107
"sift": "^5.1.0",
107108
"signal-exit": "^3.0.2",

packages/gatsby/src/bootstrap/load-plugins/__tests__/validate.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
collatePluginAPIs,
1212
handleBadExports,
1313
handleMultipleReplaceRenderers,
14+
warnOnIncompatiblePeerDependency,
1415
} = require(`../validate`)
1516

1617
describe(`collatePluginAPIs`, () => {
@@ -198,3 +199,27 @@ describe(`handleMultipleReplaceRenderers`, () => {
198199
expect(result).toMatchSnapshot()
199200
})
200201
})
202+
203+
describe(`warnOnIncompatiblePeerDependency`, () => {
204+
beforeEach(() => {
205+
reporter.warn.mockClear()
206+
})
207+
208+
it(`Does not warn when no peer dependency`, () => {
209+
warnOnIncompatiblePeerDependency(`dummy-package`, { peerDependencies: {} })
210+
211+
expect(reporter.warn).not.toHaveBeenCalled()
212+
})
213+
214+
it(`Warns on incompatible gatsby peer dependency`, async () => {
215+
warnOnIncompatiblePeerDependency(`dummy-package`, {
216+
peerDependencies: {
217+
gatsby: `<2.0.0`,
218+
},
219+
})
220+
221+
expect(reporter.warn).toHaveBeenCalledWith(
222+
expect.stringContaining(`Plugin dummy-package is not compatible`)
223+
)
224+
})
225+
})

packages/gatsby/src/bootstrap/load-plugins/load.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fs = require(`fs`)
44
const path = require(`path`)
55
const crypto = require(`crypto`)
66
const glob = require(`glob`)
7+
const { warnOnIncompatiblePeerDependency } = require(`./validate`)
78
const { store } = require(`../../redux`)
89
const existsSync = require(`fs-exists-cached`).sync
910
const createNodeId = require(`../../utils/create-node-id`)
@@ -59,6 +60,8 @@ function resolvePlugin(pluginName) {
5960
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
6061
)
6162
const name = packageJSON.name || pluginName
63+
warnOnIncompatiblePeerDependency(name, packageJSON)
64+
6265
return {
6366
resolve: resolvedPath,
6467
name,
@@ -83,6 +86,7 @@ function resolvePlugin(pluginName) {
8386
const packageJSON = JSON.parse(
8487
fs.readFileSync(`${resolvedPath}/package.json`, `utf-8`)
8588
)
89+
warnOnIncompatiblePeerDependency(packageJSON.name, packageJSON)
8690

8791
return {
8892
resolve: resolvedPath,

packages/gatsby/src/bootstrap/load-plugins/validate.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const _ = require(`lodash`)
2-
2+
const semver = require(`semver`)
3+
const { version: gatsbyVersion } = require(`gatsby/package.json`)
34
const reporter = require(`gatsby-cli/lib/reporter`)
45
const resolveModuleExports = require(`../resolve-module-exports`)
56

@@ -214,8 +215,22 @@ const handleMultipleReplaceRenderers = ({ apiToPlugins, flattenedPlugins }) => {
214215
return flattenedPlugins
215216
}
216217

218+
function warnOnIncompatiblePeerDependency(name, packageJSON) {
219+
// Note: In the future the peer dependency should be enforced for all plugins.
220+
const gatsbyPeerDependency = _.get(packageJSON, `peerDependencies.gatsby`)
221+
if (
222+
gatsbyPeerDependency &&
223+
!semver.satisfies(gatsbyVersion, gatsbyPeerDependency)
224+
) {
225+
reporter.warn(
226+
`Plugin ${name} is not compatible with your gatsby version ${gatsbyVersion} - It requires gatsby@${gatsbyPeerDependency}`
227+
)
228+
}
229+
}
230+
217231
module.exports = {
218232
collatePluginAPIs,
219233
handleBadExports,
220234
handleMultipleReplaceRenderers,
235+
warnOnIncompatiblePeerDependency,
221236
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16746,6 +16746,11 @@ semver@^4.0.3:
1674616746
resolved "https://registry.yarnpkg.com/semver/-/semver-4.3.6.tgz#300bc6e0e86374f7ba61068b5b1ecd57fc6532da"
1674716747
integrity sha1-MAvG4OhjdPe6YQaLWx7NV/xlMto=
1674816748

16749+
semver@^5.6.0:
16750+
version "5.6.0"
16751+
resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004"
16752+
integrity sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==
16753+
1674916754
semver@~5.3.0:
1675016755
version "5.3.0"
1675116756
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"

0 commit comments

Comments
 (0)