Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): correctly inject static query in theme components #10786

Merged
merged 4 commits into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
jest.mock(`glob`, () => {
const sync = jest.fn().mockImplementation(() => [])
return {
sync,
}
})
const path = require(`path`)
const glob = require(`glob`)
const { resolveThemes, Runner } = require(`../query-compiler`)

const base = path.resolve(``)

describe(`Runner`, () => {
beforeEach(() => {
glob.sync.mockClear()
})

it(`returns a file parser instance`, async () => {
const runner = new Runner(base, [], {})

const parser = await runner.parseEverything()

expect(parser).toEqual(new Map())
})

describe(`expected directories`, () => {
it(`compiles src directory`, async () => {
const runner = new Runner(base, [], {})

await runner.parseEverything()

expect(glob.sync).toHaveBeenCalledWith(
expect.stringContaining(path.join(base, `src`)),
expect.any(Object)
)
})

it(`compiles fragments directory`, async () => {
const runner = new Runner(base, [], {})

await runner.parseEverything()

expect(glob.sync).toHaveBeenCalledWith(
expect.stringContaining(path.join(base, `src`)),
expect.any(Object)
)
})

it(`compiles themes directory(s)`, async () => {
const theme = `gatsby-theme-whatever`
const runner = new Runner(
base,
[path.join(base, `node_modules`, theme)],
{}
)

await runner.parseEverything()

expect(glob.sync).toHaveBeenCalledWith(
expect.stringContaining(path.join(base, `node_modules`, theme)),
expect.any(Object)
)
})
})
})

describe(`resolveThemes`, () => {
it(`returns empty array if zero themes detected`, () => {
;[
[],
[{ resolve: path.join(base, `gatsby-plugin-whatever`) }],
undefined,
].forEach(testRun => {
expect(resolveThemes(testRun)).toEqual([])
})
})

it(`returns plugins matching gatsby-theme prefix`, () => {
const theme = `gatsby-theme-example`
expect(
resolveThemes([
{
resolve: path.join(base, `gatsby-theme-example`),
},
])
).toEqual([expect.stringContaining(theme)])
})

it(`handles scoped packages`, () => {
const theme = `@dschau/gatsby-theme-example`

expect(
resolveThemes([
{
resolve: path.join(base, theme),
},
])
).toEqual([expect.stringContaining(theme.split(`/`).join(path.sep))])
})
})
51 changes: 32 additions & 19 deletions packages/gatsby/src/internal-plugins/query-runner/query-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,24 @@ const validationRules = [
let lastRunHadErrors = null
const overlayErrorID = `graphql-compiler`

const resolveThemes = (plugins = []) =>
plugins.reduce((merged, plugin) => {
if (plugin.resolve.includes(`gatsby-theme-`)) {
merged.push(plugin.resolve)
}
return merged
}, [])

class Runner {
baseDir: string
base: string
additional: string[]
schema: GraphQLSchema
errors: string[]
fragmentsDir: string

constructor(baseDir: string, fragmentsDir: string, schema: GraphQLSchema) {
this.baseDir = baseDir
this.fragmentsDir = fragmentsDir
constructor(base: string, additional: string[], schema: GraphQLSchema) {
this.base = base
this.additional = additional
this.schema = schema
}

Expand All @@ -91,14 +100,21 @@ class Runner {
}

async parseEverything() {
// FIXME: this should all use gatsby's configuration to determine parsable
// files (and how to parse them)
let files = glob.sync(`${this.fragmentsDir}/**/*.+(t|j)s?(x)`, {
nodir: true,
})
files = files.concat(
glob.sync(`${this.baseDir}/**/*.+(t|j)s?(x)`, { nodir: true })
)
const filesRegex = path.join(`/**`, `*.+(t|j)s?(x)`)
let files = [
path.join(this.base, `src`),
path.join(this.base, `.cache`, `fragments`),
]
.concat(this.additional.map(additional => path.join(additional, `src`)))
.reduce(
(merged, folderPath) =>
merged.concat(
glob.sync(path.join(folderPath, filesRegex), {
nodir: true,
})
),
[]
)
files = files.filter(d => !d.match(/\.d\.ts$/))
files = files.map(normalize)

Expand Down Expand Up @@ -217,16 +233,13 @@ class Runner {
return compiledNodes
}
}
export { Runner }
export { Runner, resolveThemes }

export default async function compile(): Promise<Map<string, RootQuery>> {
const { program, schema } = store.getState()
// TODO: swap plugins to themes
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @ChristopherBiscardi once #10787 lands I can swap to themes from the redux state.

I don't really consider it a blocker for this PR though. We can get this one merged and then tweak later, removing the gatsby-theme test which will be 👌

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm

const { program, schema, plugins } = store.getState()

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

const queries = await runner.compileAll()

Expand Down