depseek

Seeks for dependency references in JS/TS code

MIT 17 个版本
安装
npm install depseek
yarn add depseek
pnpm add depseek
bun add depseek
README

depseek

Seeks for dependency references in JS/TS code

lcov npm

Motivation

Dep extraction is a common task for many tools solved in different ways from regexps to AST parsing. This implementation relies on streams to make controllable memory consumption.

Status

Working draft

Key features

  • Uses stream-based reader
  • Points exact dependency references by offset
  • Handles string literal and comments
  • Captures bound comments (optional)

Usage

npm i depseek

Usage

import fs from 'fs'
import {depseek} from 'depseek'

const stream = fs.createReadStream('index.js')
const deps = await depseek(stream)

// returns
[
  { type: 'dep', value: 'node:fs', index: 17 },
  { type: 'dep', value: 'foo', index: 34 },
  { type: 'dep', value: 'q', index: 92 }
  // ...
]

Options

By default depseek extracts only require and import arguments. You can also capture bound comments.

const depsAndComments = await depseek(stream, {comments: true})

[
  { type: 'dep', value: 'node:fs', index: 17 },
  { type: 'dep', value: 'foo', index: 34 },
  { type: 'comment', value: ' @1.0.0', index: 46 }
  //...
]

Stream buffer size set to 1000 by default. You can change the limit by passing bufferSize.

const deps = await depseek(stream, {bufferSize: 10000})

Sync

Streams are aimed at intensive bulk operations. If you need to process just a few files, you can use depseekSync.

import fs from 'node:fs'
import { depseekSync } from 'depseek'

const contents = fs.readFileSync('index.js', 'utf8') // Buffer or string
const deps = depseekSync(contents)

patchRefs

The one more utility is patchRefs that replaces dependency references with a given value.

import {patchRefs} from 'depseek'

const patcher = (v: string) => v.startsWith('.') ? v + '.js' : v
const input = `
import {foo} from './foo'
import {bar} from "./bar"
import {baz} from 'baz'
`

patchRefs(input, patcher)
// gives as a result:
`
import {foo} from './foo.js'
import {bar} from "./bar.js"
import {baz} from 'baz'
`

Refs

License

MIT

版本列表
0.4.6 2026-04-13
0.4.4 2026-04-05
0.4.3 2025-08-16
0.4.2 2025-08-14
0.4.1 2024-03-27
0.4.0 2024-02-20
0.3.0 2024-02-19
0.2.5 2024-02-18
0.2.4 2024-01-26
0.2.3 2024-01-25
0.2.2 2024-01-03
0.2.1 2023-12-31
0.2.0 2023-12-30
0.1.1 2023-12-29
0.1.0 2023-12-29
0.0.1 2023-12-28
0.0.0 2023-12-27