Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pooya parsa committed Jun 6, 2020
0 parents commit ae9eb72
Show file tree
Hide file tree
Showing 17 changed files with 3,138 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"@nuxtjs/eslint-config-typescript"
]
}
45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
*.log*
45 changes: 45 additions & 0 deletions README.md
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
39 changes: 39 additions & 0 deletions package.json
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"
}
}
27 changes: 27 additions & 0 deletions rollup.config.js
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'] })
]
}
67 changes: 67 additions & 0 deletions src/index.ts
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
}
17 changes: 17 additions & 0 deletions src/transform.ts
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
}
5 changes: 5 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-console": 0
}
}
1 change: 1 addition & 0 deletions test/fixtures/esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as test } from './test'
6 changes: 6 additions & 0 deletions test/fixtures/esm/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function test () {
return {
file: __filename,
dir: __dirname
}
}
1 change: 1 addition & 0 deletions test/fixtures/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as test } from './test'
6 changes: 6 additions & 0 deletions test/fixtures/typescript/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function test () {
return {
file: __filename,
dir: __dirname
}
}
4 changes: 4 additions & 0 deletions test/jiti.js
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())
17 changes: 17 additions & 0 deletions tsconfig.json
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"
]
}
Loading

0 comments on commit ae9eb72

Please sign in to comment.