Skip to content

Commit

Permalink
[1.0] Waiting for query extraction wasn't actually waiting (#1303)
Browse files Browse the repository at this point in the history
* Waiting for query extraction wasn't actually waiting

* Restore snapshots for MarkdownRemark tests
  • Loading branch information
KyleAMathews committed Jun 30, 2017
1 parent bf4281f commit e940c9c
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 47 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"jest": "^20.0.4",
"lerna": "^2.0.0-rc.5",
"plop": "^1.7.4",
"prettier": "^1.5.2",
"prettier-eslint-cli": "^4.1.1",
"prop-types": "^15.5.8",
"purdy": "^2.2.1",
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sass/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports.modifyWebpackConfig = ({ config, stage }) => {
const cssModulesConf = `css?modules&minimize&importLoaders=1`
const cssModulesConfDev = `${cssModulesConf}&sourceMap&localIdentName=[name]---[local]---[hash:base64:5]`

console.log(`stage`, stage)
switch (stage) {
case `develop`: {
config.loader(`sass`, {
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby-remark-responsive-image/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ module.exports = (
const imageNodes = select(markdownAST, `image`)

// This will also allow the use of html image tags
const rawHtmlImageNodes = select(markdownAST, `html`).filter(node => node.value.startsWith(`<img`))
const rawHtmlImageNodes = select(markdownAST, `html`).filter(node =>
node.value.startsWith(`<img`)
)

for (let node of rawHtmlImageNodes) {
let formattedImgTag = Object.assign(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ Array [
},
"id": "whatever >>> MarkdownRemark",
"internal": Object {
"content": "---
title: \\"my little pony\\"
date: \\"12/12/2014\\"
---
Where oh where is my little pony?
",
"contentDigest": "54cfa2ad99756f2fa232cac585280a37",
"type": "MarkdownRemark",
},
Expand All @@ -34,6 +41,13 @@ Array [
},
"id": "whatever >>> MarkdownRemark",
"internal": Object {
"content": "---
title: \\"my little pony\\"
date: \\"12/12/2014\\"
---
Where oh where is my little pony?
",
"contentDigest": "54cfa2ad99756f2fa232cac585280a37",
"type": "MarkdownRemark",
},
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = async function onCreateNode({
children: [],
parent: node.id,
internal: {
content,
contentDigest,
type: `MarkdownRemark`,
},
Expand Down
11 changes: 9 additions & 2 deletions packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const crypto = require(`crypto`)
const apiRunnerNode = require(`../utils/api-runner-node`)
const { graphql } = require(`graphql`)
const { store, emitter } = require(`../redux`)
const { boundActionCreators } = require(`../redux/actions`)
const loadPlugins = require(`./load-plugins`)
const { initCache } = require(`../utils/cache`)

Expand Down Expand Up @@ -51,7 +50,15 @@ module.exports = async (program: any) => {
// $FlowFixMe
config = preferDefault(require(`${program.directory}/gatsby-config`))
} catch (e) {
if (!_.includes(e.toString(), `Error: Cannot find module`)) {
const firstLine = e.toString().split(`\n`)[0]
if (
!_.includes(
firstLine,
`Error: Cannot find module` && !_.includes(firstLine, `gatsby-config`)
)
) {
console.log(``)
console.log(``)
console.log(e)
process.exit(1)
}
Expand Down
90 changes: 54 additions & 36 deletions packages/gatsby/src/internal-plugins/query-runner/file-parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
const fs = require(`fs`)
const fs = require(`fs-extra`)
const crypto = require(`crypto`)

// Traverse is a es6 module...
Expand All @@ -13,19 +13,21 @@ const { getGraphQLTag } = require(`../../utils/babel-plugin-extract-graphql`)

import type { DocumentNode, DefinitionNode } from "graphql"

const readFileAsync = Bluebird.promisify(fs.readFile)

async function parseToAst(filePath, fileStr) {
let ast

let transpiled
// TODO figure out why awaiting apiRunnerNode doesn't work
// Currently if I try that it just returns immediately.
//
// Preprocess and attempt to parse source; return an AST if we can, log an
// error if we can't.
const transpiled = await apiRunnerNode(`preprocessSource`, {
filename: filePath,
contents: fileStr,
})
// const transpiled = await apiRunnerNode(`preprocessSource`, {
// filename: filePath,
// contents: fileStr,
// })

if (transpiled.length) {
if (transpiled && transpiled.length) {
for (const item of transpiled) {
try {
const tmp = babylon.parse(item, {
Expand Down Expand Up @@ -59,19 +61,20 @@ async function parseToAst(filePath, fileStr) {
}

async function findGraphQLTags(file, text): Promise<Array<DefinitionNode>> {
let ast = await parseToAst(file, text)
if (!ast) return []

let queries = []
traverse(ast, {
ExportNamedDeclaration(path, state) {
path.traverse({
TaggedTemplateExpression(innerPath) {
const gqlAst = getGraphQLTag(innerPath)
if (gqlAst) {
gqlAst.definitions.forEach(def => {
if (!def.name || !def.name.value) {
console.log(stripIndent`
return new Promise(resolve => {
parseToAst(file, text).then(ast => {
if (!ast) return []

let queries = []
traverse(ast, {
ExportNamedDeclaration(path, state) {
path.traverse({
TaggedTemplateExpression(innerPath) {
const gqlAst = getGraphQLTag(innerPath)
if (gqlAst) {
gqlAst.definitions.forEach(def => {
if (!def.name || !def.name.value) {
console.log(stripIndent`
GraphQL definitions must be "named".
The query with the missing name is in ${file}.
To fix the query, add "query MyQueryName" to the start of your query.
Expand All @@ -90,24 +93,27 @@ async function findGraphQLTags(file, text): Promise<Array<DefinitionNode>> {
}
}
`)
process.exit(1)
}
})
process.exit(1)
}
})

queries.push(...gqlAst.definitions)
}
queries.push(...gqlAst.definitions)
}
},
})
},
})
},
resolve(queries)
})
})
return queries
}

const cache = {}

export default class FileParser {
async parseFile(file: string): Promise<?DocumentNode> {
const text = await readFileAsync(file, `utf8`)
// TODO figure out why fs-extra isn't returning a promise
const text = fs.readFileSync(file, `utf8`)

if (text.indexOf(`graphql`) === -1) return null
const hash = crypto
Expand Down Expand Up @@ -135,12 +141,24 @@ export default class FileParser {

async parseFiles(files: Array<string>): Promise<Map<string, DocumentNode>> {
const documents = new Map()
for (let file of files) {
const doc = await this.parseFile(file)

if (doc) documents.set(file, doc)
}

return documents
return Promise.all(
files.map(
file =>
new Promise(resolve => {
this.parseFile(file)
.then(doc => {
if (doc) documents.set(file, doc)
resolve()
})
.catch(e => {
console.log(`parsing file failed`, file, e)
})
})
)
)
.then(() => documents)
.catch(e => {
console.log(`parsing files failed`, e)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class Runner {

async compileAll() {
let nodes = await this.parseEverything()
return this.write(nodes)
return await this.write(nodes)
}

async parseEverything() {
let files = await globp(`${this.baseDir}/**/*.+(t|j)s?(x)`)
let files = glob.sync(`${this.baseDir}/**/*.+(t|j)s?(x)`)
files = files.filter(d => !d.match(/\.d\.ts$/))
files = files.map(normalize)
// Ensure all page components added as they're not necessarily in the
Expand Down Expand Up @@ -118,7 +118,6 @@ class Runner {
} else if (groupIndex === 2) {
docName = match
}
// console.log(`Found match, group ${groupIndex}: ${match}`)
})
}
const docWithError = documents.find(doc =>
Expand Down Expand Up @@ -179,10 +178,12 @@ class Runner {
}
}

export default function compile(): Promise<Map<string, RootQuery>> {
export default async function compile(): Promise<Map<string, RootQuery>> {
const { program, schema } = store.getState()

const runner = new Runner(`${program.directory}/src`, schema)

return runner.compileAll()
const queries = await runner.compileAll()

return queries
}
2 changes: 2 additions & 0 deletions packages/gatsby/src/utils/webpack-modify-validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export default (async function ValidateWebpackConfig(config, stage) {
// We don't care about the return as plugins just mutate the config directly.
await apiRunnerNode(`modifyWebpackConfig`, { config, stage })

// console.log(JSON.stringify(config, null, 4))

invariant(
_.isObject(config) && _.isFunction(config.resolve),
`
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4464,9 +4464,9 @@ prettier-eslint@^6.0.0:
pretty-format "^20.0.3"
require-relative "^0.8.7"

prettier@^1.4.2:
version "1.5.0"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.5.0.tgz#f3476164f9a532a218a1337b1032638275d82614"
prettier@^1.4.2, prettier@^1.5.2:
version "1.5.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.5.2.tgz#7ea0751da27b93bfb6cecfcec509994f52d83bb3"

pretty-format@^20.0.3:
version "20.0.3"
Expand Down

0 comments on commit e940c9c

Please sign in to comment.