Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-transformer-remark): correctly pass cache to sub plugins #10892

Merged
merged 8 commits into from
Jan 8, 2019
Prev Previous commit
Next Next commit
fix(gatsby-transformer-remark): add a safe way to getCache
  • Loading branch information
DSchau committed Jan 8, 2019
commit 9b5259c01ff82f98f78f674d8942de82af9d98f6
24 changes: 23 additions & 1 deletion packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ const tableOfContentsCacheKey = node =>
const withPathPrefix = (url, pathPrefix) =>
(pathPrefix + url).replace(/\/\//, `/`)

// TODO: remove this check with next major release
// the getCache feature was implemented in gatsby@2.0.88
const safeGetCache = ({ getCache, cache, reporter }) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pieh this warning looks like:

success onPreBootstrap — 0.004 s
success source and transform nodes — 0.072 s
success building schema — 0.175 s
success createPages — 0.030 s
success createPagesStatefully — 0.021 s
success onPreExtractQueries — 0.003 s
success update schema — 0.102 s
success extract queries from components — 0.119 s
warning It looks like you're using a slightly outdated version of gatsby with gatsby-transformer-remark.
To update, run 'npm install gatsby@^2.0.86 --save' or 'yarn add gatsby@^2.0.86'

I thought about using onPreBootstrap but I like how we can remove this code all in one spot (easier refactoring). Up for whatever though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it'd be cool to think more broadly about how we introduce non-backwards compatible API changes, and if we could improve this.

For now this will work fine.

const GATSBY_VERSION_WITH_GET_CACHE = `2.0.88`
let warned = false
return id => {
if (!getCache) {
if (!warned) {
warned = true
reporter.warn(reporter.stripIndent`
It looks like you're using a slightly outdated version of gatsby with gatsby-transformer-remark.
To update, run 'npm install gatsby@^${GATSBY_VERSION_WITH_GET_CACHE} --save' or 'yarn add gatsby@^${GATSBY_VERSION_WITH_GET_CACHE}'
`)
}
return cache
}
return getCache(id)
}
}

/**
* Map that keeps track of generation of AST to not generate it multiple
* times in parallel.
Expand All @@ -73,7 +93,7 @@ module.exports = (
getNode,
getNodesByType,
cache,
getCache,
getCache: possibleGetCache,
reporter,
...rest
},
Expand All @@ -85,6 +105,8 @@ module.exports = (
pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``)
pathPrefixCacheStr = pathPrefix || ``

const getCache = safeGetCache({ cache, getCache: possibleGetCache, reporter })

return new Promise((resolve, reject) => {
// Setup Remark.
const {
Expand Down