1 | # jiti
|
2 |
|
3 | > Runtime typescript and ESM support for Node.js
|
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 |
|
52 | ```js
|
53 | const jiti = require("jiti")();
|
54 | const unregister = jiti.register();
|
55 | ```
|
56 |
|
57 | ## Options
|
58 |
|
59 | ### `debug`
|
60 |
|
61 | - Type: Boolean
|
62 | - Default: `false`
|
63 | - Environment Variable: `JITI_DEBUG`
|
64 |
|
65 | Enable debug to see which files are transpiled
|
66 |
|
67 | ### `cache`
|
68 |
|
69 | - Type: Boolean | String
|
70 | - Default: `true`
|
71 | - Environment Variable: `JITI_CACHE`
|
72 |
|
73 | Use transpile cache
|
74 |
|
75 | If set to `true` will use `node_modules/.cache/jiti` (if exists) or `{TMP_DIR}/node-jiti`
|
76 |
|
77 | ### `esmResolve`
|
78 |
|
79 | - Type: Boolean | String
|
80 | - Default: `false`
|
81 | - Environment Variable: `JITI_ESM_RESOLVE`
|
82 |
|
83 | Using esm resolution algorithm to support `import` condition.
|
84 |
|
85 | ### `transform`
|
86 |
|
87 | - Type: Function
|
88 | - Default: Babel (lazy loaded)
|
89 |
|
90 | Transform function. See [src/babel](./src/babel.ts) for more details
|
91 |
|
92 | ### `sourceMaps`
|
93 |
|
94 | - Type: Boolean
|
95 | - Default `false`
|
96 | - Environment Variable: `JITI_SOURCE_MAPS`
|
97 |
|
98 | Add inline source map to transformed source for better debugging.
|
99 |
|
100 | ### `interopDefault`
|
101 |
|
102 | - Type: Boolean
|
103 | - Default: `false`
|
104 |
|
105 | Return the `.default` export of a module at the top-level.
|
106 |
|
107 | ### `alias`
|
108 |
|
109 | - Type: Object
|
110 | - Default: -
|
111 | - Environment Variable: `JITI_ALIAS`
|
112 |
|
113 | Custom alias map used to resolve ids.
|
114 |
|
115 | ### `nativeModules`
|
116 |
|
117 | - Type: Array
|
118 | - Default: ['typescript`]
|
119 | - Environment Variable: `JITI_NATIVE_MODULES`
|
120 |
|
121 | List of modules (within `node_modules`) to always use native require for them.
|
122 |
|
123 | ### `transformModules`
|
124 |
|
125 | - Type: Array
|
126 | - Default: []
|
127 | - Environment Variable: `JITI_TRANSFORM_MODULES`
|
128 |
|
129 | List of modules (within `node_modules`) to transform them regardless of syntax.
|
130 |
|
131 | ## Development
|
132 |
|
133 | - Clone this repository
|
134 | - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
|
135 | - Install dependencies using `pnpm install`
|
136 | - Run `pnpm dev`
|
137 | - Run `pnpm jiti ./test/path/to/file.ts`
|
138 |
|
139 | ## License
|
140 |
|
141 | MIT. Made with 💖
|
142 |
|
143 |
|
144 |
|
145 | [npm-v-src]: https://img.shields.io/npm/v/jiti?style=flat-square
|
146 | [npm-v-href]: https://npmjs.com/package/jiti
|
147 | [npm-d-src]: https://img.shields.io/npm/dm/jiti?style=flat-square
|
148 | [npm-d-href]: https://npmjs.com/package/jiti
|
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 | [size-src]: https://packagephobia.now.sh/badge?p=jiti
|
152 | [size-href]: https://packagephobia.now.sh/result?p=jiti
|