@adonisjs/require-ts

中文文档
2.0.13发布于 2022/09/21MITTypeScriptCommonJS近 30 天下载 4.3万

In memory typescript compiler

原始使用文档
文档目录9 节
  1. Introduction
  2. Goals
  3. Usage
  4. Programmatic usage
  5. register
  6. getWatcherHelpers
  7. Caching
  8. Differences from ts-node
  9. Transformers


Typescript Compiler

In memory Typescript compiler for Node.js with support for caching and custom transformers


gh-workflow-image npm-image license-image synk-image

Introduction

Require ts is a module similar to ts-node with a handful of differences.

The idea is to hook into the lifecycle of Node.js require calls and compile Typescript on the fly (in memory)

In case, if you are not aware, Node.js has first class support for registering custom require extensions to resolve and compile files with a certain extension. For example:

require.extenstions['.ts'] = function (module, filename) {
  var content = fs.readFileSync(filename, 'utf8')
  module._compile(content, filename)
}

If we replace the function body of the example with the Typescript compiler API, the we basically get in-memory typescript compilation. However, there are many other things to manage.

  • Making source-maps to work, so that the error points to the Typescript code and not the compiled in memory Javascript.
  • Support for typescript extensions
  • Introducing some sort of caching to avoid re-compiling the unchanged files. Typescript compiler is not one of the fastest compilers, so caching is required.

Goals

Following are the goals for writing this module

  • Able to work with Typescript without setting up a on-disk compiler
  • Keeping the in-memory compilation fast. For this, we do not perform type checking. Your IDE or text editor should do it.
  • Cache the compiled output on disk so that we can avoid re-compiling the unchanged files. A decent project has 100s of source files and we usually don't change all of them together. Also compiled cache is not same as the compiled output.
  • Expose helper functions for watchers to clear the cache. Most of the Node.js apps use some kind of a watcher to watch for file changes and then restart the process. The helpers exposed by this package, allows the watcher to cleanup cache of the changed file.
  • Add support for custom transformers.

Usage

This module is pre-configured with all the AdonisJS applications and ideally you won't have to dig into the setup process yourself. However, if you are using it outside of AdonisJS, then follow the following setup process.

npm i -D @adonisjs/require-ts

And then require it as a Node.js require hook

node -r @adonisjs/require-ts/build/register app.ts

I have personally created a bash alias for the above command.

alias tsnode="node -r @adonisjs/require-ts/build/register"

and then run it as follows

tsnode app.ts

Programmatic usage

The main goal of this package is to expose a programmatic API that others can use to create their own build tools or commands.

register

const { register } = require('@adonisjs/require-ts')

/**
 * Require ts will resolve the "tsconfig.json" file from this
 * path. tsconfig.json file is required to compile the code as * per the project requirements
 */
const appRoot = __dirname

const options = {
  cache: true,
  cachePath: join(require.resolve('node_modules'), '.cache/your-app-name'),
  transformers: {
    before: [],
    after: [],
    afterDeclarations: [],
  },
}

register(appRoot, options)

/**
 * From here on you can import the typescript code
 */
require('./typescript-app-entrypoint.ts')

The register method accepts an optional object for configuring the cache and executing transformers.

  • cache: Whether or not to configure the cache
  • cachePath: Where to write the cached output
  • transformers: An object with transformers to be executed at different lifecycles. Read transformers section.

The register method adds two global properties to the Node.js global namespace.

  • compiler: Reference to the compiler, that is compiling the source code. You can access it as follows:
    const { symbols } = require('@adonisjs/require-ts')
    console.log(global[symbols.compiler])
    
  • config: Reference to the config parser, that parses the tsconfig.json file. You can access it as follows:
    const { symbols } = require('@adonisjs/require-ts')
    console.log(global[symbols.config])
    

getWatcherHelpers

The watcher helpers allows the watchers to cleanup the cache at different events. Here's how you can use it

const { getWatcherHelpers } = require('@adonisjs/require-ts')

