Member-only story

Distribution of Typescript shared code without package repositories

Carles Capellas
5 min readJun 7, 2024

--

A couple years ago I wrote a piece about sharing code in Typescript monorepos, focusing mainly on npm workspaces, and I left out an important aspect of the software development lifecycle: shipping the compiled files to production environments. Because that is such a fundamental part of software development, I’ll be addressing it in this second piece of writing.

Having successfully extracted the duplicate code into a separate directory/npm project (covered in sharing code in Typescript monorepos), the task at hand is to decide how the shared code and its external dependencies will make their way to production environments. Let’s consider two different solutions: one based on relative path imports and another based on npm dependencies.

Note that the distribution issue is mostly a server side concern since client apps are usually bundled into static assets that include both external dependencies and code imported from outside the project.

To make the explanations more tangible, let’s introduce a simple monorepo example. For the sake of simplicity the example code has no actual functionality, just one duplicate function (getId) that relies on one external dependency (nanoid). Here is the monorepo structure:

root
📂 client
| 📂 source
| | 📝 index.html
| | 📝…

--

--

Carles Capellas
Carles Capellas

Written by Carles Capellas

Always up for sport, somewhat obsessive about order and, over all things, enthusiastic about coding

No responses yet

Write a response