Skip to content

Commit

Permalink
fix(gatsby-transformer-remark): correctly pass cache to sub plugins (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DSchau authored and pieh committed Jan 8, 2019
1 parent 41c7fd4 commit 8ea9a52
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 10 deletions.
Expand Up @@ -6,3 +6,5 @@ date: 2018-12-14
This is a truly meaningful blog post

<h2 data-testid="sub-title">%SUB_TITLE%</h2>

<h2 data-testid="gatsby-remark-subcache-value">%SUBCACHE_VALUE%</h2>
@@ -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`)
})
})
2 changes: 1 addition & 1 deletion e2e-tests/development-runtime/gatsby-config.js
Expand Up @@ -24,7 +24,7 @@ module.exports = {
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [],
plugins: [`gatsby-remark-subcache`],
},
},
`gatsby-plugin-sharp`,
Expand Down
4 changes: 2 additions & 2 deletions e2e-tests/development-runtime/package.json
Expand Up @@ -4,7 +4,7 @@
"version": "1.0.0",
"author": "Dustin Schau <dustin@gatsbyjs.com>",
"dependencies": {
"gatsby": "^2.0.71",
"gatsby": "^2.0.88",
"gatsby-image": "^2.0.20",
"gatsby-plugin-manifest": "^2.0.9",
"gatsby-plugin-offline": "^2.0.20",
Expand All @@ -29,7 +29,7 @@
"serve": "gatsby serve",
"start": "npm run develop",
"format": "prettier --write \"src/**/*.js\"",
"test": "npm run start-server-and-test || npm run reset",
"test": "npm run start-server-and-test || (npm run reset && exit 1)",
"posttest": "npm run reset",
"reset": "node scripts/reset.js",
"update": "node scripts/update.js",
Expand Down
@@ -0,0 +1 @@
exports.id = `gatsby-remark-subcache-value`
@@ -0,0 +1,5 @@
const { id } = require(`./constants`)

exports.onPreBootstrap = async ({ cache }) => {
await cache.set(id, `Hello World`)
}
@@ -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)
}
})
}
@@ -0,0 +1,3 @@
{
"name": "gatsby-remark-subcache"
}
5 changes: 2 additions & 3 deletions packages/gatsby-transformer-remark/package.json
Expand Up @@ -42,13 +42,12 @@
],
"license": "MIT",
"peerDependencies": {
"gatsby": "^2.0.33"
"gatsby": "^2.0.88"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-remark",
"scripts": {
"build": "babel src --out-dir . --ignore **/__tests__",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore **/__tests__"
},
"gitHead": "5bd5aebe066b9875354a81a4b9ed98722731c465"
}
}
29 changes: 26 additions & 3 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Expand Up @@ -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 => {
if (!getCache) {
return cache
}
return getCache(id)
}

/**
* Map that keeps track of generation of AST to not generate it multiple
* times in parallel.
Expand All @@ -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`) {
Expand All @@ -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 {
Expand Down Expand Up @@ -150,7 +169,9 @@ module.exports = (
files: fileNodes,
getNode,
reporter,
cache,
cache: getCache(plugin.name),
getCache,
...rest,
},
plugin.pluginOptions
)
Expand Down Expand Up @@ -218,7 +239,9 @@ module.exports = (
files: fileNodes,
pathPrefix,
reporter,
cache,
cache: getCache(plugin.name),
getCache,
...rest,
},
plugin.pluginOptions
)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-remark/src/on-node-create.js
Expand Up @@ -3,7 +3,7 @@ const crypto = require(`crypto`)
const _ = require(`lodash`)

module.exports = async function onCreateNode(
{ node, getNode, loadNodeContent, actions, createNodeId, reporter },
{ node, loadNodeContent, actions, createNodeId, reporter },
pluginOptions
) {
const { createNode, createParentChildLink } = actions
Expand Down

0 comments on commit 8ea9a52

Please sign in to comment.