update
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 UnJS
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+48
-59
@@ -8,15 +8,19 @@
|
||||
|
||||
## Features
|
||||
|
||||
- Stable typescript and esm syntax support
|
||||
- Provide sync interface to replace require
|
||||
- Seamless typescript and ESM syntax support
|
||||
- Seamless interoperability between ESM and CommonJS
|
||||
- Synchronous API to replace `require`
|
||||
- Super slim and zero dependency
|
||||
- Syntax detect to avoid extra transform
|
||||
- Smart syntax detection to avoid extra transforms
|
||||
- CommonJS cache integration
|
||||
- Filesystem transpile cache + V8 compile cache
|
||||
- Filesystem transpile hard cache
|
||||
- V8 compile cache
|
||||
|
||||
## Usage
|
||||
|
||||
### Programmatic
|
||||
|
||||
```js
|
||||
const jiti = require('jiti')(__filename)
|
||||
|
||||
@@ -29,29 +33,52 @@ You can also pass options as second argument:
|
||||
const jiti = require('jiti')(__filename, { debug: true })
|
||||
```
|
||||
|
||||
### CLI
|
||||
|
||||
```bash
|
||||
jiti index.ts
|
||||
# or npx jiti index.ts
|
||||
```
|
||||
|
||||
### Register require hook
|
||||
|
||||
```bash
|
||||
node -r jiti/register index.ts
|
||||
```
|
||||
|
||||
Alternatively, you can register `jiti` as a require hook programmatically:
|
||||
```js
|
||||
const jiti = require('jiti')()
|
||||
const unregister = jiti.register()
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
### `debug`
|
||||
|
||||
- Type: Boolean
|
||||
- Default: `false`
|
||||
- Environment Variable: `JITI_DEBUG`
|
||||
|
||||
Enable debug to see which files are transpiled
|
||||
|
||||
### `cache`
|
||||
|
||||
- Type: Boolean
|
||||
- Type: Boolean | String
|
||||
- Default: `true`
|
||||
- Environment Variable: `JITI_CACHE`
|
||||
|
||||
Use transpile cache
|
||||
|
||||
### `cacheDir`
|
||||
If set to `true` will use `node_modules/.cache/jiti` (if exists) or `{TMP_DIR}/node-jiti`
|
||||
|
||||
- Type: String
|
||||
- Default: `node_modules/.cache/jiti` or `{TMP_DIR}/node-jiti`
|
||||
### `esmResolve`
|
||||
|
||||
- Type: Boolean | String
|
||||
- Default: `false`
|
||||
- Environment Variable: `JITI_ESM_RESOLVE`
|
||||
|
||||
Cache directroy (only effective if `cache` is `true`)
|
||||
Using esm resolution algorithm to support `import` condition.
|
||||
|
||||
### `transform`
|
||||
|
||||
@@ -60,47 +87,20 @@ Cache directroy (only effective if `cache` is `true`)
|
||||
|
||||
Transform function. See [src/babel](./src/babel.ts) for more details
|
||||
|
||||
## Compared to Alternatives
|
||||
### `sourceMaps`
|
||||
|
||||
### [`standard-things/esm`](https://github.com/standard-things/esm)
|
||||
- Type: Boolean
|
||||
- Default `false`
|
||||
- Environment Variable: `JITI_SOURCE_MAPS`
|
||||
|
||||
- `+` Much more stable thanks to babel
|
||||
- `+` Less low level operations
|
||||
- `+` Typescript support
|
||||
- `-` Slower (without cache)
|
||||
Add inline source map to transformed source for better debugging.
|
||||
|
||||
### [`babel-register`](https://babeljs.io/docs/en/babel-register)
|
||||
### `interopDefault`
|
||||
|
||||
- `+` 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
|
||||
- Type: Boolean
|
||||
- Default: `false`
|
||||
|
||||
### [`esbuild`](https://github.com/evanw/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.
|
||||
Return the `.default` export of a module at the top-level.
|
||||
|
||||
## Development
|
||||
|
||||
@@ -108,18 +108,7 @@ Meanwhile it would be much better making an optimized bundle to deploy to produc
|
||||
- Run `yarn`
|
||||
- Run `yarn build`
|
||||
- Run `yarn dev`
|
||||
- Run `node ./test/jiti.js`
|
||||
|
||||
## Roadmap
|
||||
|
||||
- [x] Basic working
|
||||
- [x] Syntax detect and fallback to CJS require
|
||||
- [x] Improve project build system
|
||||
- [x] File system cache
|
||||
- [x] Configurable transform
|
||||
- [ ] Add tests
|
||||
- [ ] Support `node -r jiti`
|
||||
- [ ] esbuild support
|
||||
- Run `yarn jiti ./test/path/to/file.ts`
|
||||
|
||||
## License
|
||||
|
||||
@@ -132,8 +121,8 @@ MIT. Made with 💖
|
||||
[npm-d-src]: https://img.shields.io/npm/dm/jiti?style=flat-square
|
||||
[npm-d-href]: https://npmjs.com/package/jiti
|
||||
|
||||
[github-actions-src]: https://img.shields.io/github/workflow/status/nuxt-contrib/jiti/ci/master?style=flat-square
|
||||
[github-actions-href]: https://github.com/nuxt-contrib/jiti/actions?query=workflow%3Aci
|
||||
[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/jiti/ci/master?style=flat-square
|
||||
[github-actions-href]: https://github.com/unjs/jiti/actions?query=workflow%3Aci
|
||||
|
||||
[size-src]: https://packagephobia.now.sh/badge?p=jiti
|
||||
[size-href]: https://packagephobia.now.sh/result?p=jiti
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { resolve } = require('path')
|
||||
|
||||
const script = process.argv.splice(2, 1)[0]
|
||||
|
||||
if (!script) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('Usage: jiti <path> [...arguments]')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const pwd = process.cwd()
|
||||
const jiti = require('..')(pwd)
|
||||
const resolved = process.argv[1] = jiti.resolve(resolve(pwd, script))
|
||||
jiti(resolved)
|
||||
+2
-2
@@ -1,2 +1,2 @@
|
||||
import { TransformOptions } from './types';
|
||||
export default function transform(opts: TransformOptions): string;
|
||||
import { TransformOptions, TRANSFORM_RESULT } from './types';
|
||||
export default function transform(opts: TransformOptions): TRANSFORM_RESULT;
|
||||
|
||||
+344
-453
File diff suppressed because one or more lines are too long
+8
-9
@@ -1,10 +1,9 @@
|
||||
/// <reference types="node" />
|
||||
import 'v8-compile-cache';
|
||||
import { TransformOptions } from './types';
|
||||
export declare type JITIOptions = {
|
||||
transform?: (opts: TransformOptions) => string;
|
||||
debug?: boolean;
|
||||
cache?: boolean;
|
||||
cacheDir?: string;
|
||||
};
|
||||
export default function createJITI(_filename?: string, opts?: JITIOptions): NodeRequire;
|
||||
import { TransformOptions, JITIOptions } from './types';
|
||||
declare type Require = typeof require;
|
||||
export interface JITI extends Require {
|
||||
transform: (opts: TransformOptions) => string;
|
||||
register: () => (() => void);
|
||||
}
|
||||
export default function createJITI(_filename: string, opts?: JITIOptions, parentModule?: typeof module): JITI;
|
||||
export {};
|
||||
|
||||
+1
-2
File diff suppressed because one or more lines are too long
+4
@@ -0,0 +1,4 @@
|
||||
import type { PluginObj } from '@babel/core';
|
||||
export declare function TransformImportMetaPlugin(_ctx: any, opts: {
|
||||
filename?: string;
|
||||
}): PluginObj<import("@babel/core").PluginPass>;
|
||||
+23
-1
@@ -1,5 +1,27 @@
|
||||
export declare type TransformOptions = {
|
||||
source: string;
|
||||
filename?: string;
|
||||
ts?: Boolean;
|
||||
ts?: boolean;
|
||||
retainLines?: boolean;
|
||||
legacy?: boolean;
|
||||
[key: string]: any;
|
||||
};
|
||||
export declare type TRANSFORM_RESULT = {
|
||||
code: string;
|
||||
error?: any;
|
||||
};
|
||||
export declare type JITIOptions = {
|
||||
transform?: (opts: TransformOptions) => TRANSFORM_RESULT;
|
||||
debug?: boolean;
|
||||
cache?: boolean | string;
|
||||
sourceMaps?: boolean;
|
||||
requireCache?: boolean;
|
||||
v8cache?: boolean;
|
||||
interopDefault?: boolean;
|
||||
esmResolve?: boolean;
|
||||
cacheVersion?: string;
|
||||
onError?: (error: Error) => void;
|
||||
legacy?: boolean;
|
||||
extensions?: string[];
|
||||
transformOptions?: Omit<TransformOptions, 'source'>;
|
||||
};
|
||||
|
||||
+6
-1
@@ -1,2 +1,7 @@
|
||||
import type { PackageJson } from 'pkg-types';
|
||||
export declare function isDir(filename: string): boolean;
|
||||
export declare function interopDefault(ex: any): any;
|
||||
export declare function isWritable(filename: string): boolean;
|
||||
export declare function md5(content: string, len?: number): string;
|
||||
export declare function detectLegacySyntax(code: string): RegExpMatchArray | null;
|
||||
export declare function isObject(val: any): boolean;
|
||||
export declare function readNearestPackageJSON(path: string): PackageJson | undefined;
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
function onError (err) {
|
||||
throw err /* ↓ Check stack trace ↓ */
|
||||
}
|
||||
|
||||
module.exports = function (filename, opts) {
|
||||
const jiti = require('../dist/jiti')
|
||||
|
||||
opts = { onError, ...opts }
|
||||
|
||||
if (!opts.transform) {
|
||||
opts.transform = require('../dist/babel')
|
||||
}
|
||||
|
||||
return jiti(filename, opts)
|
||||
}
|
||||
+75
-46
@@ -1,81 +1,110 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"jiti@0.1.11",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"jiti@1.14.0",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "jiti@0.1.11",
|
||||
"_id": "jiti@0.1.11",
|
||||
"_from": "jiti@1.14.0",
|
||||
"_id": "jiti@1.14.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-zSPegl+ageMLSYcq1uAZa6V56pX2GbNl/eU3Or7PFHu10a2YhLAXj5fnHJGd6cHZTalSR8zXGH8WmyuyufMhLA==",
|
||||
"_integrity": "sha512-4IwstlaKQc9vCTC+qUXLM1hajy2ImiL9KnLvVYiaHOtS/v3wRjhLlGl121AmgDgx/O43uKmxownJghS5XMya2A==",
|
||||
"_location": "/jiti",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "jiti@0.1.11",
|
||||
"raw": "jiti@1.14.0",
|
||||
"name": "jiti",
|
||||
"escapedName": "jiti",
|
||||
"rawSpec": "0.1.11",
|
||||
"rawSpec": "1.14.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "0.1.11"
|
||||
"fetchSpec": "1.14.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@nuxt/config",
|
||||
"/@nuxt/telemetry"
|
||||
"/@nuxt/telemetry",
|
||||
"/@nuxt/utils"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/jiti/-/jiti-0.1.11.tgz",
|
||||
"_spec": "0.1.11",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/jiti/-/jiti-1.14.0.tgz",
|
||||
"_spec": "1.14.0",
|
||||
"_where": "/home/node/nuxt",
|
||||
"bin": {
|
||||
"jiti": "bin/jiti.js"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/nuxt-contrib/jiti/issues"
|
||||
"url": "https://github.com/unjs/jiti/issues"
|
||||
},
|
||||
"description": "Runtime typescript and ESM support for Node.js (CommonJS)",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.10.2",
|
||||
"@babel/plugin-transform-modules-commonjs": "^7.10.1",
|
||||
"@babel/plugin-transform-typescript": "^7.10.1",
|
||||
"@babel/preset-typescript": "^7.10.1",
|
||||
"@nuxtjs/eslint-config-typescript": "^2.0.0",
|
||||
"@types/babel__core": "^7.1.8",
|
||||
"@types/mkdirp": "^1.0.1",
|
||||
"@types/node": "^14.0.13",
|
||||
"@types/resolve": "^1.17.1",
|
||||
"create-require": "^1.0.2",
|
||||
"eslint": "^7.2.0",
|
||||
"esm": "^3.2.25",
|
||||
"mkdirp": "^1.0.4",
|
||||
"resolve": "^1.17.0",
|
||||
"standard-version": "^8.0.0",
|
||||
"ts-loader": "^7.0.5",
|
||||
"tslib": "^2.0.0",
|
||||
"typescript": "^3.9.5",
|
||||
"v8-compile-cache": "^2.1.1",
|
||||
"webpack": "^5.0.0-beta.17",
|
||||
"webpack-cli": "^3.3.11"
|
||||
"@babel/core": "latest",
|
||||
"@babel/plugin-proposal-decorators": "latest",
|
||||
"@babel/plugin-proposal-nullish-coalescing-operator": "latest",
|
||||
"@babel/plugin-proposal-optional-chaining": "latest",
|
||||
"@babel/plugin-syntax-class-properties": "latest",
|
||||
"@babel/plugin-transform-modules-commonjs": "latest",
|
||||
"@babel/plugin-transform-typescript": "latest",
|
||||
"@babel/preset-typescript": "latest",
|
||||
"@babel/template": "latest",
|
||||
"@babel/types": "latest",
|
||||
"@nuxtjs/eslint-config-typescript": "latest",
|
||||
"@types/babel__core": "latest",
|
||||
"@types/babel__template": "latest",
|
||||
"@types/mkdirp": "latest",
|
||||
"@types/node": "latest",
|
||||
"@types/object-hash": "latest",
|
||||
"@types/resolve": "latest",
|
||||
"@types/semver": "latest",
|
||||
"acorn": "latest",
|
||||
"babel-plugin-dynamic-import-node": "latest",
|
||||
"babel-plugin-parameter-decorator": "latest",
|
||||
"create-require": "latest",
|
||||
"cross-env": "latest",
|
||||
"destr": "latest",
|
||||
"eslint": "latest",
|
||||
"esm": "latest",
|
||||
"estree-walker": "latest",
|
||||
"execa": "latest",
|
||||
"fast-glob": "latest",
|
||||
"mkdirp": "latest",
|
||||
"mlly": "latest",
|
||||
"object-hash": "latest",
|
||||
"pathe": "latest",
|
||||
"pirates": "latest",
|
||||
"pkg-types": "latest",
|
||||
"semver": "latest",
|
||||
"standard-version": "latest",
|
||||
"terser-webpack-plugin": "latest",
|
||||
"ts-loader": "latest",
|
||||
"tslib": "latest",
|
||||
"typescript": "latest",
|
||||
"vitest": "latest",
|
||||
"webpack": "latest",
|
||||
"webpack-cli": "latest"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"dist"
|
||||
"lib",
|
||||
"dist",
|
||||
"register.js"
|
||||
],
|
||||
"homepage": "https://github.com/nuxt-contrib/jiti#readme",
|
||||
"homepage": "https://github.com/unjs/jiti#readme",
|
||||
"license": "MIT",
|
||||
"main": "./index.js",
|
||||
"main": "./lib/index.js",
|
||||
"name": "jiti",
|
||||
"packageManager": "pnpm@7.3.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/nuxt-contrib/jiti.git"
|
||||
"url": "git+https://github.com/unjs/jiti.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "yarn clean && NODE_ENV=production yarn webpack",
|
||||
"build:babel": "ncc build src/babel.ts -t -m -o dist/babel",
|
||||
"build": "pnpm clean && cross-env NODE_ENV=production pnpm webpack",
|
||||
"clean": "rm -rf dist",
|
||||
"dev": "yarn clean && yarn webpack --watch",
|
||||
"dev": "pnpm clean && pnpm webpack --watch",
|
||||
"jiti": "cross-env JITI_DEBUG=1 JITI_CACHE=false ./bin/jiti.js",
|
||||
"jiti:legacy": "cross-env JITI_DEBUG=1 npx node@12 ./bin/jiti.js",
|
||||
"lint": "eslint --ext .ts,.js .",
|
||||
"release": "yarn test && yarn build && yarn standard-version && git push --follow-tags && npm publish",
|
||||
"test": "yarn lint"
|
||||
"release": "pnpm test && pnpm build && pnpm standard-version && git push --follow-tags && pnpm publish",
|
||||
"test": "vitest"
|
||||
},
|
||||
"types": "dist/jiti.d.ts",
|
||||
"version": "0.1.11"
|
||||
"version": "1.14.0"
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
const jiti = require('.')()
|
||||
|
||||
jiti.register()
|
||||
Reference in New Issue
Block a user