Giter VIP home page Giter VIP logo

npm-ts-workspaces-example's Introduction

How to build TypeScript mono-repo project

github actions

This repository explains how to create monorepos project using npm and TypeScript.

ToC

Tools

  • npm cli(v7 or later)
  • TypeScript

Directory Structure

Put each package under the packages directory.

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

Workspaces

Using npm workspaces feature, configure the following files:

Open package.json and append the workspaces key.

/* package.json */

{
  "name": "npm-ts-workspaces-example",
  "private": true,
  ...
  "workspaces": ["packages/*"]
}

Exec npm install. After successful running, all dependencies included from each package are downloaded under the repository root node_modules directory.

Dependencies across 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.

For example, packages/x-cli/src/main.spec.ts is a test code for main.ts, which depends on packages/x-core/src/index.ts .

/* packages/x-cli/src/main.ts.*/

import { awesomeFn } from "@quramy/x-core";

export async function main() {
  // dependencies across child packages
  const out = await awesomeFn();
  return out;
}

So we need to link x-core package from x-cli to execute the x-cli 's test.

Workspaces feature of npm also solves this problem. npm i creates sim-links of each package into the top-level node_modules dir.

Resolve dependencies as TypeScript projects

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

We need to tell "x-cli package depends on x-core" to TypeScript compiler. TypeScript provides much useful feature to do this, "Project References".

First, you add composite: true to project-root tsconfig.json to use project references feature.

/* tsconfig.json */

{
  "compilerOptions": {
    ...
    "composite": true
  }
}

Second, configure each package's tsconfig and configure dependencies across packages.

/* packages/x-cli/tsconfig.json */

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

And create a project which depends on all packages:

/* tsconfig.build.json */

{
  "files": [],
  "references": [{ "path": "packages/x-core" }, { "path": "packages/x-cli" }]
}

Let's exec npx tsc --build tsconfig.build.json. The .ts files included in all packages are build at once!

How to execute scripts for each package ?

We can use --workspaces option.

# Excecute npm test in all workspaces
$ npm test --workspaces
# Bump up packages in all workspaces
$ npm version --workspaces patch

# Publish packages in all workspaces
$ npm publish --workspaces

License

The MIT License (MIT)

npm-ts-workspaces-example's People

Contributors

andreavaccari avatar jajargaming avatar quramy avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

npm-ts-workspaces-example's Issues

How to use `npm i` to add a local dependency for a package?

For example, if we are in packages/x-cli/, and wants to add a dependecy(which is in another workspace, such as @quramy/x-core) for it, which npm command should we use? Using npm i @quramy/x-core will get a "not found" error, since this packages haven't been publish yet.

Thank you!

This repo finally helped me fix my new multi-project repo. This is the only example repo I could find after hours of searching google. Starred the repo for the future.

That's it, no issue here 👍

Proper repo naming

Just stumbled over this repo, thanks for sharing! Not an issues but a question: How do you deal with repo names in a monorepo like this if you do not want ever to publish it to npm?

In pnpm I just add "my-local-repo-name": "workspace: *". With npm 7 (and also with yarn) I need to specify a repo name which isn't on npm published, so npm i doesn't pull a remote repo which might exist. So, I can make up some name, prefix it with some fictional org and all good. But if someone registers that name I am back to zero. This sounds all not really sound or I do miss something. How do you deal with this?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/build.yml
  • actions/checkout v4@1d96c772d19495a3b5c517cd2bc0cb401ea0529f
  • actions/setup-node v4
nodenv
.node-version
  • node 20
npm
package.json
  • prettier 3.2.5
  • rimraf 5.0.5
  • typescript 5.4.3
packages/x-cli/package.json
  • @types/node ^20.0.0
  • minimist ^1.2.5
  • typescript 5.4.3
packages/x-core/package.json
  • @types/node ^20.0.0
  • typescript 5.4.3

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.