-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
pieh
merged 8 commits into
gatsbyjs:master
from
DSchau:gatsby-transformer-remark/sub-cache
Jan 8, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d65def5
fix(gatsby-transformer-remark): correctly pass cache to sub plugins
DSchau 9b5259c
fix(gatsby-transformer-remark): add a safe way to getCache
DSchau 6e9bb33
chore: test broken test
DSchau cc38488
Merge remote-tracking branch 'upstream/master' into gatsby-transforme…
DSchau b5eb1ab
Merge remote-tracking branch 'upstream/master' into gatsby-transforme…
DSchau 2340397
chore: fix error and re-factor test slightly
DSchau aa7fe00
chore: revert file that should not have been committed
DSchau d5d627d
test: get tests passing maybe (cc @pieh)
DSchau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
e2e-tests/development-runtime/cypress/integration/functionality/sub-plugin-caching.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* This e2e test validates that the cache structure | ||
* is unique per plugin (even sub-plugins) | ||
* and can interact between Gatsby lifecycles and a plugin | ||
*/ | ||
describe(`sub-plugin caching`, () => { | ||
beforeEach(() => { | ||
cy.visit(`/2018-12-14-hello-world/`).waitForAPI(`onRouteUpdate`) | ||
}) | ||
|
||
it(`has access to custom sub-plugin cache`, () => { | ||
cy.getTestElement(`gatsby-remark-subcache-value`) | ||
.invoke(`text`) | ||
.should(`eq`, `Hello World`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
e2e-tests/development-runtime/plugins/gatsby-remark-subcache/constants.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.id = `gatsby-remark-subcache-value` |
5 changes: 5 additions & 0 deletions
5
e2e-tests/development-runtime/plugins/gatsby-remark-subcache/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { id } = require(`./constants`) | ||
|
||
exports.onPreBootstrap = async ({ cache }) => { | ||
await cache.set(id, `Hello World`) | ||
} |
11 changes: 11 additions & 0 deletions
11
e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const visit = require(`unist-util-visit`) | ||
const { id } = require(`./constants`) | ||
|
||
module.exports = function remarkPlugin({ cache, markdownAST }) { | ||
visit(markdownAST, `html`, async node => { | ||
if (node.value.match(id)) { | ||
const value = await cache.get(id) | ||
node.value = node.value.replace(/%SUBCACHE_VALUE%/, value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 60% less jank ™️ |
||
} | ||
}) | ||
} |
3 changes: 3 additions & 0 deletions
3
e2e-tests/development-runtime/plugins/gatsby-remark-subcache/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "gatsby-remark-subcache" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,14 @@ const tableOfContentsCacheKey = node => | |
const withPathPrefix = (url, pathPrefix) => | ||
(pathPrefix + url).replace(/\/\//, `/`) | ||
|
||
// TODO: remove this check with next major release | ||
const safeGetCache = ({ getCache, cache }) => id => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for posterity, we already warn on conflicting peerDeps, so a second warning would be sorta redundant. |
||
if (!getCache) { | ||
return cache | ||
} | ||
return getCache(id) | ||
} | ||
|
||
/** | ||
* Map that keeps track of generation of AST to not generate it multiple | ||
* times in parallel. | ||
|
@@ -67,7 +75,16 @@ const withPathPrefix = (url, pathPrefix) => | |
const ASTPromiseMap = new Map() | ||
|
||
module.exports = ( | ||
{ type, store, pathPrefix, getNode, getNodesByType, cache, reporter }, | ||
{ | ||
type, | ||
pathPrefix, | ||
getNode, | ||
getNodesByType, | ||
cache, | ||
getCache: possibleGetCache, | ||
reporter, | ||
...rest | ||
}, | ||
pluginOptions | ||
) => { | ||
if (type.name !== `MarkdownRemark`) { | ||
|
@@ -76,6 +93,8 @@ module.exports = ( | |
pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``) | ||
pathPrefixCacheStr = pathPrefix || `` | ||
|
||
const getCache = safeGetCache({ cache, getCache: possibleGetCache }) | ||
|
||
return new Promise((resolve, reject) => { | ||
// Setup Remark. | ||
const { | ||
|
@@ -150,7 +169,9 @@ module.exports = ( | |
files: fileNodes, | ||
getNode, | ||
reporter, | ||
cache, | ||
cache: getCache(plugin.name), | ||
getCache, | ||
...rest, | ||
}, | ||
plugin.pluginOptions | ||
) | ||
|
@@ -218,7 +239,9 @@ module.exports = ( | |
files: fileNodes, | ||
pathPrefix, | ||
reporter, | ||
cache, | ||
cache: getCache(plugin.name), | ||
getCache, | ||
...rest, | ||
}, | ||
plugin.pluginOptions | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These don't really matter. Just for local stuff!