From fd8e8d0dcd71f4ed605d1c2166f21ea3560d384e Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Thu, 27 Jun 2019 13:31:35 +0530 Subject: [PATCH 01/13] Parse code in node_modules as well --- packages/gatsby/src/query/query-compiler.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index b5394bfdb2d9b..268df85b91fcf 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -101,6 +101,7 @@ class Runner { const filesRegex = path.join(`/**`, `*.+(t|j)s?(x)`) let files = [ path.join(this.base, `src`), + path.join(this.base, `node_modules`), path.join(this.base, `.cache`, `fragments`), ] .concat(this.additional.map(additional => path.join(additional, `src`))) @@ -114,14 +115,15 @@ class Runner { [] ) files = files.filter(d => !d.match(/\.d\.ts$/)) + files = files.filter(d => !d.match(/gatsby/)) files = files.map(normalize) // Ensure all page components added as they're not necessarily in the - // pages directory e.g. a plugin could add a page component. Plugins + // pages directory e.g. a plugin could add a page component. Plugins // *should* copy their components (if they add a query) to .cache so that - // our babel plugin to remove the query on building is active (we don't - // run babel on code in node_modules). Otherwise the component will throw - // an error in the browser of "graphql is not defined". + // our babel plugin to remove the query on building is active. + // Otherwise the component will throw an error in the browser of + // "graphql is not defined". files = files.concat( Array.from(store.getState().components.keys(), c => normalize(c)) ) From cb04b2e91931220adb8cd7c78b53090e8f4b43e8 Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Thu, 27 Jun 2019 20:06:38 +0530 Subject: [PATCH 02/13] Ignore gatsby-transformer-sharp and gatsby --- packages/gatsby/src/query/query-compiler.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index 268df85b91fcf..f37b11542344c 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -115,7 +115,12 @@ class Runner { [] ) files = files.filter(d => !d.match(/\.d\.ts$/)) - files = files.filter(d => !d.match(/gatsby/)) + + // Added this to avoid the duplicate definitions RelayParser error + // Worst hack ever + files = files.filter(d => !d.match(/\/gatsby-transformer-sharp\//)) + files = files.filter(d => !d.match(/\/gatsby\//)) + files = files.map(normalize) // Ensure all page components added as they're not necessarily in the From 87fdce192177b8a230e1c462000d3d50cc338f19 Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 15:00:11 +0530 Subject: [PATCH 03/13] Transpile node_modules during develop, sorry Ward --- packages/gatsby/src/utils/webpack.config.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/gatsby/src/utils/webpack.config.js b/packages/gatsby/src/utils/webpack.config.js index 1cb4cadf35a6e..1a2e72d80885c 100644 --- a/packages/gatsby/src/utils/webpack.config.js +++ b/packages/gatsby/src/utils/webpack.config.js @@ -246,18 +246,10 @@ module.exports = async (program, directory, suppliedStage) => { } function getModule() { - const jsOptions = {} - - // Speedup 🏎️💨 the build! We only include transpilation of node_modules on production builds - // TODO create gatsby plugin to enable this behaviour on develop (only when people are requesting this feature) - if (stage === `develop`) { - jsOptions.exclude = [`node_modules`] - } - // Common config for every env. // prettier-ignore let configRules = [ - rules.js(jsOptions), + rules.js(), rules.yaml(), rules.fonts(), rules.images(), From 206938376738a5f24362ea977a8a031b14e0d210 Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 15:25:17 +0530 Subject: [PATCH 04/13] Only parse modules that use gatsby in deps or peer deps --- packages/gatsby/src/query/query-compiler.js | 19 ++++++++++++++++++- yarn.lock | 7 +++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index f37b11542344c..bbe0a4292e339 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -3,6 +3,7 @@ import path from "path" const normalize = require(`normalize-path`) import glob from "glob" const levenshtein = require(`fast-levenshtein`) +const fs = require(`fs`) import { validate } from "graphql" import { IRTransforms } from "@gatsbyjs/relay-compiler" @@ -11,6 +12,9 @@ import ASTConvert from "@gatsbyjs/relay-compiler/lib/ASTConvert" import GraphQLCompilerContext from "@gatsbyjs/relay-compiler/lib/GraphQLCompilerContext" import filterContextForNode from "@gatsbyjs/relay-compiler/lib/filterContextForNode" const _ = require(`lodash`) +const util = require(`util`) +const readPackageTree = require(`read-package-tree`) +const readPackageTreeAsync = util.promisify(readPackageTree) import { store } from "../redux" const { boundActionCreators } = require(`../redux/actions`) @@ -99,12 +103,22 @@ class Runner { async parseEverything() { const filesRegex = path.join(`/**`, `*.+(t|j)s?(x)`) + + const allNodeModules = await readPackageTreeAsync(this.base, () => true) + + const modulesThatUseGatsby = allNodeModules.children.filter( + node => + (node.package.dependencies && node.package.dependencies[`gatsby`]) || + (node.package.peerDependencies && + node.package.peerDependencies[`gatsby`]) + ) + let files = [ path.join(this.base, `src`), - path.join(this.base, `node_modules`), path.join(this.base, `.cache`, `fragments`), ] .concat(this.additional.map(additional => path.join(additional, `src`))) + .concat(modulesThatUseGatsby.map(module => module.path)) .reduce( (merged, folderPath) => merged.concat( @@ -114,6 +128,9 @@ class Runner { ), [] ) + + fs.writeFileSync(path.join(this.base, `testfiles`), files.join(`\n`)) + files = files.filter(d => !d.match(/\.d\.ts$/)) // Added this to avoid the duplicate definitions RelayParser error diff --git a/yarn.lock b/yarn.lock index 194c62c558eee..5fd7f017683e4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -17095,10 +17095,17 @@ read-package-tree@^5.1.6: read-package-json "^2.0.0" readdir-scoped-modules "^1.0.0" +<<<<<<< HEAD read-package-tree@^5.3.1: version "5.3.1" resolved "https://registry.yarnpkg.com/read-package-tree/-/read-package-tree-5.3.1.tgz#a32cb64c7f31eb8a6f31ef06f9cedf74068fe636" integrity sha512-mLUDsD5JVtlZxjSlPPx1RETkNjjvQYuweKwNVt1Sn8kP5Jh44pvYuUHCp6xSVDZWbNxVxG5lyZJ921aJH61sTw== +======= +read-package-tree@^5.3.0: + version "5.3.0" + resolved "https://registry.yarnpkg.com/read-package-tree/-/read-package-tree-5.3.0.tgz#4f95472e45e7145fb77f4069d12844b139f5ea12" + integrity sha512-Gi64+EWmi4515E1rPR77ae/Ip8cjFQTlsWytSYJj974U0tSnxm67pyXltbDjB1lvLw4dc85HbtidGL1K2c/oxw== +>>>>>>> Only parse modules that use gatsby in deps or peer deps dependencies: read-package-json "^2.0.0" readdir-scoped-modules "^1.0.0" From cc33623f636b82c08a1c2133d928c548a27fc3c2 Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 16:25:47 +0530 Subject: [PATCH 05/13] Switch to gatsby-dependents --- packages/gatsby/src/query/query-compiler.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index bbe0a4292e339..6eef679b2dc4d 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -12,9 +12,7 @@ import ASTConvert from "@gatsbyjs/relay-compiler/lib/ASTConvert" import GraphQLCompilerContext from "@gatsbyjs/relay-compiler/lib/GraphQLCompilerContext" import filterContextForNode from "@gatsbyjs/relay-compiler/lib/filterContextForNode" const _ = require(`lodash`) -const util = require(`util`) -const readPackageTree = require(`read-package-tree`) -const readPackageTreeAsync = util.promisify(readPackageTree) +import getGatsbyDependents from "../utils/gatsby-dependents" import { store } from "../redux" const { boundActionCreators } = require(`../redux/actions`) @@ -104,14 +102,7 @@ class Runner { async parseEverything() { const filesRegex = path.join(`/**`, `*.+(t|j)s?(x)`) - const allNodeModules = await readPackageTreeAsync(this.base, () => true) - - const modulesThatUseGatsby = allNodeModules.children.filter( - node => - (node.package.dependencies && node.package.dependencies[`gatsby`]) || - (node.package.peerDependencies && - node.package.peerDependencies[`gatsby`]) - ) + const modulesThatUseGatsby = await getGatsbyDependents() let files = [ path.join(this.base, `src`), From 34bf7e0200fb6b5e8709bb2515c6095339aa55fd Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 16:35:29 +0530 Subject: [PATCH 06/13] Fix lock file --- yarn.lock | 7 ------- 1 file changed, 7 deletions(-) diff --git a/yarn.lock b/yarn.lock index 5fd7f017683e4..194c62c558eee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -17095,17 +17095,10 @@ read-package-tree@^5.1.6: read-package-json "^2.0.0" readdir-scoped-modules "^1.0.0" -<<<<<<< HEAD read-package-tree@^5.3.1: version "5.3.1" resolved "https://registry.yarnpkg.com/read-package-tree/-/read-package-tree-5.3.1.tgz#a32cb64c7f31eb8a6f31ef06f9cedf74068fe636" integrity sha512-mLUDsD5JVtlZxjSlPPx1RETkNjjvQYuweKwNVt1Sn8kP5Jh44pvYuUHCp6xSVDZWbNxVxG5lyZJ921aJH61sTw== -======= -read-package-tree@^5.3.0: - version "5.3.0" - resolved "https://registry.yarnpkg.com/read-package-tree/-/read-package-tree-5.3.0.tgz#4f95472e45e7145fb77f4069d12844b139f5ea12" - integrity sha512-Gi64+EWmi4515E1rPR77ae/Ip8cjFQTlsWytSYJj974U0tSnxm67pyXltbDjB1lvLw4dc85HbtidGL1K2c/oxw== ->>>>>>> Only parse modules that use gatsby in deps or peer deps dependencies: read-package-json "^2.0.0" readdir-scoped-modules "^1.0.0" From 742543f421e9f6f68df19b21613433e1277b987b Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 16:40:18 +0530 Subject: [PATCH 07/13] Remove manual plugin page addition --- packages/gatsby/src/query/query-compiler.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index 6eef679b2dc4d..bf71f1bec4f70 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -131,15 +131,6 @@ class Runner { files = files.map(normalize) - // Ensure all page components added as they're not necessarily in the - // pages directory e.g. a plugin could add a page component. Plugins - // *should* copy their components (if they add a query) to .cache so that - // our babel plugin to remove the query on building is active. - // Otherwise the component will throw an error in the browser of - // "graphql is not defined". - files = files.concat( - Array.from(store.getState().components.keys(), c => normalize(c)) - ) files = _.uniq(files) let parser = new FileParser() From 6cdfdf6944a9929ff1ea73d42f1dcc53e70846c3 Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 16:50:32 +0530 Subject: [PATCH 08/13] Revert "Remove manual plugin page addition" This reverts commit 742543f421e9f6f68df19b21613433e1277b987b. --- packages/gatsby/src/query/query-compiler.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index bf71f1bec4f70..6eef679b2dc4d 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -131,6 +131,15 @@ class Runner { files = files.map(normalize) + // Ensure all page components added as they're not necessarily in the + // pages directory e.g. a plugin could add a page component. Plugins + // *should* copy their components (if they add a query) to .cache so that + // our babel plugin to remove the query on building is active. + // Otherwise the component will throw an error in the browser of + // "graphql is not defined". + files = files.concat( + Array.from(store.getState().components.keys(), c => normalize(c)) + ) files = _.uniq(files) let parser = new FileParser() From c2406d018633c0325eb643a3871fa1a72a68ff7e Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 16:54:24 +0530 Subject: [PATCH 09/13] Add comment explaining revert --- packages/gatsby/src/query/query-compiler.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index 6eef679b2dc4d..c5210690d4699 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -131,6 +131,11 @@ class Runner { files = files.map(normalize) + // We should be able to remove the following and preliminary tests do suggest + // that they aren't needed anymore since we transpile node_modules now + // However, there could be some cases (where a page is outside of src for example) + // that warrant keeping this and removing later once we have more confidence (and tests) + // Ensure all page components added as they're not necessarily in the // pages directory e.g. a plugin could add a page component. Plugins // *should* copy their components (if they add a query) to .cache so that @@ -140,6 +145,7 @@ class Runner { files = files.concat( Array.from(store.getState().components.keys(), c => normalize(c)) ) + files = _.uniq(files) let parser = new FileParser() From 7050786be73ee06bd94bdc79d25e29f8987c40a2 Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 16:59:36 +0530 Subject: [PATCH 10/13] Remove debug stuff --- packages/gatsby/src/query/query-compiler.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index c5210690d4699..50d80afd3dbfd 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -3,7 +3,6 @@ import path from "path" const normalize = require(`normalize-path`) import glob from "glob" const levenshtein = require(`fast-levenshtein`) -const fs = require(`fs`) import { validate } from "graphql" import { IRTransforms } from "@gatsbyjs/relay-compiler" @@ -120,8 +119,6 @@ class Runner { [] ) - fs.writeFileSync(path.join(this.base, `testfiles`), files.join(`\n`)) - files = files.filter(d => !d.match(/\.d\.ts$/)) // Added this to avoid the duplicate definitions RelayParser error From fdf0233280e5bd6bda31fd2a325ef0970362b40d Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 17:28:10 +0530 Subject: [PATCH 11/13] Remove fragment dedupe hack --- packages/gatsby/src/query/query-compiler.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index 50d80afd3dbfd..dd37f37f22309 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -121,11 +121,6 @@ class Runner { files = files.filter(d => !d.match(/\.d\.ts$/)) - // Added this to avoid the duplicate definitions RelayParser error - // Worst hack ever - files = files.filter(d => !d.match(/\/gatsby-transformer-sharp\//)) - files = files.filter(d => !d.match(/\/gatsby\//)) - files = files.map(normalize) // We should be able to remove the following and preliminary tests do suggest From 9d8408798e6dd481e6e6db256f48d85db707e6e3 Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 20:13:11 +0530 Subject: [PATCH 12/13] Filter out duplicated fragments --- packages/gatsby/src/query/query-compiler.js | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index dd37f37f22309..9e9324746edd4 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -39,6 +39,8 @@ const { ScalarLeafsRule, VariablesAreInputTypesRule, VariablesInAllowedPositionRule, + Kind, + print, } = require(`graphql`) type RootQuery = { @@ -151,6 +153,7 @@ class Runner { const nameDefMap = new Map() const nameErrorMap = new Map() const documents = [] + const fragmentMap = new Map() for (let [filePath, doc] of nodes.entries()) { let errors = validate(this.schema, doc, validationRules) @@ -185,6 +188,25 @@ class Runner { return compiledNodes } + // The way we currently export fragments requires duplicated ones + // to be filtered out since there is a global Fragment namespace + // We maintain a top level fragment Map to keep track of all definitions + // of thge fragment type and to filter them out if theythey've already been + // declared before + doc.definitions = doc.definitions.filter(definition => { + if (definition.kind === Kind.FRAGMENT_DEFINITION) { + const fragmentName = definition.name.value + if (fragmentMap.has(fragmentName)) { + if (print(definition) === fragmentMap.get(fragmentName)) { + return false + } + } else { + fragmentMap.set(fragmentName, print(definition)) + } + } + return true + }) + documents.push(doc) doc.definitions.forEach((def: any) => { const name: string = def.name.value From ac1ff571eedd39ce3ea7c30ea36e6028aae437ad Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Mon, 1 Jul 2019 20:32:39 +0530 Subject: [PATCH 13/13] Don't parse files in node_modules --- packages/gatsby/src/query/query-compiler.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/gatsby/src/query/query-compiler.js b/packages/gatsby/src/query/query-compiler.js index 9e9324746edd4..64294c556c69d 100644 --- a/packages/gatsby/src/query/query-compiler.js +++ b/packages/gatsby/src/query/query-compiler.js @@ -11,7 +11,6 @@ import ASTConvert from "@gatsbyjs/relay-compiler/lib/ASTConvert" import GraphQLCompilerContext from "@gatsbyjs/relay-compiler/lib/GraphQLCompilerContext" import filterContextForNode from "@gatsbyjs/relay-compiler/lib/filterContextForNode" const _ = require(`lodash`) -import getGatsbyDependents from "../utils/gatsby-dependents" import { store } from "../redux" const { boundActionCreators } = require(`../redux/actions`) @@ -103,14 +102,11 @@ class Runner { async parseEverything() { const filesRegex = path.join(`/**`, `*.+(t|j)s?(x)`) - const modulesThatUseGatsby = await getGatsbyDependents() - let files = [ path.join(this.base, `src`), path.join(this.base, `.cache`, `fragments`), ] .concat(this.additional.map(additional => path.join(additional, `src`))) - .concat(modulesThatUseGatsby.map(module => module.path)) .reduce( (merged, folderPath) => merged.concat(