A powerful TypeScript utility for recursively analyzing and extracting exports from JavaScript/TypeScript packages and modules. Handles both CommonJS and ES Module patterns with robust support for default exports, prototype methods, and lazy-loaded getters.
- π Deep Analysis: Recursively traverse package objects to find all exports
- π― Smart Detection: Automatically handles CommonJS and ES Module patterns
- π‘οΈ Type Safe: Full TypeScript support with comprehensive type definitions
- β‘ Flexible: Configurable depth, exclusions, and analysis options
- π§ Robust: Handles edge cases like getters, setters, and prototype methods
- π Detailed: Provides both export lists and callable function objects
npm install export-scanner
yarn add export-scanner
pnpm add export-scanner
import { analyzeExports, getExports, listExportNames } from 'export-scanner'
import * as myLib from 'some-library'
// Get a list of all export names
const exportNames = listExportNames(myLib)
console.log(exportNames)
// ['foo', 'bar.baz', 'MyClass.prototype.method']
// Get callable functions and classes
const callables = getExports(myLib)
console.log(Object.keys(callables))
// Access functions: callables.foo(), callables['bar.baz']()
// Get comprehensive analysis
const analysis = analyzeExports(myLib)
console.log(analysis.summary)
// { totalFunctions: 5, totalExports: 8, hasDefault: true, ... }
Recursively analyzes a package object and returns a list of export names.
pkg
- The package or module object to analyzeoptions
- Optional configuration object
Option | Type | Default | Description |
---|---|---|---|
maxDepth |
number |
3 |
Maximum recursion depth for nested objects |
excludedKeys |
string[] |
['constructor', 'prototype', ...] |
Property names to exclude |
includePrivate |
boolean |
false |
Include properties starting with _ or __ |
includeNonFunctions |
boolean |
false |
Include non-function exports |
followPrototypes |
boolean |
true |
Include prototype methods |
debug |
boolean |
false |
Enable verbose debug logging |
Array of export names as strings, including nested paths (e.g., "foo.bar.baz"
).
Extracts all callable exports (functions and classes) from a package object.
pkg
- The package or module object to extract fromoptions
- Optional configuration object (extendslistExportNames
options)
Option | Type | Default | Description |
---|---|---|---|
includeClasses |
boolean |
true |
Include classes in the result |
Object containing all extracted functions and classes, keyed by their property paths.
Provides comprehensive analysis of package exports.
{
functions: string[]; // Function export names
allExports: string[]; // All export names
summary: {
totalFunctions: number; // Count of function exports
totalExports: number; // Total export count
hasDefault: boolean; // Has default export
isFunction: boolean; // Package itself is a function
isObject: boolean; // Package is an object
};
}
import { listExportNames } from 'export-scanner'
import * as lodash from 'lodash'
const exports = listExportNames(lodash, {
maxDepth: 2,
includeNonFunctions: true
})
console.log(exports)
// ['map', 'filter', 'reduce', 'debounce', 'throttle', ...]
import { analyzeExports } from 'export-scanner'
import React from 'react'
const analysis = analyzeExports(React, {
followPrototypes: true,
includePrivate: false
})
console.log(analysis.summary)
// { totalFunctions: 15, hasDefault: true, ... }
import * as fs from 'node:fs'
import { getExports } from 'export-scanner'
const callables = getExports(fs, {
maxDepth: 1
})
// Now you can call functions dynamically
if (callables.readFile) {
// callables.readFile is the actual fs.readFile function
}
import { listExportNames } from 'export-scanner'
const exports = listExportNames(somePackage, {
debug: true // Enables detailed console logging
})
const exports = listExportNames(myPackage, {
excludedKeys: [
'constructor',
'prototype',
'__internal',
'deprecatedMethod'
]
})
const exports = listExportNames(myPackage, {
includePrivate: true, // Includes _private and __internal methods
maxDepth: 5
})
Contributions are welcome! Please feel free to submit a Pull Request.
MIT