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 | - Seamless typescript and ESM syntax support
|
12 | - Seamless interoperability between ESM and CommonJS
|
13 | - Synchronous API to replace `require`
|
14 | - Super slim and zero dependency
|
15 | - Smart syntax detection to avoid extra transforms
|
16 | - CommonJS cache integration
|
17 | - Filesystem transpile hard cache
|
18 | - V8 compile cache
|
19 | - Custom resolve alias
|
20 |
|
21 | ## Usage
|
22 |
|
23 | ### Programmatic
|
24 |
|
25 | ```js
|
26 | const jiti = require('jiti')(__filename)
|
27 |
|
28 | jiti('./path/to/file.ts')
|
29 | ```
|
30 |
|
31 | You can also pass options as second argument:
|
32 |
|
33 | ```js
|
34 | const jiti = require('jiti')(__filename, { debug: true })
|
35 | ```
|
36 |
|
37 | ### CLI
|
38 |
|
39 | ```bash
|
40 | jiti index.ts
|
41 | # or npx jiti index.ts
|
42 | ```
|
43 |
|
44 | ### Register require hook
|
45 |
|
46 | ```bash
|
47 | node -r jiti/register index.ts
|
48 | ```
|
49 |
|
50 | Alternatively, you can register `jiti` as a require hook programmatically:
|
51 | ```js
|
52 | const jiti = require('jiti')()
|
53 | const unregister = jiti.register()
|
54 | ```
|
55 |
|
56 | ## Options
|
57 |
|
58 | ### `debug`
|
59 |
|
60 | - Type: Boolean
|
61 | - Default: `false`
|
62 | - Environment Variable: `JITI_DEBUG`
|
63 |
|
64 | Enable debug to see which files are transpiled
|
65 |
|
66 | ### `cache`
|
67 |
|
68 | - Type: Boolean | String
|
69 | - Default: `true`
|
70 | - Environment Variable: `JITI_CACHE`
|
71 |
|
72 | Use transpile cache
|
73 |
|
74 | If set to `true` will use `node_modules/.cache/jiti` (if exists) or `{TMP_DIR}/node-jiti`
|
75 |
|
76 | ### `esmResolve`
|
77 |
|
78 | - Type: Boolean | String
|
79 | - Default: `false`
|
80 | - Environment Variable: `JITI_ESM_RESOLVE`
|
81 |
|
82 | Using esm resolution algorithm to support `import` condition.
|
83 |
|
84 | ### `transform`
|
85 |
|
86 | - Type: Function
|
87 | - Default: Babel (lazy loaded)
|
88 |
|
89 | Transform function. See [src/babel](./src/babel.ts) for more details
|
90 |
|
91 | ### `sourceMaps`
|
92 |
|
93 | - Type: Boolean
|
94 | - Default `false`
|
95 | - Environment Variable: `JITI_SOURCE_MAPS`
|
96 |
|
97 | Add inline source map to transformed source for better debugging.
|
98 |
|
99 | ### `interopDefault`
|
100 |
|
101 | - Type: Boolean
|
102 | - Default: `false`
|
103 |
|
104 | Return the `.default` export of a module at the top-level.
|
105 |
|
106 | ### `alias`
|
107 |
|
108 | - Type: Object
|
109 | - Default: -
|
110 | - Environment Variable: `JITI_ALIAS`
|
111 |
|
112 | Custom alias map used to resolve ids.
|
113 |
|
114 | ### `nativeModules`
|
115 |
|
116 | - Type: Array
|
117 | - Default: ['typescript`]
|
118 | - Environment Variable: `JITI_NATIVE_MODULES`
|
119 |
|
120 | List of modules (within `node_modules`) to always use native require for them.
|
121 |
|
122 | ### `transformModules`
|
123 |
|
124 | - Type: Array
|
125 | - Default: []
|
126 | - Environment Variable: `JITI_TRANSFORM_MODULES`
|
127 |
|
128 | List of modules (within `node_modules`) to transform them regardless of syntax.
|
129 |
|
130 | ## Development
|
131 |
|
132 | - Clone Repo
|
133 | - Run `yarn`
|
134 | - Run `yarn build`
|
135 | - Run `yarn dev`
|
136 | - Run `yarn jiti ./test/path/to/file.ts`
|
137 |
|
138 | ## License
|
139 |
|
140 | MIT. Made with 💖
|
141 |
|
142 |
|
143 | [npm-v-src]: https://img.shields.io/npm/v/jiti?style=flat-square
|
144 | [npm-v-href]: https://npmjs.com/package/jiti
|
145 |
|
146 | [npm-d-src]: https://img.shields.io/npm/dm/jiti?style=flat-square
|
147 | [npm-d-href]: https://npmjs.com/package/jiti
|
148 |
|
149 | [github-actions-src]: https://img.shields.io/github/workflow/status/unjs/jiti/ci/master?style=flat-square
|
150 | [github-actions-href]: https://github.com/unjs/jiti/actions?query=workflow%3Aci
|
151 |
|
152 | [size-src]: https://packagephobia.now.sh/badge?p=jiti
|
153 | [size-href]: https://packagephobia.now.sh/result?p=jiti
|