DEV Community

Folke Lemaitre
Folke Lemaitre

Posted on

πŸš€βš‘οΈ ts-node on steroids

I just released esbuild-runner. It makes it easy to run arbitrary code or tests without needing to build your whole project. It's a great way to improve your development workflow. Underneath it uses the fantastic and super fast javascript bundler esbuild

✨ Usage

The easiest way to use esbuild-runner is to install it globally and use the included esr binary.

$ esr hello-world.ts
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can require esbuild-runner within any nodejs process to include realtime transpilation:

$ node -r esbuild-runner/register hello-world.ts
Enter fullscreen mode Exit fullscreen mode

In order to use esbuild-runner with Jest, you need to configure a Jest transform in your jest.config.js

module.exports = {
  transform: {
    "\\.ts$": "esbuild-runner/jest",
  },
}
Enter fullscreen mode Exit fullscreen mode

βš™οΈ Configuration

esr provides two different ways to transpile your code:

  • bundling (default): this transpiles the script and all its dependencies in typically one invocation of esbuild. Dependencies defined in package.json or node_modules will never be transpiled. Running esr will always transpile the code. No caching is used.
  • transform (--cache): this method will invoke esbuild for every source file, but will cache the result. This means that the initial run will be slower, but after that, only changed source files will be transpiled.
$ bin/esr.js --help
Usage: esr [options] <source-file> [file-options]

  --cache       Transform on a file per file basis and cache code
  --clearCache  Clear transform cache
  --help|-h     Display this help message

Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Installation

Simply install the esbuild-runner npm package using your favorite package manager.

  • globally ...
$ npm install -g esbuild-runner
Enter fullscreen mode Exit fullscreen mode
  • ... or locally in your project
$ npm add --dev esbuild-runner
Enter fullscreen mode Exit fullscreen mode

πŸ‘‹ Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

βš– License

Apache 2.0

Top comments (0)