Skip to content

Latest commit

 

History

History

typescript-monorepo

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Typescript Monorepo

In this example we are in a typescript monorepo using yarn workspaces.

Workspaces

In package.json our workspaces are defined as the following:

{
	// ...
	"private": "true",
	"workspaces": ["packages/*", "apps/*"]
	// ...
}

This will allow yarn to handle all our packages in packages and apps from the root of our project with the yarn install command.

For more info about yarn workspaces check the documentation here.

Structure

Our monorepo is a basic example. We just have the @example/tsconfig package on which all our typescript packages will depend.

It just contains a basic tsconfig.json where all our shared typescript options are defined.

Templays

Our templay in templays folder will help us setting up new packages in our monorepo with a single command.

This is our module templay in .templays.yml:

templays:
  module: ./templays/module

Notice: All files in a templay folder require the .tp extension

We want our modules to have a src directory with a simple index.ts file, a tsconfig.json which extends from our @example/tsconfig package, and a package.json file.

The package.json.tp file has a templay variable defined as the following:

{
	"name": "@example/{{.module}}"
	// ...
}

In this way we will be able to specify our new package's name by passing a templay variable named module

So we just defined a templay called module located in templays/module.

If we now run the list command

yarn templay list

we will get the following output:

Name       Path
module     ./templays/module

This will indicate that the templay is set correctly.

Now we are ready to generate!

Generate new packages

To generate our new templayed packages we just need to run the following command:

yarn templay gen -d destination/path -v module="module name" module

In this example we generated apps/generated1 and packages/generated2 packages by running:

yarn templay gen -d apps/generated1 -v module=generated1 module

and

yarn templay gen -d packages/generated2 -v module=generated2 module

getting the follwing outputs:

Templay "module" successfully generated in "apps/generated1"

and

Templay "module" successfully generated in "packages/generated2"