TypeScript文件更改时如何监视和重新加载ts节点 [英] How to watch and reload ts-node when TypeScript files change

查看:528
本文介绍了TypeScript文件更改时如何监视和重新加载ts节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TypeScript和Angular应用程序运行开发服务器,而无需每次都编译ts文件.我发现我可以使用ts-node进行运行,但我也想监视.ts文件并重新加载应用程序/服务器,就像对gulp watch这样的操作一样.

I'm trying to run a dev server with TypeScript and an Angular application without transpiling ts files every time. I found that I can do the running with ts-node but I want also to watch .ts files and reload the app/server as I would do with something like gulp watch.

推荐答案

在开发环境中,我一直在苦苦挣扎,直到我注意到nodemon的API允许我们更改其默认行为以便执行自定义命令.例如:

I was struggling with the same thing for my development environment until I noticed that nodemon's API allows us to change its default behaviour in order to execute a custom command. For example:

nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' src/index.ts

甚至更好:将nodemon的配置外部化为具有以下内容的nodemon.json文件,然后按照Sandokan的建议运行nodemon:

Or even better: externalize nodemon's config to a nodemon.json file with the following content, and then just run nodemon, as Sandokan suggested:

{ "watch": ["src/**/*.ts"], "ignore": ["src/**/*.spec.ts"], "exec": "ts-node ./index.ts" }

借助此操作,您可以实时重新加载ts-node进程,而不必担心基础实现.

By virtue of doing this, you'll be able to live-reload a ts-node process without having to worry about the underlying implementation.

干杯!

已更新为最新版本的nodemon:

创建具有以下内容的nodemon.json文件.

Create a nodemon.json file with the following content.

{
  "watch": ["src"],
  "ext": "ts",
  "ignore": ["src/**/*.spec.ts"],
  "exec": "ts-node ./src/index.ts"      // or "npx ts-node src/index.ts"
}

这篇关于TypeScript文件更改时如何监视和重新加载ts节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