Yarn workspaces are coming

You’ve got to love it!

David
Sparkles Blog
2 min readMay 6, 2017

--

The yarn team just started a set of RFCs for workspaces (#3294)!

As a framework developer and library author, you’ve got to love that! T0day, my project teams prefer a solution where we put a single package.json in the root folder and one package.json in sub-directories per each “sub-module”.

Opened in an IDE, this looks like this:

In this approach, we can always cd packages/<pkg> && yarn run build to run a build for an individual module. We write build artefacts to a dist/<pkg> directory for each module.

Dependencies are shared between modules and managed in the root package.json. Because of that, yarn install must not be run outside the root directory!

In this folder structure, the shared dependencies are kept in node_modules folder of the root directory, so that each of the sub-modules links against the shared dependencies.

However, by accident, running a yarn install in a sub-module folder creates a nestednode_modules folder, thus causing havoc in the build. In special, the workspaces installs RFC (#60) is really going to help us here, solving the “child packages to install into a single node_modules in the root package” issue (#711).

I think such a layout is better than working with local file:// dependencies which slows down development and has other issues, such as #2165.

Usually, we put a (global) build script into the root folder, too! It simply iterates over each of the packages/* sub-directories and executes yarn run build in that folder. For common build tasks, helpers, and friends, we keep a tools directory at the root level. Modules can re-use a common build task or customize, if needed. It would be super cool to get a “run script for each sub-module”-feature from yarn, as well!

Personally, I am really looking forward to this RFC! I’d love yarn sub-modules to behave similar to how Gradle handles multi-projects! Since the RFC is still very very young, let’s see what happens!

--

--