Get the dependencies specifier of an es6 module
npm install detective-module> Get the dependencies specifier of an ES6 module and CommonJS require() - Powered by oxc-parser for blazing fast performance โก
npm install detective-module
- ๐ High Performance: Built with oxc-parser (Rust-based) for 3-5x faster parsing
- โก Modern Testing: Uses Vitest for blazing fast test execution and development experience
- ๐ฆ Zero Configuration: Works out of the box with intelligent file type detection
- ๐ฏ Full Language Support: JavaScript, TypeScript, JSX, TSX, and latest ECMAScript features
- ๐ Dual Mode: Supports both ES6 modules (import/export) and CommonJS (require())
- ๐ชถ Lightweight: Minimal dependencies with optimized bundle size
``js
const {
detectiveModuleAndRequire,
detectiveModule,
} = require("detective-module");
const mySourceCode = fs.readFileSync("myfile.js", "utf8");
// Pass in a file's content or an AST
const dependencies = detectiveModule(mySourceCode);
// Input:
// import Abc, * as BBBBBB from "mylib";
// Output:
[
{
name: "mylib",
default: "Abc",
star: true,
alias: "BBBBBB",
},
];
`
#### ES6 Modules with Named Imports
`js
// Input:
// import { foo as Foo, bar } from "mylib";
// Output:
[
{
name: "mylib",
members: [
{
name: "foo",
alias: "Foo",
},
{
name: "bar",
alias: "bar",
},
],
},
];
`
#### TypeScript + React (TSX)
`js
// Input:
// import React, { Component } from 'react';
// import { fetchUser } from '@/services/api';
//
// interface Props {
// theme: string;
// }
//
// export default class App extends Component
// render() {
// return Hello World;
// }
// }
// Output:
[
{
name: "react",
default: "React",
members: [
{
name: "Component",
alias: "Component",
},
],
},
{
name: "@/services/api",
members: [
{
name: "fetchUser",
alias: "fetchUser",
},
],
},
];
`
#### CommonJS with Destructuring
`js
// Use detectiveModuleAndRequire for both ES6 and CommonJS
const dependencies = detectiveModuleAndRequire(sourceCode);
// Input:
// const { default: React, useState } = require('react');
// Output:
[
{
name: "react",
default: "React",
members: [
{
name: "useState",
alias: "useState",
},
],
},
];
`
Thanks to modern tooling (oxc-parser + Vitest), detective-module now offers:
Parsing Performance:
- โก 3-5x faster than previous Babel-based implementations
- ๐ฏ ~45,000+ operations/second on modern hardware
- ๐ฆ Smaller bundle size with fewer dependencies
- ๐ก๏ธ Better error recovery and parsing reliability
Development Experience:
- ๐งช 2-5x faster test execution with Vitest vs Jest
- โก Instant hot reload in watch mode (npm run test:watch)
- ๐ฅ Native ES modules support without configuration
- ๐ Better error reporting and debugging experience
#### detectiveModule(code, options?)
Extracts ES6 import/export dependencies from source code.
- code: Source code string or pre-parsed AST objectoptions.filename
- : Override file extension for parser context (optional)
#### detectiveModuleAndRequire(code, options?)
Extracts both ES6 imports/exports and CommonJS require() dependencies.
- code: Source code string or pre-parsed AST objectoptions.filename
- : Override file extension for parser context (optional)
- โ
JavaScript (ES5+, ESNext)
- โ
TypeScript (including latest features)
- โ
JSX and TSX
- โ
ES6 Modules (import/export)require()
- โ
CommonJS (/module.exports)import()
- โ
Dynamic imports ()
- โ
Stage 3+ proposals and decorators
Version 4.0+ includes major improvements:
- Parser: Uses oxc-parser instead of node-source-walk for 3-5x better performance
- Testing: Migrated from Jest to Vitest for faster development and CI/CD
- API: Remains 100% compatible - no code changes needed for existing users
For Contributors:
- npm test - Run tests oncenpm run test:watch` - Run tests in watch mode for development
-
#### License
MIT