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