When injected dependencies are enabled, Workspace packages are copied, allowing them to run with different versions of the peer. As you can see in this example, the Button component uses React version 17, but the Card component uses React version 16. When we run tests for the Button component, we aim to use React version 17. In contrast, when we run the Button component from within the Card component, we want Button to use the same version of React as the Card. Therefore, React version 17 is installed in the node modules of Button. Meanwhile, Card doesn't reference the Button directly from the Workspace, but from a hidden subdirectory where it's aligned with React version 16. This setup means there are two instances of the Button component in the Workspace, one with React version 17 and another, a copy inside the node-modules.pnpm directory with React version 16. The only downside to this approach is that once you modify the Button component, you need to rerun the installation process so that pnpm can update the files with the Button component in the copied instances.
After discussing the challenges associated with monorepos, I'd like to introduce another tool that addresses these issues out of the box. The name of the tool is Bit. While Bit is not a package manager, managing packages is one of its primary responsibilities. So, what exactly is Bit? It's a toolchain designed for building composable software. You can conceptualize it as an alternative to Git, GitHub, the npm registry, and various npm clients. When using Bit, it serves as your version control system, manages your dependencies, and publishes your packages. For this presentation, we'll focus solely on package management, so I'll discuss only the installation aspect of Bit.
In many ways, a Bit workspace resembles a pnpm workspace. It's a collection of packages or components. However, there's a distinct difference, as there are no packages on files in a Bit workspace. Instead, all dependencies for all components are declared in a single manifest located at the workspace's root. Moreover, there's no separate field for dev dependencies. This streamlined approach is feasible because Bit conducts a code analysis of the components within the workspace. It automatically identifies which components utilize which dependencies and discerns whether a particular dependency is for production or development for a given component. Let me show you now a quick demo of a Bit workspace. For the demo, I will use VS Code with the Bit extension installed. As you can see, I already have it installed on my computer. Let's go to the Bit section and start a new workspace. Bit has created a workspace manifest for me. Let's now generate some new components using the Bit CLI tool. I will create two new node apps app1 and app2. Here are the directories of my newly created app components.
Comments