Skip to content

Commit

Permalink
fix: move esm resolve behind a flag
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 26, 2022
1 parent 0ea19c8 commit 60e094c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ Use transpile cache

If set to `true` will use `node_modules/.cache/jiti` (if exists) or `{TMP_DIR}/node-jiti`

### `esmResolve`

- Type: Boolean | String
- Default: `false`
- Environment Vriable: `JITI_ESM_RESOLVE`

Using esm resolution algorithm to support `import` condition.

### `transform`

- Type: Function
Expand Down
27 changes: 16 additions & 11 deletions src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import { TransformOptions, JITIOptions } from './types'

const _EnvDebug = destr(process.env.JITI_DEBUG)
const _EnvCache = destr(process.env.JITI_CACHE)
const _EnvESMReolve = destr(process.env.JITI_ESM_RESOLVE)
const _EnvRequireCache = destr(process.env.JITI_REQUIRE_CACHE)

const defaults: JITIOptions = {
debug: _EnvDebug,
cache: _EnvCache !== undefined ? !!_EnvCache : true,
requireCache: _EnvRequireCache !== undefined ? !!_EnvRequireCache : true,
interopDefault: false,
esmResolve: _EnvESMReolve || false,
cacheVersion: '6',
legacy: lt(process.version || '0.0.0', '14.0.0'),
extensions: ['.js', '.mjs', '.cjs', '.ts']
Expand Down Expand Up @@ -84,18 +86,21 @@ export default function createJITI (_filename: string, opts: JITIOptions = {}, p
const _url = pathToFileURL(_filename)
const _additionalExts = [...opts.extensions!].filter(ext => ext !== '.js')
const _resolve = (id: string, options?: { paths?: string[] }) => {
// Try ESM resolve
let resolved, err
try {
resolved = resolvePathSync(id, {
url: _url,
conditions: ['node', 'require', 'import']
})
} catch (_err) {
err = _err
}
if (resolved) {
return resolved

// Try ESM resolve
if (opts.esmResolve) {
try {
resolved = resolvePathSync(id, {
url: _url,
conditions: ['node', 'require', 'import']
})
} catch (_err) {
err = _err
}
if (resolved) {
return resolved
}
}

// Try native require resolve
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type JITIOptions = {
requireCache?: boolean
v8cache?: boolean
interopDefault?: boolean
esmResolve?: boolean,
cacheVersion?: string
onError?: (error: Error) => void
legacy?: boolean
Expand Down

0 comments on commit 60e094c

Please sign in to comment.