From d65def5c84503dbe3d680523de7a54fa5d5d8a00 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Mon, 7 Jan 2019 10:35:08 -0600 Subject: [PATCH 1/6] fix(gatsby-transformer-remark): correctly pass cache to sub plugins --- .../content/2018-12-14-hello-world.md | 2 ++ .../functionality/sub-plugin-caching.js | 16 ++++++++++++++++ .../development-runtime/gatsby-config.js | 2 +- .../gatsby-remark-subcache/constants.js | 1 + .../gatsby-remark-subcache/gatsby-node.js | 5 +++++ .../plugins/gatsby-remark-subcache/index.js | 11 +++++++++++ .../gatsby-remark-subcache/package.json | 3 +++ .../src/extend-node-type.js | 19 ++++++++++++++++--- .../src/on-node-create.js | 2 +- 9 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 e2e-tests/development-runtime/cypress/integration/functionality/sub-plugin-caching.js create mode 100644 e2e-tests/development-runtime/plugins/gatsby-remark-subcache/constants.js create mode 100644 e2e-tests/development-runtime/plugins/gatsby-remark-subcache/gatsby-node.js create mode 100644 e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js create mode 100644 e2e-tests/development-runtime/plugins/gatsby-remark-subcache/package.json diff --git a/e2e-tests/development-runtime/content/2018-12-14-hello-world.md b/e2e-tests/development-runtime/content/2018-12-14-hello-world.md index 7598fd3256790..cc4da9cdbf960 100644 --- a/e2e-tests/development-runtime/content/2018-12-14-hello-world.md +++ b/e2e-tests/development-runtime/content/2018-12-14-hello-world.md @@ -6,3 +6,5 @@ date: 2018-12-14 This is a truly meaningful blog post

%SUB_TITLE%

+ +

