default to esm when running in deno

This commit is contained in:
Nathan Whitaker 2024-10-15 17:27:49 -07:00
parent 458e6c81fe
commit 26c13a28bf
No known key found for this signature in database

View file

@ -289,10 +289,16 @@ export function fileIsModule(file: string): boolean {
function folderIsModule(folder: string): boolean { function folderIsModule(folder: string): boolean {
const packageJsonPath = getPackageJsonPath(folder); const packageJsonPath = getPackageJsonPath(folder);
// deno is ESM unless package.json specifies otherwise
const isDeno = typeof process.versions.deno === 'string';
if (!packageJsonPath) if (!packageJsonPath)
return isDeno;
const type = require(packageJsonPath).type;
if (type === 'module')
return true;
if (type === 'commonjs')
return false; return false;
// Rely on `require` internal caching logic. return isDeno;
return require(packageJsonPath).type === 'module';
} }
const packageJsonMainFieldCache = new Map<string, string | undefined>(); const packageJsonMainFieldCache = new Map<string, string | undefined>();