-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pooya parsa
committed
Jun 6, 2020
0 parents
commit ae9eb72
Showing
17 changed files
with
3,138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": [ | ||
"@nuxtjs/eslint-config-typescript" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: ci | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
ci: | ||
runs-on: ${{ matrix.os }} | ||
|
||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
node: [10] | ||
|
||
steps: | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node }} | ||
|
||
- name: checkout | ||
uses: actions/checkout@master | ||
|
||
- name: cache node_modules | ||
uses: actions/cache@v1 | ||
with: | ||
path: node_modules | ||
key: ${{ matrix.os }}-node-v${{ matrix.node }}-deps-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }} | ||
|
||
- name: Install dependencies | ||
if: steps.cache.outputs.cache-hit != 'true' | ||
run: yarn | ||
|
||
- name: Lint | ||
run: yarn lint | ||
|
||
# - name: Test | ||
# run: yarn jest | ||
|
||
# - name: Coverage | ||
# uses: codecov/codecov-action@v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
dist | ||
*.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# jiti | ||
|
||
Require with just-in-time compiler for typescript and esm files | ||
|
||
## Features | ||
|
||
- Stable support for typescript and esm syntax | ||
- Provide sync interface to use inplace of `esm` or `require` | ||
- Super slim and zero dependency (~1.8M install size) | ||
- Works with CJS cache | ||
- [ ] Filesystem caching | ||
- [ ] Syntax detect to avoid extra transforms | ||
|
||
## Usage | ||
|
||
```js | ||
const jiti = require('jiti')(__filename) | ||
|
||
jiti('./path/to/file.ts') | ||
``` | ||
|
||
## How it works | ||
|
||
Transform is based on babel and babel-preset-env | ||
|
||
## Development | ||
|
||
- Clone Repo | ||
- Run `yarn` | ||
- Run `yarn build` | ||
- Run `yarn dev` | ||
- Run `node ./test/jiti.js` | ||
|
||
## Roadmap | ||
|
||
- [x] Basic working | ||
- [ ] File based caching | ||
- [ ] Syntax detect and fallback to CJS require | ||
- [ ] Configurable transform | ||
- [ ] Try sourcemap improvements | ||
- [ ] Simplify project build system | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "jiti", | ||
"version": "0.0.0", | ||
"description": "", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "yarn build:transform && yarn build:jit", | ||
"dev": "yarn build:jit --watch", | ||
"build:jit": "rollup -c", | ||
"build:transform": "ncc build src/transform.ts -t -m -o dist/transform", | ||
"lint": "eslint --ext .ts,.js .", | ||
"test": "yarn lint" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.10.2", | ||
"@babel/plugin-transform-typescript": "^7.10.1", | ||
"@babel/preset-env": "^7.10.2", | ||
"@babel/preset-typescript": "^7.10.1", | ||
"@nuxtjs/eslint-config-typescript": "^2.0.0", | ||
"@rollup/plugin-commonjs": "^13.0.0", | ||
"@rollup/plugin-json": "^4.1.0", | ||
"@rollup/plugin-node-resolve": "^8.0.1", | ||
"@types/babel__core": "^7.1.8", | ||
"@types/node": "^14.0.11", | ||
"@zeit/ncc": "^0.22.3", | ||
"create-require": "^1.0.1", | ||
"eslint": "^7.2.0", | ||
"resolve": "^1.17.0", | ||
"rollup": "^2.13.1", | ||
"rollup-plugin-typescript2": "^0.27.1", | ||
"tslib": "^2.0.0", | ||
"typescript": "^3.9.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { builtinModules } from 'module' | ||
import typescript from 'rollup-plugin-typescript2' | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import resolve from '@rollup/plugin-node-resolve' | ||
import json from '@rollup/plugin-json' | ||
|
||
const output = { | ||
dir: 'dist', | ||
format: 'cjs', | ||
preferConst: true | ||
} | ||
|
||
const external = [ | ||
...builtinModules | ||
] | ||
|
||
export default { | ||
input: './src/index.ts', | ||
external, | ||
output, | ||
plugins: [ | ||
json(), | ||
typescript(), | ||
resolve(), | ||
commonjs({ extensions: ['.js', '.ts'] }) | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { readFileSync } from 'fs' | ||
import { Module, builtinModules } from 'module' | ||
import { dirname } from 'path' | ||
import _createRequire from 'create-require' | ||
import resolve from 'resolve' | ||
|
||
export default function jiti (_filename: string): NodeRequire { | ||
const { transform } = require('./transform') | ||
const _require = _createRequire(_filename) | ||
|
||
// https://www.npmjs.com/package/resolve | ||
const resolveOpts = { | ||
extensions: ['.js', '.mjs', '.ts'], | ||
basedir: dirname(_filename) | ||
} | ||
|
||
function requireJIT (id: string) { | ||
// Check for builtin node module like fs | ||
if (builtinModules.includes(id)) { | ||
return _require(id) | ||
} | ||
|
||
// Resolve path | ||
const filename = resolve.sync(id, resolveOpts) | ||
|
||
// Check for CJS cache | ||
if (_require.cache[filename]) { | ||
return _require.cache[filename]?.exports | ||
} | ||
|
||
// Read source | ||
let source = readFileSync(filename, 'utf-8') | ||
|
||
// Apply transform | ||
source = transform(source) | ||
|
||
// Compile module | ||
const mod = new Module(filename) | ||
mod.filename = filename | ||
mod.parent = module | ||
mod.require = jiti(filename) | ||
// @ts-ignore | ||
mod.path = dirname(filename) | ||
// @ts-ignore | ||
mod.paths = Module._nodeModulePaths(mod.path) | ||
|
||
// Compile module | ||
// @ts-ignore | ||
mod._compile(source, filename) | ||
|
||
// Set as loaded | ||
mod.loaded = true | ||
|
||
// Set cache entry | ||
_require.cache[filename] = mod | ||
|
||
// Return exports | ||
return mod.exports | ||
} | ||
|
||
requireJIT.resolve = _require.resolve | ||
requireJIT.cache = _require.cache | ||
requireJIT.extensions = _require.extensions | ||
requireJIT.main = _require.main | ||
|
||
return requireJIT | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { transformSync } from '@babel/core' | ||
// @ts-ignore | ||
import babelPresetEnv from '@babel/preset-env' | ||
|
||
export function transform (source: string): string { | ||
const result = transformSync(source, { | ||
presets: [ | ||
[babelPresetEnv, { | ||
targets: { | ||
node: 'current' | ||
} | ||
}] | ||
] | ||
})?.code || '' | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"no-console": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as test } from './test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default function test () { | ||
return { | ||
file: __filename, | ||
dir: __dirname | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as test } from './test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default function test () { | ||
return { | ||
file: __filename, | ||
dir: __dirname | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const jiti = require('..')(__filename) | ||
|
||
console.log(jiti('./fixtures/esm').test()) | ||
console.log(jiti('./fixtures/typescript').test()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES6", | ||
"module": "ESNext", | ||
"moduleResolution": "Node", | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"declarationDir": "dist", | ||
"declaration": true, | ||
"types": [ | ||
"node" | ||
] | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
} |
Oops, something went wrong.