Initialize a new Gatsby project with a gitignore in yarn workspaces

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

We initialize a new Gatsby project while covering a few of the tripping points such as ignoring .cache and public using a .gitignore file.

Chris Biscardi: [0:00] To get started writing code, we'll initialize a new Gatsby project in a Yarn Workspaces setup. If we initialize our project with yarn init -y and we look at the package.json, we can see that we have a basic package.json.

[0:13] We can use the code CLI to open a VS Code instance or open your preferred editor. Then open the package.json file. We'll set private to true, which is required for any Yarn Workspaces with package.json. We'll also use workspaces pointing to any npm package inside of the packages directory. We also need to create the packages directory.

[0:31] In the packages directory, we'll create a new directory called www. In www, we'll initialize a new npm package. We then need to add a couple dependencies. At minimum, we need gatsby, react and react-dom.

[0:50] You can see that our dependencies have been installed in the package.json. We'll set up a couple of scripts. The scripts we've set up are just aliases to the Gatsby build, develop is for development, build is for production and clean will remove any caches.

[1:05] We'll create an empty gatsby-config.js, [inaudible] an index.js file in src pages. From the index.js file, we'll export a React component. Now that we have this setup, we can go back to the root of our project and run yarn workspace www develop.

[1:34] ESLint has told us that we need React in scope when using JSX, which is true because JSX compiles to React.createElement() calls.

[1:45] If we visit our site in Chrome or any other browser, we can now see our <h1> tag. Having confirmed everything works, we'll add the packages directory, the package.json, and the yarn.lock. Note that this added .cache, which we don't want. The .cache folder is a folder that Gatsby creates that we don't want to check into our Git repo.

[2:04] If we run git reset packages/www/.cache, you'll see that .cache is no longer in our index, but it's still hanging around our project. To deal with this, we'll create a .gitignore file in the root of our project. We'll add .cache.

[2:20] Now, when we run git status, we no longer see .cache because it's being ignored. We'll add gitignore and note that we also have the public folder, which is another folder that Gatsby creates that we don't need checked in.

[2:33] Note that we now have far fewer files than we did before and only the ones that we want. We will git add any files that we are already tracking, which in this case is the gitignore file. You can see that the gitignore file had some modifications signified by the M and that it's added to the index signified by the A. We can now commit our first Gatsby site.

[3:02] Note that we're ahead by one commit from the remote, we'll push. Now that we've pushed, we can take advantage of hub again using git browse. Git browse sends us to our site where we can see that we've uploaded our packages.

Lucas Homer
Lucas Homer
~ 4 years ago

How come you don't have a www/yarn.lock?

Chris Biscardi
Chris Biscardiinstructor
~ 4 years ago

When using Yarn workspaces there is only one yarn.lock at the root of the repo rather than one for every package.

Benoit LEGER-DERVILLE
Benoit LEGER-DERVILLE
~ 4 years ago

Can you tell again the interest (or the main goal/purpose) of going through a yarn workspace?

Will Johnson
Will Johnson
~ 4 years ago

Hi Benoit, I asked this question on twitter

Here's the response I use yarn workspaces so that I can pull packages out and use them locally without publishing, or develop a set of packages I do publish, without changing the way in which I use them

Benoit LEGER-DERVILLE
Benoit LEGER-DERVILLE
~ 4 years ago

Hey Will,

Perfect, thanks a lot.

Merci Take care of yours Benoit

Luigi Claudio
Luigi Claudio
~ 3 years ago

Hey Chris, hi. Thank you so much for this course. I'm trying to take this course a step further and by following the todo app tutorial on the fauna docs, adding relationships between lists and todos. This can be achieved by using the @relation directive on the todo field in the TodoList type. I have a doubt now about how the resolver would look like in our GraphQL server? And couldn't find anything related to this online. Do you have any docs or examples to recommend in this regard? 🙏

Markdown supported.
Become a member to join the discussionEnroll Today