Skip to content

Commit

Permalink
refactor: simplify loader detection
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 11, 2024
1 parent b34c93d commit 430cf79
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ const mod = await import('importx')

## Runtime-Loader Compatibility Table

Importing a TypeScript module with `importx`:

<!-- TABLE_START -->

> Generated with version v0.0.1 at 2024-05-11T03:58:02.482Z
> Generated with version v0.0.2 at 2024-05-11T04:17:16.249Z
| | native | tsx | jiti | bundle-require |
| ------- | --- | --- | --- | --- |
Expand Down
51 changes: 20 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,22 @@ export async function isNativeTsImportSupported(): Promise<boolean> {
return _isNativeTsImportSupported
}

async function _detectLoaderImpl(): Promise<SupportedLoader> {
if (await isNativeTsImportSupported())
const nodeVersionNumbers = globalThis?.process?.versions?.node?.split('.').map(Number)

/**
* Detect the 'auto' loader to use for importing the file.
*/
async function detectLoader(cache: boolean | null, isTsFile: boolean): Promise<SupportedLoader> {
if (cache === false)
return 'tsx'

if (!isTsFile || await isNativeTsImportSupported())
return 'native'

const nodeVersion = globalThis?.process?.versions?.node?.split('.').map(Number)
if (!nodeVersion)
if (cache === true)
return 'jiti'

if (!nodeVersionNumbers)
return 'tsx'

/**
Expand All @@ -120,30 +130,19 @@ async function _detectLoaderImpl(): Promise<SupportedLoader> {
* @see https://nodejs.org/api/module.html#moduleregisterspecifier-parenturl-options
*/
if (
nodeVersion[0] < 18
|| (nodeVersion[0] === 18 && nodeVersion[1] < 19)
|| (nodeVersion[0] === 20 && nodeVersion[1] < 8)
nodeVersionNumbers[0] < 18
|| (nodeVersionNumbers[0] === 18 && nodeVersionNumbers[1] < 19)
|| (nodeVersionNumbers[0] === 20 && nodeVersionNumbers[1] < 8)
)
return 'jiti'

return 'tsx'
}

let _loader: Promise<SupportedLoader>

/**
* Detect the 'auto' loader to use for importing the file.
*/
export async function detectLoader() {
if (!_loader)
_loader = _detectLoaderImpl()
return _loader
}

const reIsTypeScriptFile = /\.[mc]?tsx?$/

export function isTypeScriptFile(path: string) {
return path.match(reIsTypeScriptFile)
return reIsTypeScriptFile.test(path)
}

/**
Expand Down Expand Up @@ -173,18 +172,8 @@ export async function importTs<T = any>(path: string, options: string | URL | Im
} = options

let loader = options.loader || 'auto'
if (loader === 'auto') {
if (cache === false) {
loader = 'tsx'
}
else {
loader = isTypeScriptFile(path)
? await detectLoader()
: 'native'
if (cache === true && (loader === 'tsx' || loader === 'bundle-require'))
loader = 'jiti'
}
}
if (loader === 'auto')
loader = await detectLoader(cache, isTypeScriptFile(path))

debug(`[${loader}]`, 'Importing', path, 'from', parentURL)

Expand Down
2 changes: 1 addition & 1 deletion test/run-table.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ for (const loader of loaders) {
if (stderr)
object.errors = stderr

console.log(object, { stdout })
console.log(object)

records.push(object)
}
Expand Down

0 comments on commit 430cf79

Please sign in to comment.