Gatsby: Having a `src` directory as the root of all source files

Created on 17 Apr 2017  ยท  2Comments  ยท  Source: gatsbyjs/gatsby

I consider the use of a src directory a pretty useful convention. Is there a particular reason you choose to not have that structure for Gatsby? It seems a bit clumsy to me having all these different source directories directly in the root of the project.

A src directory makes certain configurations easier to manage since sources are clearly separated from config files, static asset or data and node-modules and such. For example having webpack resolve all directories in src for easy import statements, or running some tool like prettier-eslint over all js files.

Right now I was trying to do just that, and prettier-eslint appears to have a bug where it's not respecting the .eslintignore contents correctly, munching away on my node-modules directory.

When using GraphQL to serve data from files which are fetched from an external API, I would expect those files to live in a location like data next to src, indicating that that might not be part of the repository.

Maybe you have your reasons. I'm curious to know.

Most helpful comment

So @fabien0102 has been working on this exact issue actually https://github.com/gatsbyjs/gatsby/pull/802 and we've been discussing this in Discord.

My main problem with his PR has been the idea of adding another configuration option as every bit of configuration has a very high cost โ€” sometimes costs you don't realize until down the road when a configuration option adds all sorts of complexity to adding a new feature or event prevents the new feature from being added altogether.

So no new configuration options unless they're super high value.

Which... setting a different root directory isn't.

But his and your's arguments are persuasive that it'd be a lot simpler if all site src code was gathered under one directory.

So here's a proposed directory structure:

โ”œโ”€โ”€ .cache // Stuff gets cached here. Shouldn't be under source control.
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ data // dump images, json, csv, yaml, etc. here.
โ”œโ”€โ”€ gatsby-config.js
โ”œโ”€โ”€ gatsby-node.js
โ”œโ”€โ”€ plugins // Additional site-specific plugins
โ”œโ”€โ”€ src // Everything related to the frontend goes here.
โ”‚ย ย  โ”œโ”€โ”€ components
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ header.js
|   โ”œโ”€โ”€ css
โ”‚ย ย        โ””โ”€โ”€ prism-coy.css
โ”‚ย ย  โ”œโ”€โ”€ html.js
โ”‚ย ย  โ”œโ”€โ”€ layouts
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ index.js
โ”‚ย ย  โ”œโ”€โ”€ pages
โ”‚ย ย  โ”œโ”€โ”€ templates
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ blog-post.js
โ”‚ย ย  โ””โ”€โ”€ utils
โ”‚ย ย      โ””โ”€โ”€ typography.js
โ”œโ”€โ”€ static // Everything in this directory gets copied directly into public
โ”œโ”€โ”€ public // Output directory

Thoughts?

All 2 comments

So @fabien0102 has been working on this exact issue actually https://github.com/gatsbyjs/gatsby/pull/802 and we've been discussing this in Discord.

My main problem with his PR has been the idea of adding another configuration option as every bit of configuration has a very high cost โ€” sometimes costs you don't realize until down the road when a configuration option adds all sorts of complexity to adding a new feature or event prevents the new feature from being added altogether.

So no new configuration options unless they're super high value.

Which... setting a different root directory isn't.

But his and your's arguments are persuasive that it'd be a lot simpler if all site src code was gathered under one directory.

So here's a proposed directory structure:

โ”œโ”€โ”€ .cache // Stuff gets cached here. Shouldn't be under source control.
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ data // dump images, json, csv, yaml, etc. here.
โ”œโ”€โ”€ gatsby-config.js
โ”œโ”€โ”€ gatsby-node.js
โ”œโ”€โ”€ plugins // Additional site-specific plugins
โ”œโ”€โ”€ src // Everything related to the frontend goes here.
โ”‚ย ย  โ”œโ”€โ”€ components
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ header.js
|   โ”œโ”€โ”€ css
โ”‚ย ย        โ””โ”€โ”€ prism-coy.css
โ”‚ย ย  โ”œโ”€โ”€ html.js
โ”‚ย ย  โ”œโ”€โ”€ layouts
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ index.js
โ”‚ย ย  โ”œโ”€โ”€ pages
โ”‚ย ย  โ”œโ”€โ”€ templates
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ blog-post.js
โ”‚ย ย  โ””โ”€โ”€ utils
โ”‚ย ย      โ””โ”€โ”€ typography.js
โ”œโ”€โ”€ static // Everything in this directory gets copied directly into public
โ”œโ”€โ”€ public // Output directory

Thoughts?

That is pretty much exactly the structure I would have proposed ๐Ÿ˜„ Glad you see value in this also.

Was this page helpful?
0 / 5 - 0 ratings