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

Conversation

DSchau
Copy link
Contributor

@DSchau DSchau commented Jan 3, 2019

Description

This PR (correctly) replaces static query content in themes. It does this with a probably naive algorithm, where it checks the plugins array, and if a plugins path matches (gatsby-theme-), that plugin will be added to the query compiler list. It also slightly cleans up some of the code in the query compiler and adds a few tests

Notes:

  • I validated with this repo -> https://github.com/DSchau/gatsby-theme-static-query-repro
  • It may not be reasonable to impose gatsby-theme-* constraint for all themes, so another solution may be preferable
  • Can't really tie into webpack (e.g. to detect a custom loader targeting node_modules/whatever-custom-component) because we use webpack-merge to persist into redux state, which gets translated to an empty object; although I would like to find some solution to non-theme packages in node_modules, as well as themes not named gatsby-theme-*

Feedback welcome, cc @ChristopherBiscardi @jbolda

Related Issues

Fixes #10785

DSchau added 2 commits January 2, 2019 17:52
This PR (correctly) replaces static query content in themes. It does
this with a probably naive algorithm, where it checks the plugins array,
and if a plugins path matches (gatsby-theme-), that plugin will be added
to the query compiler list. It also slightly cleans up some of the code
in the query compiler and adds a few tests

Notes:
  - I validated with this repo -> https://github.com/DSchau/gatsby-theme-static-query-repro
  - Can't really tie into webpack (e.g. to detect a custom loader
targeting node_modules/whatever-custom-component) because we use
webpack-merge to persist into redux state, which gets translated to an
empty object; although I would like to find some solution to non-theme
packages in node_modules

Feedback welcome, cc @ChristopherBiscardi @jbolda
@DSchau DSchau requested a review from a team as a code owner January 3, 2019 00:02
const filesRegex = `/**/*.+(t|j)s?(x)`
let files = [`${this.base}/src`, `${this.base}/.cache/fragments`]
.concat(
this.additional.map(additional => path.join(this.base, additional))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This presumes all the same directory (e.g. node_modules/gatsby-theme, or plugins/gatsby-theme)... not sure if/how monorepos would work, so that's a wrinkle to consider.

Copy link
Contributor

Choose a reason for hiding this comment

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

Themes need an index.js because they function as plugins so I think we can use path.dirname(require.resolve(theme)) to get the correct base location

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like an easy fix--thanks!

@ChristopherBiscardi
Copy link
Contributor

ChristopherBiscardi commented Jan 3, 2019

Nice! Pretty exciting to see that this was actually the issue.

closes ChristopherBiscardi/gatsby-theme-examples#11

I'll mention that I'm doing some work in #10787 that would be useful for getting a list of themes in use from the redux store. From my understanding, that would help fix the gatsby-theme-* restriction

@DSchau
Copy link
Contributor Author

DSchau commented Jan 3, 2019

Need to fix the Windows pathing stuff (as usual...)--addressing that now.

Should be fixed in 3b65605


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


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

Choose a reason for hiding this comment

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

sgtm

@ChristopherBiscardi
Copy link
Contributor

Really excited to see this in. You rock @DSchau

I can refactor the themes call in #10787

@DSchau
Copy link
Contributor Author

DSchau commented Jan 4, 2019

@ChristopherBiscardi I'll merge this first thing tomorrow AM and get a build out with this tweak!

Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

Verified and works as expected. Will iterate on grabbing themes once PR from @ChristopherBiscardi will be merged, but this is good to go in meantime

@pieh pieh merged commit edff703 into gatsbyjs:master Jan 4, 2019
@DSchau DSchau deleted the gatsby/query-runner-theme-fix branch January 4, 2019 15:12
@jbolda
Copy link
Contributor

jbolda commented Jan 7, 2019

Cheers @DSchau, works a treat!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants