npm package: @davidwells/extract-deps
Description: Get imports and requires from a codebase without AST
Author: David Wells
Latest version: 0.1.4

# extract-deps

A lightweight utility for extracting imports, requires, and exports from JavaScript/TypeScript code without using AST parsing.

## Features

- Extract ES6 imports (static and dynamic)
- Extract CommonJS requires (static and dynamic)
- Extract ES6 exports (default, named, namespace, type exports)
- Parse code without AST for better performance
- Support for destructuring in imports/requires
- TypeScript support for type exports and imports
- Handles complex patterns like re-exports and aliased exports

## Installation

```bash
npm install @davidwells/extract-deps
```

## Usage

### Basic Usage

```javascript
const { extractDeps } = require('@davidwells/extract-deps')

const code = `
import { test } from 'uvu'
import assert from 'uvu/assert'
const { execSync } = require('child_process')
const fs = require('fs')

export const myFunction = () => {}
export default class MyClass {}
`

const { imports, requires, exports } = extractDeps(code)

console.log('Imports:', imports)
console.log('Requires:', requires)  
console.log('Exports:', exports)
```

### Extract Imports Only

```javascript
const { getImports, getStaticImports, getDynamicImports } = require('@davidwells/extract-deps')

const code = `
import { test } from 'uvu'
import assert from 'uvu/assert'

// Dynamic imports
const dynamicModule = await import('./utils')
const config = await import('./config.js')
`

// Get all imports
const allImports = getImports(code)

// Get only static imports
const staticImports = getStaticImports(code)

// Get only dynamic imports  
const dynamicImports = getDynamicImports(code)

console.log('All imports:', allImports)
console.log('Static imports:', staticImports)
console.log('Dynamic imports:', dynamicImports)
```

### Extract Requires Only

```javascript
const { getRequires, getStaticRequires, getDynamicRequires } = require('@davidwells/extract-deps')

const code = `
const { test } = require('uvu')
const assert = require('uvu/assert')
const fs = require('fs')
`

// Get all requires
const allRequires = getRequires(code)

// Get only static requires
const staticRequires = getStaticRequires(code)

// Get only dynamic requires
const dynamicRequires = getDynamicRequires(code)

console.log('All requires:', allRequires)
console.log('Static requires:', staticRequires)
console.log('Dynamic requires:', dynamicRequires)
```

### Extract Exports Only

```javascript
const { 
  getExports, 
  getDefaultExports, 
  getNamedExports,
  getNamespaceExports,
  getTypeExports 
} = require('@davidwells/extract-deps')

const code = `
// Default exports
export default function myFunction() {}
export default class MyClass {}

// Named exports
export const myVariable = 'value'
export function myFunction() {}

// Named declaration exports
export { myFunction, anotherFunction }
export { myFunction as aliasedFunction }

// Re-exports
export * from './module'
export * as namespace from './module'

// Type exports
export type MyType = string
export interface MyInterface {}
`

// Get all exports
const allExports = getExports(code)

// Get specific export types
const defaultExports = getDefaultExports(code)
const namedExports = getNamedExports(code)
const namespaceExports = getNamespaceExports(code)
const typeExports = getTypeExports(code)

console.log('All exports:', allExports)
console.log('Default exports:', defaultExports)
console.log('Named exports:', namedExports)
console.log('Namespace exports:', namespaceExports)
console.log('Type exports:', typeExports)
```

## API Reference

### `extractDeps(code)`

Main function that extracts all imports, requires, and exports from the given code.

**Parameters:**
- `code` (string): The JavaScript/TypeScript code to analyze

**Returns:**
- Object with `imports`, `requires`, and `exports` arrays

### Import Functions

- `getImports(code)` - Get all imports (static and dynamic)
- `getStaticImports(code)` - Get only static imports
- `getDynamicImports(code)` - Get only dynamic imports

### Require Functions

- `getRequires(code)` - Get all requires (static and dynamic)
- `getStaticRequires(code)` - Get only static requires
- `getDynamicRequires(code)` - Get only dynamic requires

### Export Functions

- `getExports(code)` - Get all exports
- `getDefaultExports(code)` - Get only default exports
- `getNamedExports(code)` - Get only named exports
- `getNamedDeclarationExports(code)` - Get named declaration exports
- `getAllExports(code)` - Get all exports (alias for getExports)
- `getNamespaceExports(code)` - Get namespace exports (export * as)
- `getTypeExports(code)` - Get TypeScript type exports

## Data Structure

Each import/require/export object contains:

```javascript
{
  moduleSpecifier: string,    // The module being imported/required
  exportType: string,         // Type of export (for exports)
  exportName: string,         // Name of export (for exports)
  definitions: Array,         // Destructuring and variable definitions
  // ... other properties specific to the type
}
```

## Examples

You can find more examples in the `examples/` directory and usage examples in `_usage.js`.

## Performance

This library avoids AST parsing for better performance when you only need to extract dependency information. It uses regex-based parsing optimized for common JavaScript/TypeScript patterns.

## Alternative Approaches

- Runtime analysis: [pkg-exports](https://github.com/antfu/pkg-exports/)
- AST-based parsing: [parse-imports](https://github.com/import-js/parse-imports)
- ESM/CJS utils https://www.npmjs.com/package/mlly

## License

MIT