Skip to content

Commit

Permalink
fix(cjs): tsx.require to support core modules
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Jun 29, 2024
1 parent c67e3ba commit f748e19
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/cjs/api/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const resolveContext = (
throw new Error('The current file path (__filename or import.meta.url) must be provided in the second argument of tsx.require()');
}

// If id is not a relative path, it doesn't need to be resolved
if (!id.startsWith('.')) {
return id;
}

if (
(typeof fromFile === 'string' && fromFile.startsWith('file://'))
|| fromFile instanceof URL
Expand Down Expand Up @@ -86,7 +91,7 @@ export const register: Register = (
const [request, query] = resolvedId.split('?');

const parameters = new URLSearchParams(query);
if (options.namespace) {
if (options.namespace && !request.startsWith('node:')) {
parameters.set('namespace', options.namespace);
}

Expand All @@ -100,7 +105,7 @@ export const register: Register = (
const [request, query] = resolvedId.split('?');

const parameters = new URLSearchParams(query);
if (options.namespace) {
if (options.namespace && !request.startsWith('node:')) {
parameters.set('namespace', options.namespace);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/specs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ export default testSuite(({ describe }, node: NodeApis) => {
const loaded = tsx.require('./file', __filename);
console.log(loaded.message);
// Can require core modules
tsx.require('node:path', __filename);
tsx.require('fs', __filename);
// Remove from cache
const loadedPath = tsx.require.resolve('./file', __filename);
console.log(loadedPath.split(path.sep).pop());
Expand Down

0 comments on commit f748e19

Please sign in to comment.