@flownet/lib-parse-imports-js
v0.3.6
Published
## Introduction
Downloads
199
Readme
@flownet/lib-parse-imports-js
Introduction
The @flownet/lib-parse-imports-js
is a tool designed to analyze JavaScript and TypeScript files to determine their module import dependencies. This utility helps users understand which modules are imported, identifies unused imports, and distinguishes between local, npm, and Node.js built-in modules. It is useful for developers who want to manage and optimize their codebase by identifying unnecessary dependencies.
How It Works
The tool reads a specified JavaScript or TypeScript file and parses it to extract all import statements. It checks for both static imports and dynamic imports, as well as require()
calls. The tool then categorizes each import as local, npm, or built-in Node.js module. It can also recursively analyze dependencies, providing a comprehensive view of the module's dependencies.
Key Features
- Parses JavaScript and TypeScript files to list all module imports.
- Differentiates between local, npm, and built-in Node.js modules.
- Identifies unused or unreferenced imports in the code.
- Supports recursive analysis of dependencies.
- Outputs a detailed list of all, unreferenced, unnecessary, and required modules.
Conclusion
The @flownet/lib-parse-imports-js
tool provides developers with valuable insights into their project's module dependencies. By identifying and categorizing imports, it helps improve code maintenance by addressing unused dependencies, thus making the codebase cleaner and potentially improving performance.
Developer Guide for "@flownet/lib-parse-imports-js"
Overview
The "@flownet/lib-parse-imports-js" library is designed to assist developers in analyzing JavaScript files to extract and analyze import paths. Its primary function is to identify all external modules, both local and npm-based, used within a JavaScript project. The library provides insights into imported modules, categorizing them into necessary and unnecessary modules based on their usage within the codebase.
Installation
You can install the "@flownet/lib-parse-imports-js" library using either npm or yarn. Here's how you can do it:
Using npm:
npm install @flownet/lib-parse-imports-js
Using yarn:
yarn add @flownet/lib-parse-imports-js
Usage
Below is a basic example of how to use the library to parse and analyze import statements in a JavaScript file. The main entry point to the library is the index
function, which you can use as follows:
import parseImports from '@flownet/lib-parse-imports-js';
// Example usage
const analyzeImports = async () => {
const result = await parseImports({
file: '/path/to/your/file.js', // specify the path to the JavaScript file
recursive: true, // whether to analyze imports recursively
});
console.log('All Modules:', result.all);
console.log('Unreferenced:', result.unreferenced);
console.log('Unnecessary:', result.unnecessary);
console.log('Required:', result.required);
};
analyzeImports();
Examples
Here are some scenario-based examples that illustrate the core functionality of this library:
Analyzing a Single File: To analyze a single file without following imports recursively, set the
recursive
option tofalse
.parseImports({ file: '/path/to/entry-file.js', recursive: false, }).then(result => { console.log(result); });
Recursive Import Analysis: When you want to analyze your entire project, including all nested dependencies, set
recursive
totrue
.parseImports({ file: '/path/to/project/index.js', recursive: true, }).then(result => { console.log(result); });
Handling Node Built-in and Npm Modules: The library can distinguish between local, Node.js built-in, and npm modules, organizing them accordingly in the output.
parseImports({ file: './src/app.js', recursive: true, }).then(result => { console.log('Node Built-ins:', result.all.filter(mod => mod.type === 'node')); console.log('NPM Modules:', result.all.filter(mod => mod.type === 'npm')); });
Acknowledgement
This library leverages several open-source tools and libraries, notably Babel's parser
and traverse
modules, which are critical in parsing and walking through codebase to extract import statements efficiently. The contributors to these tools significantly aid in the development of functionalities provided by "@flownet/lib-parse-imports-js".
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
file:
type: string
description: The file path to be analyzed for module imports.
recursive:
type: boolean
default: false
description: Whether to recursively resolve all module imports in the specified file.
required:
- file