All Projects → Quramy → Lerna Yarn Workspaces Example

Quramy / Lerna Yarn Workspaces Example

Licence: mit
How to build TypeScript mono-repo project with yarn and lerna

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Lerna Yarn Workspaces Example

monopack
A JavaScript bundler for node.js monorepo-codebased applications.
Stars: ✭ 52 (-93.39%)
Mutual labels:  yarn, lerna, monorepo
Flow Mono Cli
A command line interface that aims to solve a few issues while working with flow typed codebases in a mono-repo.
Stars: ✭ 84 (-89.33%)
Mutual labels:  lerna, yarn, monorepo
Eslint Plugin Monorepo
ESLint Plugin for monorepos
Stars: ✭ 56 (-92.88%)
Mutual labels:  lerna, yarn, monorepo
monoreact
📦 React workspaces implementation
Stars: ✭ 13 (-98.35%)
Mutual labels:  yarn, workspace, monorepo
nextjs-monorepo-example
Collection of monorepo tips & tricks
Stars: ✭ 874 (+11.05%)
Mutual labels:  yarn, workspace, monorepo
Yerna
A Lerna-like tool for managing Javascript monorepos using Yarn
Stars: ✭ 140 (-82.21%)
Mutual labels:  lerna, yarn, monorepo
Vitamin Web
Decathlon Design System libraries for web applications
Stars: ✭ 70 (-91.11%)
Mutual labels:  lerna, yarn, monorepo
manager
OVHcloud Control Panel
Stars: ✭ 153 (-80.56%)
Mutual labels:  yarn, workspace, lerna
monorepo-utils
A collection of utilities for monorepo/lerna. Tools for TypeScript project references etc..
Stars: ✭ 143 (-81.83%)
Mutual labels:  yarn, lerna, monorepo
introduction-nodejs
Introduction to NodeJS
Stars: ✭ 13 (-98.35%)
Mutual labels:  yarn, workspace, monorepo
yarn-workspaces-example
Sample monorepo project using new Yarn feature called Workspaces
Stars: ✭ 39 (-95.04%)
Mutual labels:  yarn, lerna, monorepo
Syncpack
Manage multiple package.json files, such as in Lerna Monorepos and Yarn/Pnpm Workspaces
Stars: ✭ 356 (-54.76%)
Mutual labels:  lerna, yarn, monorepo
React-Native-Web-TypeScript-Prettier-Boilerplate
A starterkit to work with nextjs, react-native, storybook… all with prettified typescript and in a monorepo
Stars: ✭ 16 (-97.97%)
Mutual labels:  yarn, monorepo
ng-mono-repo-starter
Angular Mono Repo Starter
Stars: ✭ 79 (-89.96%)
Mutual labels:  lerna, monorepo
pacote
A box of goodies, in TypeScript.
Stars: ✭ 14 (-98.22%)
Mutual labels:  lerna, monorepo
mock-spy-module-import
JavaScript import/require module testing do's and don'ts with Jest
Stars: ✭ 40 (-94.92%)
Mutual labels:  yarn, monorepo
monopacker
A tool for managing builds of monorepo frontend projects with eg. npm- or yarn workspaces, lerna or similar tools into a standalone application - no other tools needed.
Stars: ✭ 17 (-97.84%)
Mutual labels:  lerna, monorepo
Monorepo Starter
Monorepo starter project for Kotlin, Python, TypeScript x React
Stars: ✭ 255 (-67.6%)
Mutual labels:  lerna, yarn
Webclient
Monorepo hosting the proton web clients
Stars: ✭ 3,079 (+291.23%)
Mutual labels:  yarn, monorepo
lerna-terminal
Powerful cli ui for monorepos
Stars: ✭ 25 (-96.82%)
Mutual labels:  lerna, monorepo

How to build TypeScript mono-repo project

CircleCI

Tools

  • yarn: NPM client.
  • Lerna: Multiple packages management tool.
  • TypeScript: ^3.0.0 supports project references.

Directory Structure

Put each package under the packages directory.

.
├── README.md
├── lerna.json
├── package.json
├── packages
│   ├── x-cli
│   │   ├── lib
│   │   │   ├── main.d.ts
│   │   │   ├── main.js
│   │   │   └── main.js.map
│   │   ├── package.json
│   │   ├── src
│   │   │   └── main.ts
│   │   └── tsconfig.json
│   └── x-core
│       ├── lib
│       │   ├── index.d.ts
│       │   ├── index.js
│       │   └── index.js.map
│       ├── package.json
│       ├── src
│       │   └── index.ts
│       └── tsconfig.json
├── tsconfig.json
└── yarn.lock

Workspaces

Using yarn workspace feature, configure the following files:

  • /package.json

Append the workspaces key.

{
  "private": true,
  "workspaces": [
    "packages/*"
  ]
}
  • lerna.json

Set npmClient "yarn" and turn useWorkspaces on.

{
  "lerna": "2.2.0",
  "packages": [
    "packages/*"
  ],
  "npmClient": "yarn",
  "useWorkspaces": true,
  "version": "1.0.0"
}

Exec yarn install(or lerna bootstrap). After successful running, all dependency packages are downloaded under the repository root node_modules directory.

Dependencies between packages

In this example, the x-cli package depends on another package, x-core. So to execute (or test) x-cli, x-core packages should be installed. But in development the x-core package is not published so you can't install it.

yarn solves this problem. This command creates sim-links of each package into the top-level node_modules dir.

Resolve Dependencies as TypeScript Modules

As mentioned above, Lerna resolves dependencies between packages. It's enough for "runtime". However considering TypeScript sources, in other words "static", it's not.

For example, the following code depends a module x-core located at other package.

/* packages/x-cli/src/main.ts */
import { awesomeFn } from "@quramy/x-core";

export function cli() {
  awesomeFn();
  return Promise.resolve(true);
}

If you compile this code, TypeScript compiler emits a "Cannot find module" error until building x-core package and creating x-core/index.d.ts. And it's silly to compile dependent packages(e.g. x-core) in the same repository after each editing them.

TypeScript's path mapping is the best solution. Path mappings are declared such as:

/* tsconfig.json */
{
  "compilerOptions": {
    /* other options */
    "baseUrl": "./packages",
    "paths": {
      "@quramy/*": ["./*/src"]
    }
  }
}

The above setting means import { awesomeFn } from "@quramy/x-core" is mapped to import { awesomeFn } from "../../x-core/src"(it's relative from "packages/x-cli/src/main.ts"). In other words, path mapping allows to treat developing packages' sources as published(compiled) modules.

References between packages

TypeScript 3.0 supports Project reference feature. You can tell tsc that x-cli depends on x-core project using this.

/* packages/x-cli/tsconfig.json */
{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "lib"
  },
  "references": [
    { "path": "../x-core" }
  ]
}

In the above json, the key references means the dependency.

Compile all packages

Our repository has multiple tsconfig.json files. We can compile all packages with "-b" option:

$ tsc -b packages/x-core packages/x-cli

License

The MIT License (MIT)

Copyright 2017 Quramy

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].