line_push/node_modules/jiti/README.md
2022-07-17 13:16:16 +08:00

3.4 KiB

jiti

Runtime typescript and ESM support for Node.js (CommonJS)

version downloads size

Features

  • Stable typescript and esm syntax support
  • Provide sync interface to replace require
  • Super slim and zero dependency
  • Syntax detect to avoid extra transform
  • CommonJS cache integration
  • Filesystem transpile cache + V8 compile cache

Usage

const jiti = require('jiti')(__filename)

jiti('./path/to/file.ts')

You can also pass options as second argument:

const jiti = require('jiti')(__filename, { debug: true })

Options

debug

  • Type: Boolean
  • Default: false

Enable debug to see which files are transpiled

cache

  • Type: Boolean
  • Default: true

Use transpile cache

cacheDir

  • Type: String
  • Default: node_modules/.cache/jiti or {TMP_DIR}/node-jiti

Cache directroy (only effective if cache is true)

transform

  • Type: Function
  • Default: Babel (lazy loaded)

Transform function. See src/babel for more details

Compared to Alternatives

standard-things/esm

  • + Much more stable thanks to babel
  • + Less low level operations
  • + Typescript support
  • - Slower (without cache)

babel-register

  • + Smaller install size (~1M vs ~11M with same plugins)
  • + Configured out of the box
  • + Smart syntax detect to avoid unnecessary trnaspilation
  • + Does not ignores node_modules. ESM everywhere yay!
  • + Embeddable

esbuild

  • + No native dependency
  • + More stable thanks to babel
  • - Slower
  • + Embeddable

ts-node

  • + Support both esm and typescript
  • / No typechecking support / Faster
  • + Smart syntax detect to avoid unnecessary transpilation

Native ESM Support (MJS)

  • It is not (yet) landed as a stable feature
  • No typescript support
  • Limitted to .mjs files with different executation context (no __filename, require, etc)

Bundlers (rollup, webpack, snowpack, etc)

Meanwhile 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.

Note: However currently only babel transform is supported, configurable transform support is in the roadmap so using esbuild or other solutions would be possible.

Development

  • Clone Repo
  • Run yarn
  • Run yarn build
  • Run yarn dev
  • Run node ./test/jiti.js

Roadmap

  • Basic working
  • Syntax detect and fallback to CJS require
  • Improve project build system
  • File system cache
  • Configurable transform
  • Add tests
  • Support node -r jiti
  • esbuild support

License

MIT. Made with 💖