UNPKG

3.1 kBMarkdownView Raw
1# :zap: Esbuild Runner (`esr`)
2
3Super-fast on-the-fly transpilation of modern JS, TypeScript and JSX using [esbuild](https://github.com/evanw/esbuild).
4
5**esr** 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.
6
7## ✨ Usage
8
9The easiest way to use **esbuild-runner** is to install it globally and use the included `esr` binary.
10
11```shell
12$ esr hello-world.ts
13```
14
15Alternatively, you can _require_ **esbuild-runner** within any nodejs process to include realtime transpilation:
16
17```shell
18$ node -r esbuild-runner/register hello-world.ts
19```
20
21In order to use **esbuild-runner** with Jest, you need to configure a [Jest transform](https://jestjs.io/docs/en/configuration.html#transform-objectstring-pathtotransformer--pathtotransformer-object) in your `jest.config.js`
22
23```js
24module.exports = {
25 transform: {
26 "\\.ts$": "esbuild-runner/jest",
27 },
28}
29```
30
31VSCode Debugging
32
33```JSON
34{
35 "version": "0.2.0",
36 "configurations": [
37 {
38 "name": "Debug with esbuild-runner",
39 "program": "${workspaceFolder}/hello-world.ts",
40 "runtimeArgs": [
41 "-r",
42 "esbuild-runner/register"
43 ],
44 "request": "launch",
45 "sourceMaps": true,
46 "skipFiles": [
47 "<node_internals>/**"
48 ],
49 "type": "pwa-node"
50 }
51 ]
52}
53```
54
55## ⚙️ Configuration
56
57`esr` provides two different ways to transpile your code:
58
59- **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.
60- **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.
61
62```shell
63$ bin/esr.js --help
64Usage: esr [options] <source-file> [file-options]
65
66 --cache Transform on a file per file basis and cache code
67 --clearCache Clear transform cache
68 --help|-h Display this help message
69
70```
71
72To customize the options passed to esbuild, you can create an `esbuild-runner.config.js` file in the current directory or one of the ancestor directories.
73
74```js
75// example esbuild-runner.config.js
76module.exports = {
77 type: "bundle", // bundle or transform (see description above)
78 esbuild: {
79 // Any esbuild build or transform options go here
80 target: "esnext",
81 },
82}
83```
84
85## 📦 Installation
86
87Simply install the **esbuild-runner** npm package using your favorite package manager.
88
89- globally ...
90
91```shell
92$ npm install -g esbuild-runner
93```
94
95- ... or locally in your project
96
97```shell
98$ npm add --dev esbuild-runner
99```
100
101## 👋 Contributing
102
103Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
104
105## ⚖ License
106
107[Apache 2.0](https://github.com/folke/esbuild-runner/blob/main/LICENSE)
108
109<!-- markdownlint-disable-file MD014 MD033 -->