tsc-watch 技术文档

tsc-watch 技术文档

tsc-watch The TypeScript compiler with --watch and a new onSuccess argument tsc-watch 项目地址: https://gitcode.com/gh_mirrors/ts/tsc-watch

1. 安装指南

1.1 使用 npm 安装

要安装 tsc-watch,请在终端中运行以下命令:

npm install tsc-watch --save-dev

1.2 全局安装

如果你希望在命令行中全局使用 tsc-watch,可以运行以下命令:

npm install -g typescript tsc-watch

2. 项目使用说明

2.1 从命令行使用

2.1.1 监视项目(使用 tsconfig.json
tsc-watch --onSuccess "node ./dist/server.js"
2.1.2 编译失败时发出提示音
tsc-watch --onFailure "echo Beep! Compilation Failed"
2.1.3 监视单个文件
tsc-watch server.ts --outDir ./dist --onSuccess "node ./dist/server.js"
2.1.4 使用自定义编译器
tsc-watch --onSuccess "node ./dist/server.js" --compiler my-typescript/bin/tsc

2.2 从 npm 脚本使用

你可以在 package.json 中添加以下脚本:

"dev-server": "tsc-watch --noClear -p ./src/tsconfig.json --onSuccess \"node ./dist/server.js\""

2.3 从 JavaScript 使用

你可以通过 EventEmitter 监听编译事件。以下是一个示例:

// 使用 CommonJS:
const { TscWatchClient } = require('tsc-watch/client');
// 使用 ES6 import:
import { TscWatchClient } from 'tsc-watch/client';

const watch = new TscWatchClient();

watch.on('started', () => {
  console.log('Compilation started');
});

watch.on('first_success', () => {
  console.log('First success!');
});

watch.on('success', () => {
  // 你的代码在这里...
});

watch.on('compile_errors', () => {
  // 你的代码在这里...
});

watch.start('--project', '.');

try {
  // 做一些事情...
} catch (e) {
  watch.kill(); // 致命错误,终止编译器实例。
}

3. 项目 API 使用文档

3.1 事件监听

tsc-watch 提供了以下事件供你监听:

  • started - 编译开始时触发(初始或增量)。
  • first_success - 第一次成功编译时触发。
  • subsequent_success - 每次后续成功编译时触发。
  • compile_errors - 每次编译失败时触发。
  • file_emitted - 每次文件被转译时触发(如果使用了 --listEmittedFiles)。

3.2 启动和终止

  • watch.start() - 启动监视器。
  • watch.kill() - 终止监视器。

4. 项目安装方式

4.1 使用 npm 安装

npm install tsc-watch --save-dev

4.2 全局安装

npm install -g typescript tsc-watch

通过以上步骤,你可以轻松地安装和使用 tsc-watch,并在开发过程中享受 TypeScript 的便捷编译和监视功能。

tsc-watch The TypeScript compiler with --watch and a new onSuccess argument tsc-watch 项目地址: https://gitcode.com/gh_mirrors/ts/tsc-watch

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔡显鹰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值