Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

TypeScript Monorepo Configuration

TypeScript Monorepo Configuration

Monorepo is a development model that places multiple projects in the same code repository.

TypeScript supports Monorepo through project references and toolchains, enabling efficient management of multiple packages.


Monorepo project structure Root directory package.json | tsconfig.json | lerna.json | turbo.json The packages/ directory utils ui-components hooks app Management tools: npm workspaces | yarn workspaces | pnpm | lerna | turbo

Why Monorepo is needed

When a project contains multiple packages (such as utility libraries, component libraries, and applications), the traditional approach requires maintaining multiple code repositories.

Monorepo places all packages in the same repository, making code sharing more convenient and version management more unified.

TypeScript's project reference feature makes type checking and building Monorepo projects more efficient.

Concept:Monorepo (single repository) places multiple related projects in the same code repository, facilitating code sharing and coordinated development.


pnpm Workspace

pnpm is a modern package manager with native support for Workspace features.

Root directory package.json

{

   

"name"

:

"my-monorepo"

,

   

"version"

:

"1.0.0"

,

   

"private"

:

true

,

   

// Enable pnpm workspace

   

"packages"

:

[

       

"packages/*"

   

]

,

   

// Development dependencies

   

"devDependencies"

:

{

       

"typescript"

:

"^5.0.0"

   

}

,

   

// Scripts

   

"scripts"

:

{

       

"build"

:

"pnpm -r run build"

,

       

"clean"

:

"pnpm -r run clean"

,

       

"type-check"

:

"pnpm -r run type-check"

   

}

}

pnpm:pnpm's Workspace feature automatically links packages in the packages directory together.


Project Structure

Create the directory structure of a Monorepo project.

Directory structure

my

monorepo

/

├── packages

/

│   ├── utils

/

             # Utility package

│   │   ├── src

/

│   │   │   └── index.

ts

│   │   ├── package.

json

│   │   └── tsconfig.

json

│   │

│   ├── ui

components

/

     # UI component package

│   │   ├── src

/

│   │   │   ├── Button.

tsx

│   │   │   └── index.

ts

│   │   ├── package.

json

│   │   └── tsconfig.

json

│   │

│   └── app

/

               # Application

│       ├── src

/

│       │   └── index.

tsx

│       ├── package.

json

│       └── tsconfig.

json

│

├── package.

json

           # Root directory configuration

├── tsconfig.

base

.

json

    # Base configuration

└── pnpm

workspace.

yaml

   # pnpm configuration

packages:All packages are placed under the packages directory, and each package has its own package.json and tsconfig.json.


Base TypeScript configuration

Create a base configuration for all packages to use.

tsconfig.base.json

{

   

// Compiler options

   

"compilerOptions"

:

{

       

// Target version

       

"target"

:

"ES2020"

,

       

// Module system

       

"module"

:

"ESNext"

,

       

// Strict mode

       

"strict"

:

true

,

       

// Skip library type checking

       

"skipLibCheck"

:

true

,

       

// Enable ES module interop

       

"esModuleInterop"

:

true

,

       

// Enforce consistent file name casing

       

"forceConsistentCasingInFileNames"

:

true

,

       

// Module resolution

       

"moduleResolution"

:

"bundler"

,

       

// Resolve JSON modules

       

"resolveJsonModule"

:

true

,

       

// Isolated modules

       

"isolatedModules"

:

true

,

       

// Do not emit output files

       

"noEmit"

:

true

   

}

}

Inherited:Each package's tsconfig.json extends the base configuration, overriding only the options that need customization.


Utility package configuration

Configure TypeScript for the utility package.

packages/utils/tsconfig.json

{

   

// Extend the base configuration

   

"extends"

:

"../../tsconfig.base.json"

,

   

// Compiler options

   

"compilerOptions"

:

{

       

// Output directory

       

"outDir"

:

"./dist"

,

       

// Declaration file directory

       

"declarationDir"

:

"./dist/types"

,

       

// Generate declaration files

       

"declaration"

:

true

,

       

// Generate declarations for the entry file

       

"declarationMap"

:

true

,

       

// Module export style

       

"module"

:

"ESNext"

,

       

// Enable project references

       

"composite"

:

true

   

}

,

   

// Included files

   

"include"

:

[

"src/**/*"

]

,

   

// Excluded files

   

"exclude"

:

[

"node_modules"

,

"dist"

,

"**/*.test.ts"

]

}

composite:After enabling project references, TypeScript can compile this package incrementally.


Application configuration

Configure TypeScript for the main application and reference other packages.

packages/app/tsconfig.json

{

   

// Extend the base configuration

   

"extends"

:

"../../tsconfig.base.json"

,

   

// Compiler options

   

"compilerOptions"

:

{

       

// Output directory

       

"outDir"

:

"./dist"

,

       

// JSX configuration

       

"jsx"

:

"react-jsx"

,

       

// Path aliases

       

"baseUrl"

:

"."

,

       

"paths"

:

{

           

"@my-utils/*"

:

[

"../utils/src/*"

]

,

           

"@my-ui/*"

:

[

"../ui-components/src/*"

]

       

}

   

}

,

   

// Project references

   

"references"

:

[

       

{

"path"

:

"../utils"

}

,

       

{

"path"

:

"../ui-components"

}

   

]

,

   

// Included files

   

"include"

:

[

"src/**/*"

]

,

   

// Excluded files

   

"exclude"

:

[

"node_modules"

,

"dist"

]

}

Path aliases:Path aliases can be configured to directly reference other packages in the same repository.


Dependencies between packages

Declare dependencies on packages in the same repository in package.json.

packages/app/package.json

{

   

"name"

:

"@my-org/app"

,

   

"version"

:

"1.0.0"

,

   

"private"

:

true

,

   

"dependencies"

:

{

       

// Reference a package in the same repository

       

"@my-org/utils"

:

"workspace:*"

,

       

"@my-org/ui-components"

:

"workspace:*"

,

       

// External dependencies

       

"react"

:

"^18.2.0"

,

       

"react-dom"

:

"^18.2.0"

   

}

,

   

"devDependencies"

:

{

       

// Development dependencies

       

"@types/react"

:

"^18.2.0"

,

       

"typescript"

:

"^5.0.0"

   

}

}

workspace:Use workspace:* to point to other packages in the same repository, and pnpm will resolve them automatically.


Build scripts

Create unified build scripts for the Monorepo.

Root directory package.json

{

   

"scripts"

:

{

       

// Build all packages

       

"build"

:

"pnpm -r run build"

,

       

// Clean all build outputs

       

"clean"

:

"pnpm -r run clean"

,

       

// Type checking

       

"type-check"

:

"pnpm -r run type-check"

,

       

// Test all packages

       

"test"

:

"pnpm -r run test"

,

       

// Incremental build

       

"build:watch"

:

"pnpm -r --parallel run build:watch"

,

       

// Launch the app

       

"dev"

:

"pnpm --filter @my-org/app run dev"

   

}

}

pnpm -r:Recursively execute scripts with the same name in all packages.


Notes

Best practices:Monorepo is suitable for managing multiple related projects and can significantly improve code reuse and development efficiency.


Summary

Monorepo is the recommended model for modern front-end project management.

Note:When a project contains multiple related packages, prioritize the Monorepo approach.