If you’re developing a very simple Drupal site or you’re maintaining a single Drupal module or theme that requires building front-end assets, you can get away with just plonking a package.json
in its directory and opening a terminal there. However, if your project begins to span multiple modules and themes, it quickly becomes impractical to build each one without hard-coding a bunch of stuff in a custom script so you can invoke it from the root of a Drupal project. There has to be a better way, right?
Yarn workspaces and the yarn.BUILD plug-in completely solve this.
The demo
Clone and set up I’ve created. You’ll notice that it’s extremely bare bones, with the demo modules and theme not containing any code - this is intentional to keep it as simple as possible.
After you look through each module and the theme, you’ll also notice they use different packages and build tools - each workspace defines its own build script and has full control over what it does, despite all workspaces being part of the same work tree and sharing one Yarn install via the top-level package.
Let’s get building
Now that you’ve familiarised yourself with the structure, it’s time to build things. In your terminal at the repository root, type ddev yarn build
, hit Enter, and you should see something similar to this:
Simple enough. It built every workspace.
Now we can get to the really clever stuff: subsequent builds. Type ddev yarn build
again and hit Enter. You should see this:
Wait, why didn’t it build anything? That’s because none of the workspaces had any changes. Let’s make a change. Open web/modules/custom/demo_module2/stylesheets/demo.scss
in your editor and save it without any other edits. Now run ddev yarn build
again. You should see the following:
yarn.BUILD not only saw that the workspace had been modified, but it also built the other workspaces that depend on it directly or indirectly.
Now try saving each of the following like above, one at a time, and run ddev yarn build
after each:
web/modules/custom/demo_module1/stylesheets/demo.less
web/modules/custom/demo_module3/stylesheets/demo.styl
web/themes/custom/demo_theme/stylesheets/demo.scss
Note how it only builds what’s changed or what’s had a dependency change. Feel free to experiment with different combinations of the source stylesheets to get a good sense how it does things.
How does this work?
You need the following key parts: