首页 > 其他分享 >[Typescript] TypeScript Project References

[Typescript] TypeScript Project References

时间:2024-09-04 14:35:48浏览次数:5  
标签:files project TypeScript client Project json Typescript server tsconfig

Consider this package.json file:

{
  "name": "exercise",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "dev": "run-p dev:*",
    "dev:client": "tsc --project ./src/client/tsconfig.json --watch",
    "dev:server": "tsc --project ./src/server/tsconfig.json --watch"
  }
}

There are scripts for running the client and server TypeScript configurations, and a dev script runs both of them in parallel.

This is a lot of plumbing to get TypeScript to work. We would need to do the same for build scripts and any other scripts that involve TypeScript.

A better way to organize this is by using TypeScript project references.

 

Solution 1 (code)

In this solution, we have a new tsconfig.json file that has references to src/client/tsconfig.json and src/server/tsconfig.json. Unfortunately, there's no way to use wildcards in the references, so you have to manually list each tsconfig.json:

{
  "references": [
    {
      "path": "./src/client/tsconfig.json"
    },
    {
      "path": "./src/server/tsconfig.json"
    }
  ],
  "files": []
}

Note that there is also an empty files array. This is because the tsconfig.json file in the root directory is not responsible for type checking any files. Instead, it serves as a reference tsconfig.json to run the client and server tsconfig.json files in a specific order.

Inside of the package.json file, the dev script uses the -b flag. This tells TypeScript to run in build mode, which means it runs all the project references as well. This allows us to type check the entire repo with a single command:

// package.json
{
  "name": "exercise",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "dev": "tsc -b --watch"
  }
}

Inside of the tsconfig.base.json file, we've added the composite: true property. This enables constraints, allowing a TypeScript project to be used with project references. In other words, it tells TypeScript that this is a child project and needs to be run with project references.

// tsconfig.base.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "esModuleInterop": true,
    "noEmit": true,
    "strict": true,
    "skipLibCheck": true,
    "isolatedModules": true,
    "composite": true
  },
}

When TypeScript runs in build mode, it emits tsconfig.tsbuildinfo files, which are large and should not be committed to your repository. These files cache the results of type checking, which makes subsequent checks much faster.

To ignore these files, you can add *.tsbuildinfo to your root .gitignore file.

This first solution is an example of how you can organize your TypeScript configurations using project references.

However, there are several tsconfig files present in different directories.

 

Solution 2 (Code)

In this solution, we move all the tsconfig files to the root directory. This makes it clear that the tsconfig.json file belongs to the entire project, not just a specific package:

tsconfig.json
tsconfig.base.json
tsconfig.client.json
tsconfig.server.json

Inside of the tsconfig.client.json file, we can use the extends keyword to inherit the compiler options from tsconfig.base.json. Then, we can specify the include paths for the client packages:

// tsconfig.client.json

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "lib": [
      "ES2022",
      "DOM",
      "DOM.Iterable"
    ]
  },
  "include": [
    "src/client"
  ]
}

The tsconfig.server.json configuration is similar, but won't include the DOM-related libraries:

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "lib": [
      "ES2022"
    ]
  },
  "include": [
    "src/server"
  ]
}

Both of the client and server tsconfig files are referenced by the tsconfig.json file in the root directory:

{
  "references": [
    {
      "path": "./tsconfig.client.json"
    },
    {
      "path": "./tsconfig.server.json"
    }
  ],
  "files": []
}

This solution also works, but it can feel like a bit much to have four tsconfig files in the root directory.

 

Solution 3 (Code)

In this solution, we create a .config folder in the project root.

Inside of the .config directory we'll add the tsconfig.base.json file along with the tsconfig.client.json and tsconfig.server.json files.

Then inside of the root directory of the project, the tsconfig.json file will reference the other configurations.

This solution is becoming a more common pattern for storing configuration files.

 

Wrapping Up

Project references are a great feature for configuration. They allow for efficient organization and speed up type checking by caching the results. Whether you keep your configurations in the root directory, each package, or a separate configuration folder, choose what makes the most sense for your project's structure and complexity.

标签:files,project,TypeScript,client,Project,json,Typescript,server,tsconfig
From: https://www.cnblogs.com/Answer1215/p/18396433

