Using Yarn Workspaces to Create a Monorepo

Using yarn workspace to create a monorepo

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Build a Complete Company Design System course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Build a Complete Company Design System, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Build a Complete Company Design System
  • [00:00 - 00:07] So, it's time to get hands-on and start building their mono repo. To build their mono repo, we will work with yarn work spaces.

    [00:08 - 00:42] And this is a really convenient feature of yarn to work with node-based mono ripos, because it has some advantages, like delegating and deduplicating dependencies, running packet scripts directly from their repository root, and it also handles symbolic packet dependencies, which in our case is extremely useful, because for example, we will have react packets, depending on foundation and store a book, depending to react and foundation at the same time. Before jumping to the terminal, we need only two dependencies, node and yarn installed on your local machine.

    [00:43 - 00:57] If we have those installed, let's up to the terminal. We have an empty git project, and the only dependencies we need is node and yarn.

    [00:58 - 01:20] So we can take with node version we are using, so the version 16, and with yarn version, the 1.22. So in this course, we will use the yarn version 3, and to work with yarn version 3, it's really easy, we just run yarn set version and bury.

    [01:21 - 01:39] Burries basically the version 3 of yarn, and everything will be done. So if we go to our VS code, you will see that some files are also generated, like the yarn RC file, and we are ready to move on.

    [01:40 - 02:03] So next step is to create the workspace, and this is really easy to do with yarn, we just run yarn in need from initialize, and then the W flag or the workspace flag. So now we have the workspace, and we can see the structure.

    [02:04 - 02:16] We have read me, basically read me the packages that it will host our actual packages. And now let's go and create our first package, the foundation package.

    [02:17 - 02:30] To do this, we will go to package this in foundation. Oops, we don't have an actual foundation folder, so let's go to package this.

    [02:31 - 02:44] Create the foundation folder. Go to the foundation folder.

    [02:45 - 02:58] So now we are in the slas packages, slas foundation, and to create the package we just run yarn in need. So we initialize the foundation package.

    [02:59 - 03:11] And as we discussed, we will have react and storeable packages. So we make the same MKD react and MKD storebook.

    [03:12 - 03:25] And we navigate to react, do exactly the same yarn in need, and go to the store book package. But instead of running yarn in need, here we will run yarn in need.

    [03:26 - 03:34] And then the P flag, which means that these packets will be private. And this is because we will publish foundation and our design tokens.

    [03:35 - 03:45] We will publish react for our component library, but we will not publish as an NPM library or storebook. So we don't need this be public rather than private.

    [03:46 - 03:55] And we can see here that we have a private flag equals true when we use the P flag. So that's perfect.

    [03:56 - 04:13] And to move forward, we need to somehow make our packets is dependent on each other. So we see that the react packets will be depending on the foundation packets and the storebook packets will be depending on the react and foundation packets.

    [04:14 - 04:22] But there is something here that we need to take care of. So if we go to the storebook packets, let's say for example.

    [04:23 - 04:38] And we want to add the dependency to the react packets. If we just write dependency and react, how do we ensure that we mean the local react packets rather than the global react packets from the Facebook team?

    [04:39 - 04:57] So here comes enhance them the scope operator that we will use. So if we go to the react package of JSON, instead of having our packets named react, we will use new line does ds slas react.

    [04:58 - 05:16] And we will do the same for a foundation file and the same for the storebook file. So here if we go back and we write yarn workspace is least, we can see that we have three packages already installed.

    [05:17 - 05:26] And to under the dependency, it's extremely easy. We just run yarn and then workspace, we target the storebook workspace here.

    [05:27 - 05:37] And then we run the command add and we add two dependencies, the new line ds react and the new line foundation. So if we click run, everything is completed.

    [05:38 - 05:56] And you will see here that we don't target any specific version rather than the workspace version and this is basically, this basically means that this is a local dependency. And we will do the same for our react package.

    [05:57 - 06:06] We just added the foundation as a dependency and we can validate this here. We have the foundation as a local dependency.

    [06:07 - 06:17] And basically this is it. So just to recap, we started our project with yarn and set it the version to yardberry three.

    [06:18 - 06:22] And then we created all the packages and added the dependencies in the package. json.

    [06:23 - 06:27] And now let's move forward and add some TypeScript and easily the configurations.