UNPKG

3.52 kBMarkdownView Raw
1# jiti
2
3> Runtime typescript and ESM support for Node.js (CommonJS)
4
5[![version][npm-v-src]][npm-v-href]
6[![downloads][npm-d-src]][npm-d-href]
7[![size][size-src]][size-href]
8
9## Features
10
11- Stable typescript and esm syntax support
12- Provide sync interface to replace require
13- Super slim and zero dependency
14- Syntax detect to avoid extra transform
15- CommonJS cache integration
16- Filesystem transpile cache + V8 compile cache
17
18## Usage
19
20```js
21const jiti = require('jiti')(__filename)
22
23jiti('./path/to/file.ts')
24```
25
26You can also pass options as second argument:
27
28```js
29const jiti = require('jiti')(__filename, { debug: true })
30```
31
32## Options
33
34### `debug`
35
36- Type: Boolean
37- Default: `false`
38
39Enable debug to see which files are transpiled
40
41### `cache`
42
43- Type: Boolean
44- Default: `true`
45
46Use transpile cache
47
48### `cacheDir`
49
50- Type: String
51- Default: `node_modules/.cache/jiti` or `{TMP_DIR}/node-jiti`
52
53
54Cache directroy (only effective if `cache` is `true`)
55
56### `transform`
57
58- Type: Function
59- Default: Babel (lazy loaded)
60
61Transform function. See [src/babel](./src/babel.ts) for more details
62
63## Compared to Alternatives
64
65### [`standard-things/esm`](https://github.com/standard-things/esm)
66
67- `+` Much more stable thanks to babel
68- `+` Less low level operations
69- `+` Typescript support
70- `-` Slower (without cache)
71
72### [`babel-register`](https://babeljs.io/docs/en/babel-register)
73
74- `+` Smaller install size (~1M vs ~11M with same plugins)
75- `+` Configured out of the box
76- `+` Smart syntax detect to avoid unnecessary trnaspilation
77- `+` Does not ignores `node_modules`. ESM everywhere yay!
78- `+` Embeddable
79
80### [`esbuild`](https://github.com/evanw/esbuild)
81
82- `+` No native dependency
83- `+` More stable thanks to babel
84- `-` Slower
85- `+` Embeddable
86
87### `ts-node`
88
89- `+` Support both esm and typescript
90- `/` No typechecking support / Faster
91- `+` Smart syntax detect to avoid unnecessary transpilation
92
93### Native ESM Support (MJS)
94
95- It is not (yet) landed as a stable feature
96- No typescript support
97- Limitted to `.mjs` files with different executation context (no `__filename`, `require`, etc)
98
99### Bundlers (`rollup`, `webpack`, `snowpack`, etc)
100
101Meanwhile it would be much better making an optimized bundle to deploy to production or as npm package, using bundler setup and watching is frustrating during project development that's where `jiti` (or similar tools like `ts-node`) would be more convenient.
102
103**Note:** However currently only babel transform is supported, configurable transform support is in the roadmap so using `esbuild` or other solutions would be possible.
104
105## Development
106
107- Clone Repo
108- Run `yarn`
109- Run `yarn build`
110- Run `yarn dev`
111- Run `node ./test/jiti.js`
112
113## Roadmap
114
115- [x] Basic working
116- [x] Syntax detect and fallback to CJS require
117- [x] Improve project build system
118- [x] File system cache
119- [x] Configurable transform
120- [ ] Add tests
121- [ ] Support `node -r jiti`
122- [ ] esbuild support
123
124## License
125
126MIT. Made with 💖
127
128<!-- Refs -->
129[npm-v-src]: https://img.shields.io/npm/v/jiti?style=flat-square
130[npm-v-href]: https://npmjs.com/package/jiti
131
132[npm-d-src]: https://img.shields.io/npm/dm/jiti?style=flat-square
133[npm-d-href]: https://npmjs.com/package/jiti
134
135[github-actions-src]: https://img.shields.io/github/workflow/status/nuxt-contrib/jiti/ci/master?style=flat-square
136[github-actions-href]: https://github.com/nuxt-contrib/jiti/actions?query=workflow%3Aci
137
138[size-src]: https://packagephobia.now.sh/badge?p=jiti
139[size-href]: https://packagephobia.now.sh/result?p=jiti