Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
chore(tools): cleanup esm loader
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Oct 30, 2021
1 parent ffa44cf commit d4f10f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
1 change: 1 addition & 0 deletions .eslintrc.base.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ module.exports = {
'basedir',
'cjs',
'commonjs',
'ctx',
'dotenv',
'esm',
'extensionless',
Expand Down
49 changes: 26 additions & 23 deletions tools/loaders/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'fs/promises'
import path from 'path'
import path from 'node:path'
import { createMatchPath, loadConfig } from 'tsconfig-paths'
/** @type {import('ts-node/dist/esm').registerAndCreateEsmHooks} */
const hooks = await import('ts-node/esm')
Expand All @@ -8,17 +7,16 @@ const hooks = await import('ts-node/esm')
* @file Helpers - Custom ESM Loader Hooks
* @module tools/loaders/esm
* @see https://github.com/TypeStrong/ts-node/issues/1007
* @see https://nodejs.org/docs/latest-v16.x/api/all.html#esm_hooks
* @see https://nodejs.org/docs/latest-v12.x/api/all.html#esm_hooks
*/

/** @typedef {'builtin'|'commonjs'|'dynamic'|'json'|'module'|'wasm'} Format */
/** @typedef {{ parentURL: string }} ResolveContext */
/** @typedef {{ url: string }} ResolvedFile */

/**
* Determines if `url` should be interpreted as a CommonJS or ES module.
*
* @see https://github.com/nodejs/modules/issues/488#issuecomment-804895142
*
* @async
* @param {string} url - File URL
* @param {Record<string, never>} ctx - Resolver context
Expand All @@ -40,25 +38,25 @@ export const getFormat = async (url, ctx, defaultGetFormat) => {
// See `tsconfig.json#ts-node.moduleTypes` for file-specific overrides
if (ext === '.ts') return { format: 'module' }

// Defer to Node.js for all other URLs
return defaultGetFormat(url, ctx, defaultGetFormat)
// Defer to ts-node for all other URLs
return await hooks.getFormat(url, ctx, defaultGetFormat)
}

/**
* Returns the resolved file URL for a given module specifier and parent URL.
* Returns the resolved file URL for a given module `specifier` and parent URL.
*
* @see https://github.com/TypeStrong/ts-node/discussions/1450
* @see https://github.com/dividab/tsconfig-paths
*
* @async
* @param {string} specifier - `import` statement / `import()` expression string
* @param {ResolveContext} context - Resolver context
* @param {string} [context.parentURL] - URL of module that imported `specifier`
* @param {typeof hooks.resolve} defaultResolve - Default resolver function
* @return {Promise<{ url: string }>} Promise containing resolved file URL
* @param {string} specifier - Module specifier
* @param {ResolveContext} ctx - Function context
* @param {string} [ctx.parentURL] - URL of module that imported `specifier`
* @param {typeof hooks.resolve} defaultResolve - Default resolve fn
* @return {Promise<ResolvedFile>} Resolved file URL
* @throws {Error}
*/
export const resolve = async (specifier, context, defaultResolve) => {
export const resolve = async (specifier, ctx, defaultResolve) => {
// Load TypeScript config to get path mappings
const result = loadConfig(process.cwd())

Expand All @@ -72,16 +70,21 @@ export const resolve = async (specifier, context, defaultResolve) => {
const match = createMatchPath(absoluteBaseUrl, paths)(specifier)

// Update specifier if match was found
if (match) {
try {
const directory = (await fs.lstat(match)).isDirectory()
specifier = `${match}${directory ? '/index.js' : '.js'}`
} catch {
specifier = `${match}.js`
}
}
if (match) specifier = match

return hooks.resolve(specifier, context, defaultResolve)
// Defer to ts-node
return await hooks.resolve(specifier, ctx, defaultResolve)
}

/**
* Applies transformations to `source`.
*
* @async
* @param {Buffer | string} source - Source code to transform
* @param {{ format: Format; url: string }} ctx - Function context
* @param {Format} [ctx.format] - Module format of source code
* @param {string} [ctx.url] - Source code file URL
* @param {typeof transformSource} defaultTransformSource - Default transform fn
* @return {Promise<{ source: string }>} Promise containing source code
*/
export const transformSource = hooks.transformSource

0 comments on commit d4f10f6

Please sign in to comment.