From a64ddc023bc390880bb489638f1253f6201f42c5 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Fri, 2 Jun 2017 08:32:55 -0700 Subject: [PATCH] [1.0] Factor out node sourcing code into its own module (#1083) * Factor out sourcing nodes into its own module * Add explicit mapping for highlight.js cause jest can't find it for whatever reason --- jest.config.js | 1 + packages/gatsby/src/bootstrap/index.js | 7 ++- packages/gatsby/src/schema/index.js | 54 +---------------------- packages/gatsby/src/utils/source-nodes.js | 43 ++++++++++++++++++ 4 files changed, 52 insertions(+), 53 deletions(-) create mode 100644 packages/gatsby/src/utils/source-nodes.js diff --git a/jest.config.js b/jest.config.js index 079366db00539..9039dc1aebe56 100644 --- a/jest.config.js +++ b/jest.config.js @@ -22,5 +22,6 @@ module.exports = { "^graphql-skip-limit$": `/packages/graphql-skip-limit/src/index.js`, "^gatsby-plugin-sharp$": `/packages/gatsby-plugin-sharp/src/index.js`, "^gatsby/dist/redux/actions$": `/packages/gatsby/src/redux/actions.js`, + "^highlight.js$": `/node_modules/highlight.js/lib/index.js`, }, } diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index fe64a0de4f2c3..85f2a19a2054c 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -36,7 +36,7 @@ module.exports = async (program: any) => { console.log( `lib/bootstrap/index.js time since started:`, process.uptime(), - "sec" + `sec` ) // Fix program directory path for windows env @@ -221,6 +221,11 @@ data console.timeEnd(`copy gatsby files`) + // Source nodes + console.time(`initial sourcing and transforming nodes`) + await require(`../utils/source-nodes`)() + console.timeEnd(`initial sourcing and transforming nodes`) + // Create Schema. await require(`../schema`)() diff --git a/packages/gatsby/src/schema/index.js b/packages/gatsby/src/schema/index.js index ab0fdd0c161cf..15615b986a5de 100644 --- a/packages/gatsby/src/schema/index.js +++ b/packages/gatsby/src/schema/index.js @@ -2,15 +2,11 @@ const _ = require(`lodash`) const { GraphQLSchema, GraphQLObjectType } = require(`graphql`) -const apiRunner = require(`../utils/api-runner-node`) const buildNodeTypes = require(`./build-node-types`) const buildNodeConnections = require(`./build-node-connections`) -const { store, emitter, getNode } = require(`../redux`) -const { boundActionCreators } = require(`../redux/actions`) -const { deleteNodes } = boundActionCreators +const { store } = require(`../redux`) -async function buildSchema() { - console.time(`building schema`) +module.exports = async () => { const typesGQL = await buildNodeTypes() const connections = buildNodeConnections(_.values(typesGQL)) @@ -35,49 +31,3 @@ async function buildSchema() { return } - -module.exports = () => - new Promise(resolve => { - console.time(`initial sourcing and transforming nodes`) - apiRunner(`sourceNodes`, { - traceId: `initial-sourceNodes`, - waitForCascadingActions: true, - }).then(() => { - console.timeEnd(`initial sourcing and transforming nodes`) - const state = store.getState() - - // Garbage collect stale data nodes before creating the - // schema. - const touchedNodes = Object.keys(state.nodesTouched) - const staleNodes = _.values(state.nodes).filter(node => { - // Find the root node. - let rootNode = node - let whileCount = 0 - while ( - rootNode.parent && - getNode(rootNode.parent) !== undefined && - whileCount < 101 - ) { - rootNode = getNode(rootNode.parent) - whileCount += 1 - if (whileCount > 100) { - console.log( - `It looks like you have a node that's set its parent as itself`, - rootNode - ) - } - } - - return !_.includes(touchedNodes, rootNode.id) - }) - - if (staleNodes.length > 0) { - deleteNodes(staleNodes.map(n => n.id)) - } - - // Resolve promise once the schema is built. - buildSchema().then(() => { - resolve() - }) - }) - }) diff --git a/packages/gatsby/src/utils/source-nodes.js b/packages/gatsby/src/utils/source-nodes.js new file mode 100644 index 0000000000000..04feee60f5311 --- /dev/null +++ b/packages/gatsby/src/utils/source-nodes.js @@ -0,0 +1,43 @@ +const _ = require(`lodash`) + +const apiRunner = require(`./api-runner-node`) +const { store, getNode } = require(`../redux`) +const { boundActionCreators } = require(`../redux/actions`) +const { deleteNodes } = boundActionCreators + +module.exports = async () => { + await apiRunner(`sourceNodes`, { + traceId: `initial-sourceNodes`, + waitForCascadingActions: true, + }) + + const state = store.getState() + + // Garbage collect stale data nodes + const touchedNodes = Object.keys(state.nodesTouched) + const staleNodes = _.values(state.nodes).filter(node => { + // Find the root node. + let rootNode = node + let whileCount = 0 + while ( + rootNode.parent && + getNode(rootNode.parent) !== undefined && + whileCount < 101 + ) { + rootNode = getNode(rootNode.parent) + whileCount += 1 + if (whileCount > 100) { + console.log( + `It looks like you have a node that's set its parent as itself`, + rootNode + ) + } + } + + return !_.includes(touchedNodes, rootNode.id) + }) + + if (staleNodes.length > 0) { + deleteNodes(staleNodes.map(n => n.id)) + } +}