-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose CommonJS API via
tsx/cjs/api
(#3)
- Loading branch information
1 parent
b63019c
commit 42b7ede
Showing
4 changed files
with
127 additions
and
2 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
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { register } from './global-require-patch.js'; | ||
export { require } from './require.js'; |
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,48 @@ | ||
import path from 'path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { register } from './global-require-patch.js'; | ||
import { resolveFilename } from './module-resolve-filename.js'; | ||
|
||
const getRequestContext = ( | ||
filepath: string | URL, | ||
) => { | ||
if ( | ||
(typeof filepath === 'string' && filepath.startsWith('file://')) | ||
|| filepath instanceof URL | ||
) { | ||
filepath = fileURLToPath(filepath); | ||
} | ||
return path.dirname(filepath); | ||
}; | ||
|
||
const tsxRequire = ( | ||
id: string, | ||
fromFile: string | URL, | ||
) => { | ||
const unregister = register(); | ||
try { | ||
const contextId = path.resolve(getRequestContext(fromFile), id); | ||
|
||
// eslint-disable-next-line import-x/no-dynamic-require, n/global-require | ||
return require(contextId); | ||
} finally { | ||
unregister(); | ||
} | ||
}; | ||
|
||
const resolve = ( | ||
id: string, | ||
fromFile: string | URL, | ||
options?: { paths?: string[] | undefined }, | ||
) => { | ||
const contextId = path.resolve(getRequestContext(fromFile), id); | ||
return resolveFilename(contextId, module, false, options); | ||
}; | ||
resolve.paths = require.resolve.paths; | ||
|
||
tsxRequire.resolve = resolve; | ||
tsxRequire.main = require.main; | ||
tsxRequire.extensions = require.extensions; | ||
tsxRequire.cache = require.cache; | ||
|
||
export { tsxRequire as require }; |