/**
 * Require ts will resolve the "tsconfig.json" file from this
 * path. tsconfig.json file is required to compile the code as * per the project requirements
 */
const appRoot = __dirname

/**
 * Same as what you passed to the `register` method
 */
const cachePath = join(require.resolve('node_modules'), '.cache/your-app-name')

const helpers = getWatcherHelpers(appRoot, cachePath)

helpers.clear('./relative/path/from/app/root')

This is how you should set up the flow

  • Clean the entire cache when you start the watcher for the first time. helpers.clear(). No arguments means, clear everything

  • Clean the cache for the file that just changed. helpers.clear('./file/path')

  • Check if the config file has changed in a way that will impact the compiled output. If yes, then clear all the cached files.

    if (helpers.isConfigStale()) {
      helpers.clear() // clear all files from cache
    }
    

Caching

Caching is really important for us. Reading the compiled output from the disk is way faster than re-compiling the same file with Typescript.

This is how we perform caching.

  • Create a md5 hash of the file contents using the rev-hash package.
  • Checking the cache output with the same name as the hash.
  • If the file exists, pass its output to Node.js module._compile method.
  • Otherwise, compile the file using the Typescript compiler API and cache it on the disk

The module itself doesn't bother itself with clearing the stale cached files. Meaning, the cache grows like grass.

However, we expose helper functions to cleanup the cache. Usually, you will be using them with a file watcher like nodemon to clear the cache for the changed file.

Differences from ts-node

ts-node and require-ts has a few but important differences.

  • ts-node also type checks the Typescript code. They do allow configuring ts-node without type checking. But overall, they pay extra setup cost just by even considering type checking.
  • ts-node has no concept of on-disk caching. This is a deal breaker for us. Then why not contribute this feature to ts-node?. Well, we can. But in order for caching to work properly, the module need to expose the helpers for watchers to cleanup the cache and I don't think, ts-node will bother itself with this.
  • ts-node ships with inbuilt REPL. We don't want to bother ourselves with this. Again, keeping the codebase focused on a single use case. You can use @adonisjs/repl for the REPL support.

These are small differences, but has biggest impact overall.

Transformers

Typescript compiler API supports transformers to transform/mutate the AST during the compile phase. Here you can learn about transformers in general.

With require-ts, you can register the transformers with in the tsconfig.json file or pass them inline, when using the programmatic API.

Following is an example of the tsconfig.json file

{
  "compilerOptions": {},
  "transformers": {
    "before": ["./transformer-before"],
    "after": ["./transformer-after"],
    "afterDeclarations": ["./transformer-after-declarations"]
  }
}

The transformer array accepts the relative file name from the appRoot. The transformer module must export a function as follows:

export default transformerBefore(ts: typescript, appRoot: string) {
  return function transformerFactory (context) {}
}

版本与发布记录

@adonisjs/require-ts 共发布 21 个版本,首次发布于 2020/08/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.13latest2022/09/214 年前
2.0.122022/05/224 年前
2.0.112022/04/044 年前
2.0.102022/02/224 年前
2.0.92022/01/174 年前
2.0.82021/08/295 年前

关于 @adonisjs/require-ts 的常见问题

@adonisjs/require-ts 是什么?

In memory typescript compiler

如何安装 @adonisjs/require-ts?

在项目目录运行 npm install @adonisjs/require-ts,或使用 pnpm add / yarn add / bun add。

@adonisjs/require-ts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adonisjs/require-ts 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@adonisjs/require-ts 可以商用吗?

@adonisjs/require-ts 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@adonisjs/require-ts 的下载量有多少?

根据 npm 官方下载统计,@adonisjs/require-ts 在 2026/10/05 – 2026/11/03 这 30 天内被下载 42,703 次,日均约 1,423 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adonisjs/require-ts 还在维护吗?

最新版本 2.0.13 发布于 2022/09/21(4 年前);至今共发布 21 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/08/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。