diff --git a/use.cjs b/use.cjs index 3917be3..566bc4f 100644 --- a/use.cjs +++ b/use.cjs @@ -618,7 +618,7 @@ const baseUse = async (modulePath) => { // More robust default export handling for cross-environment compatibility const keys = Object.keys(module); - // If it's a Module object with a default property, unwrap it + // If it's a Module object with a default property, decide whether to unwrap it if (module.default !== undefined) { // Check if this is likely a CommonJS module with only default export if (keys.length === 1 && keys[0] === 'default') { @@ -638,6 +638,21 @@ const baseUse = async (modulePath) => { if (nonMetadataKeys.length === 0) { return module.default; } + + // Enhanced heuristic: Return default export for packages that primarily export through default + // but are commonly used as their default export (like http clients, main APIs, etc.) + if (typeof module.default === 'function') { + // If there are many utility exports but the default seems to be the main API, + // and the module doesn't export things users typically destructure (like $, sh, cli tools) + const hasCommonDestructuredExports = nonMetadataKeys.some(key => + /^(\$|sh|cli|cmd|exec|run|create|make|build|test)$/i.test(key) + ); + + // Return default if it's a function and there are no common destructured exports + if (!hasCommonDestructuredExports) { + return module.default; + } + } } // Return the whole module if it has multiple meaningful exports or no default diff --git a/use.js b/use.js index c2dcb3b..db67b1d 100644 --- a/use.js +++ b/use.js @@ -618,7 +618,7 @@ const baseUse = async (modulePath) => { // More robust default export handling for cross-environment compatibility const keys = Object.keys(module); - // If it's a Module object with a default property, unwrap it + // If it's a Module object with a default property, decide whether to unwrap it if (module.default !== undefined) { // Check if this is likely a CommonJS module with only default export if (keys.length === 1 && keys[0] === 'default') { @@ -638,6 +638,21 @@ const baseUse = async (modulePath) => { if (nonMetadataKeys.length === 0) { return module.default; } + + // Enhanced heuristic: Return default export for packages that primarily export through default + // but are commonly used as their default export (like http clients, main APIs, etc.) + if (typeof module.default === 'function') { + // If there are many utility exports but the default seems to be the main API, + // and the module doesn't export things users typically destructure (like $, sh, cli tools) + const hasCommonDestructuredExports = nonMetadataKeys.some(key => + /^(\$|sh|cli|cmd|exec|run|create|make|build|test)$/i.test(key) + ); + + // Return default if it's a function and there are no common destructured exports + if (!hasCommonDestructuredExports) { + return module.default; + } + } } // Return the whole module if it has multiple meaningful exports or no default diff --git a/use.mjs b/use.mjs index 250fe81..478dc45 100644 --- a/use.mjs +++ b/use.mjs @@ -618,7 +618,7 @@ export const baseUse = async (modulePath) => { // More robust default export handling for cross-environment compatibility const keys = Object.keys(module); - // If it's a Module object with a default property, unwrap it + // If it's a Module object with a default property, decide whether to unwrap it if (module.default !== undefined) { // Check if this is likely a CommonJS module with only default export if (keys.length === 1 && keys[0] === 'default') { @@ -638,6 +638,21 @@ export const baseUse = async (modulePath) => { if (nonMetadataKeys.length === 0) { return module.default; } + + // Enhanced heuristic: Return default export for packages that primarily export through default + // but are commonly used as their default export (like http clients, main APIs, etc.) + if (typeof module.default === 'function') { + // If there are many utility exports but the default seems to be the main API, + // and the module doesn't export things users typically destructure (like $, sh, cli tools) + const hasCommonDestructuredExports = nonMetadataKeys.some(key => + /^(\$|sh|cli|cmd|exec|run|create|make|build|test)$/i.test(key) + ); + + // Return default if it's a function and there are no common destructured exports + if (!hasCommonDestructuredExports) { + return module.default; + } + } } // Return the whole module if it has multiple meaningful exports or no default