diff --git a/e2e-tests/development-runtime/cypress/integration/functionality/sub-plugin-caching.js b/e2e-tests/development-runtime/cypress/integration/functionality/sub-plugin-caching.js new file mode 100644 index 0000000000000..f45df00a8a5f3 --- /dev/null +++ b/e2e-tests/development-runtime/cypress/integration/functionality/sub-plugin-caching.js @@ -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`) + }) +}) diff --git a/e2e-tests/development-runtime/gatsby-config.js b/e2e-tests/development-runtime/gatsby-config.js index e229721485cea..07d6209963039 100644 --- a/e2e-tests/development-runtime/gatsby-config.js +++ b/e2e-tests/development-runtime/gatsby-config.js @@ -24,7 +24,7 @@ module.exports = { { resolve: `gatsby-transformer-remark`, options: { - plugins: [], + plugins: [`gatsby-remark-subcache`], }, }, `gatsby-plugin-sharp`, diff --git a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/constants.js b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/constants.js new file mode 100644 index 0000000000000..426ce562fdafa --- /dev/null +++ b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/constants.js @@ -0,0 +1 @@ +exports.id = `gatsby-remark-subcache-value` diff --git a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/gatsby-node.js b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/gatsby-node.js new file mode 100644 index 0000000000000..0c7d3f4524705 --- /dev/null +++ b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/gatsby-node.js @@ -0,0 +1,5 @@ +const { id } = require(`./constants`) + +exports.onPreBootstrap = async ({ cache }) => { + await cache.set(id, `Hello World`) +} diff --git a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js new file mode 100644 index 0000000000000..89e132aed0bf1 --- /dev/null +++ b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js @@ -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(/> `>${value}<`) + } + }) +} diff --git a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/package.json b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/package.json new file mode 100644 index 0000000000000..58b61fbeea21a --- /dev/null +++ b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/package.json @@ -0,0 +1,3 @@ +{ + "name": "gatsby-remark-subcache" +} diff --git a/packages/gatsby-transformer-remark/src/extend-node-type.js b/packages/gatsby-transformer-remark/src/extend-node-type.js index c6cf553f7b61f..b356822b5dcd8 100644 --- a/packages/gatsby-transformer-remark/src/extend-node-type.js +++ b/packages/gatsby-transformer-remark/src/extend-node-type.js @@ -67,7 +67,16 @@ const withPathPrefix = (url, pathPrefix) => const ASTPromiseMap = new Map() module.exports = ( - { type, store, pathPrefix, getNode, getNodesByType, cache, reporter }, + { + type, + pathPrefix, + getNode, + getNodesByType, + cache, + getCache, + reporter, + ...rest + }, pluginOptions ) => { if (type.name !== `MarkdownRemark`) { @@ -150,7 +159,9 @@ module.exports = ( files: fileNodes, getNode, reporter, - cache, + cache: getCache(plugin.name), + getCache, + ...rest, }, plugin.pluginOptions ) @@ -218,7 +229,9 @@ module.exports = ( files: fileNodes, pathPrefix, reporter, - cache, + cache: getCache(plugin.name), + getCache, + ...rest, }, plugin.pluginOptions ) diff --git a/packages/gatsby-transformer-remark/src/on-node-create.js b/packages/gatsby-transformer-remark/src/on-node-create.js index 76b2931ca82ad..37a273e739db7 100644 --- a/packages/gatsby-transformer-remark/src/on-node-create.js +++ b/packages/gatsby-transformer-remark/src/on-node-create.js @@ -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 From 9b5259c01ff82f98f78f674d8942de82af9d98f6 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 8 Jan 2019 13:30:34 -0600 Subject: [PATCH 2/6] fix(gatsby-transformer-remark): add a safe way to getCache --- .../src/extend-node-type.js | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-transformer-remark/src/extend-node-type.js b/packages/gatsby-transformer-remark/src/extend-node-type.js index b356822b5dcd8..1cdf2e8331aa3 100644 --- a/packages/gatsby-transformer-remark/src/extend-node-type.js +++ b/packages/gatsby-transformer-remark/src/extend-node-type.js @@ -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 }) => { + 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. @@ -73,7 +93,7 @@ module.exports = ( getNode, getNodesByType, cache, - getCache, + getCache: possibleGetCache, reporter, ...rest }, @@ -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 { From 6e9bb33cd3901fc60f95fae4fb6edcbde47c5a17 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 8 Jan 2019 13:39:33 -0600 Subject: [PATCH 3/6] chore: test broken test --- .../development-runtime/content/2018-12-14-hello-world.md | 2 +- e2e-tests/development-runtime/package.json | 2 +- .../plugins/gatsby-remark-subcache/index.js | 2 +- e2e-tests/development-runtime/src/components/title.js | 2 +- packages/gatsby-transformer-remark/package.json | 5 ++--- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/e2e-tests/development-runtime/content/2018-12-14-hello-world.md b/e2e-tests/development-runtime/content/2018-12-14-hello-world.md index cc4da9cdbf960..bdedb261138b7 100644 --- a/e2e-tests/development-runtime/content/2018-12-14-hello-world.md +++ b/e2e-tests/development-runtime/content/2018-12-14-hello-world.md @@ -7,4 +7,4 @@ This is a truly meaningful blog post

%SUB_TITLE%

-

+

%SUBCACHE_VALUE%

diff --git a/e2e-tests/development-runtime/package.json b/e2e-tests/development-runtime/package.json index f3dc3285df795..5943eb3ba9ba5 100644 --- a/e2e-tests/development-runtime/package.json +++ b/e2e-tests/development-runtime/package.json @@ -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", diff --git a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js index 89e132aed0bf1..afe24f4ba8452 100644 --- a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js +++ b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js @@ -5,7 +5,7 @@ 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(/> `>${value}<`) + node.value = node.value.replace(/%SUBCACHE_VALUE%/, () => `>${value}<`) } }) } diff --git a/e2e-tests/development-runtime/src/components/title.js b/e2e-tests/development-runtime/src/components/title.js index 3733199c9f939..3d2a97779a91f 100644 --- a/e2e-tests/development-runtime/src/components/title.js +++ b/e2e-tests/development-runtime/src/components/title.js @@ -1,3 +1,3 @@ import React from "react" -export default () =>

{`%TITLE%`}

+export default () =>

{`The title`}

diff --git a/packages/gatsby-transformer-remark/package.json b/packages/gatsby-transformer-remark/package.json index 0af0144d0d081..8c9430df17c70 100644 --- a/packages/gatsby-transformer-remark/package.json +++ b/packages/gatsby-transformer-remark/package.json @@ -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" + } } From 234039748501f76f1408f7710c40f5f6a1c205d8 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 8 Jan 2019 13:56:45 -0600 Subject: [PATCH 4/6] chore: fix error and re-factor test slightly --- e2e-tests/development-runtime/package.json | 2 +- .../plugins/gatsby-remark-subcache/index.js | 2 +- .../src/extend-node-type.js | 22 +++++-------------- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/e2e-tests/development-runtime/package.json b/e2e-tests/development-runtime/package.json index 5943eb3ba9ba5..078a580e87eda 100644 --- a/e2e-tests/development-runtime/package.json +++ b/e2e-tests/development-runtime/package.json @@ -4,7 +4,7 @@ "version": "1.0.0", "author": "Dustin Schau ", "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", diff --git a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js index afe24f4ba8452..57a1f51e598e9 100644 --- a/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js +++ b/e2e-tests/development-runtime/plugins/gatsby-remark-subcache/index.js @@ -5,7 +5,7 @@ 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}<`) + node.value = node.value.replace(/%SUBCACHE_VALUE%/, value) } }) } diff --git a/packages/gatsby-transformer-remark/src/extend-node-type.js b/packages/gatsby-transformer-remark/src/extend-node-type.js index 1cdf2e8331aa3..c4054ba1722aa 100644 --- a/packages/gatsby-transformer-remark/src/extend-node-type.js +++ b/packages/gatsby-transformer-remark/src/extend-node-type.js @@ -59,23 +59,11 @@ 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 }) => { - 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) +const safeGetCache = ({ getCache, cache }) => id => { + if (!getCache) { + return cache } + return getCache(id) } /** @@ -105,7 +93,7 @@ module.exports = ( pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``) pathPrefixCacheStr = pathPrefix || `` - const getCache = safeGetCache({ cache, getCache: possibleGetCache, reporter }) + const getCache = safeGetCache({ cache, getCache: possibleGetCache }) return new Promise((resolve, reject) => { // Setup Remark. From aa7fe002313fd1d2ca58923f96ecd1ba944e336a Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 8 Jan 2019 14:01:18 -0600 Subject: [PATCH 5/6] chore: revert file that should not have been committed --- e2e-tests/development-runtime/src/components/title.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e-tests/development-runtime/src/components/title.js b/e2e-tests/development-runtime/src/components/title.js index 3d2a97779a91f..3733199c9f939 100644 --- a/e2e-tests/development-runtime/src/components/title.js +++ b/e2e-tests/development-runtime/src/components/title.js @@ -1,3 +1,3 @@ import React from "react" -export default () =>

{`The title`}

+export default () =>

{`%TITLE%`}

From d5d627dd7938fde5e188e8e9e74579e8bcd45772 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 8 Jan 2019 14:11:35 -0600 Subject: [PATCH 6/6] test: get tests passing maybe (cc @pieh) --- e2e-tests/development-runtime/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e-tests/development-runtime/package.json b/e2e-tests/development-runtime/package.json index 078a580e87eda..c2a65599ab1f8 100644 --- a/e2e-tests/development-runtime/package.json +++ b/e2e-tests/development-runtime/package.json @@ -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 && exit 1", + "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",