Skip to content
Advertisement

gatsby-source-filesystem not sourcing files from site directory when used in a theme

I’m trying to build a gatsby theme that uses gatsby-source-filesystem to source images.

I have set up a yarn workspace for development, my folder structure looks like this.

JavaScript

The yarn workspace is also set up correctly and the gatsby-theme-example is a dependency of it. yarn workspaces info correctly shows it in workspaceDependencies.

Both workspaces have a gatsby-config.js file, the gatsby-theme-example has the gatsby-source-filesystem in it.

JavaScript

The problem I’m facing is that I have to put the images inside the theme directories image folder, otherwise they are not found by gatsby-source-filesystem. From following this video and reading the tutorial, using path.join and the __dirname variable should point to the package that has the theme installed as a dependency, in my case example.com

Strangely, the gatsby-plugin-page-creator plugin in gatsby-theme-example/gatsby-config.js creates the pages defined in example.com/src/pages (the site directory) perfectly.

JavaScript

I also have onPreBootstrap defined in gatsby-theme-example/gatsby-node.js like this

JavaScript

and it creates the images directory in gatsby-theme-example not example.com

I’m using gatsby@4.18.0 and gatsby-source-filesystem@4.18.0 and start the project using this command: yarn workspace example.com start

I’ve set up a repo, which you can use to reproduce the issue like this:

  1. git clone https://github.com/AlexanderProd/gatsby-source-filesystem-theme-bug
  2. yarn workspace example.com install
  3. yarn workspace example.com start
  4. go to http://localhost:8000/___graphql
  5. run the following query
JavaScript

Advertisement

Answer

Another solution and in this case the optimal one is to just use images as a path.

JavaScript

Based on this answer.

Advertisement