相关文章

  • 前端Pinia教程,Pinia+Vue3+Vite+TypeScript+腾讯IM聊天解决方案项目实战
    前端Pinia教程:‌Pinia+Vue3+Vite+TypeScript+腾讯IM聊天解决方案项目实战在前端开发中,‌随着Vue3的普及和Vite构建工具的兴起,‌结合TypeScript和Pinia进行状态管理成为了一个高效且受欢迎的选择。‌本文将详细介绍如何结合这些技术栈以及腾讯IM聊天解决方案,‌搭建一个高效的前端......
  • 前端项目实战Uniapp移动端项目+Vue3+Typescript+AntdVue管理平台
    ‌前端项目实战:‌构建Uniapp移动端项目与Vue3+Typescript+AntdVue管理平台‌在当今的前端开发领域,‌技术的不断迭代和创新为开发者带来了更多的选择和可能性。‌本文将介绍如何使用Uniapp框架开发移动端项目,‌并结合Vue3、‌Typescript以及AntdVue来构建一个高效的管理平台。......
  • [Typescript] Build mode of tsc: tsc -b
    Along-awaitedfeatureissmartincrementalbuildsforTypeScriptprojects.In3.0youcanusethe --build flagwith tsc.Thisiseffectivelyanewentrypointfor tsc thatbehavesmorelikeabuildorchestratorthanasimplecompiler.Running tsc--bui......
  • 最新最详细的Project软件百度云下载 附图文教程
    总的来说,MicrosoftProject是一款专业的项目管理软件,它的核心作用在于协助团队高效创建和管理项目计划。目前常用的版本有project2010、project2016、project2019及project2021。通过精准的控制和分析,Project帮助项目经理监控项目进度、成本,预测可能的风险,并通过甘特图的......
  • 使用LangChain加载Project Gutenberg电子书:实用指南
    使用LangChain加载ProjectGutenberg电子书:实用指南引言ProjectGutenberg是一个提供免费电子书的在线图书馆,拥有超过60,000本电子书。对于自然语言处理(NLP)和文本分析项目来说,这是一个宝贵的资源。本文将介绍如何使用LangChain的GutenbergLoader来加载ProjectGutenberg的......
  • 前端Vue3项目VUE3+TypeScript企业级前端Vue项目
    前端Vue3项目VUE3+TypeScript企业级前端Vue项目‌Vue3+SpringBoot前端项目实战:‌智慧实验室管理平台‌在当今数字化快速发展的时代,‌智慧实验室管理平台的建设成为了提升科研效率与管理水平的关键一环。‌本文将通过一个实战案例,‌详细介绍如何使用Vue3和SpringBoot技术栈构建......
  • Project 1: Specification for Automail
    Project1:SpecificationforAutomailBackground:AutomailDeliveringSolutionsInc.(DS)hasrecentlydevelopedandprovidedaRoboticMailDeliverysystemcalledAutomailtothemarket.Automailisanautomatedmailsortinganddeliverysystemdesigned......
  • TypeScript+Vue3+组合式API编码前端开发
    1.Vue3简介·2020年9月18日,Vue.js发布版3.0版本,代号:0nePiece (海贼王)·经历了:4800+次提交、40+个RFC、600+次PR、300+贡献者·官方发版地址:Releasev3.0.0OnePiece·vuejs/core 1.1.【性能的提升】打包大小减少41%。初次渲染快55%,更新渲染快133%。内存减......
  • 云计算41——部署project_exam_system项目(续)
    #创建脚本,可以在java环境中运行任何的jar包或者war包 #!/bin/bash /usr/local/jdk/bin/java-jar/java/src/*.?ar一、思路分析(1)nginx1、下载镜像,将本地的dist项目的目录挂载在容器的/usr/share/nginx/html/2、启动容器3、该项目是一个前后端分离的项目,并非所有的......
  • Vue3+TypeScript+Vite+Pinia+ElementPlus开发项目在线医疗服务平台
     前言随着vue3.0的越来越受欢迎,开始有许多公司和个人开始学习并使用vue3开发项目。我从接触学习并使用vue2,到现在的vue3,工作中也一直在使用vue。vue3也发布很长时间了,目前vue3+vite+ts再结合一些优秀的UI框架,如Elementplus,Antdesign,NaiveUI,移动端的VantUI,成为了较为流......