Package Exports
- import-cleaner
- import-cleaner/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (import-cleaner) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Import Cleaner
A Node.js tool to scan your project and automatically remove unused import statements.
Installation
# Install globally
npm install -g import-cleaner
# Or locally in your project
npm install --save-dev import-cleaner
Usage
Command Line
# Clean current directory
import-cleaner
# Clean specific directory
import-cleaner ./src
# Dry run (preview without making changes)
import-cleaner --dry-run
# Show detailed information about unused imports
import-cleaner --verbose
# Exclude additional directories
import-cleaner --exclude "temp,output,cache"
# Combine options
import-cleaner ./src --dry-run --verbose
Programmatic Usage
const { cleanUnusedImports } = require("import-cleaner");
const stats = cleanUnusedImports("./src", {
extensions: [".js", ".jsx", ".ts", ".tsx"], // file extensions to scan
recursive: true, // scan subdirectories
dryRun: false, // set to true to preview without changes
exclude: ["node_modules", ".git", "dist"], // directories to exclude
verbose: true, // show detailed information
});
console.log(stats);
// {
// filesScanned: 25,
// filesModified: 8,
// totalImportsRemoved: 15,
// unusedImports: [
// {
// file: 'src/components/Button.js',
// imports: [
// { name: 'useState', source: 'react', isDefault: false },
// { name: 'classNames', source: 'classnames', isDefault: true }
// ]
// }
// ]
// }
Features
- Scans JavaScript and TypeScript files (
.js
,.jsx
,.ts
,.tsx
) - Identifies and removes unused import statements
- Keeps import statements that are actually used
- Works with ES6 named and default imports
- Recursive directory scanning
- Dry run mode to preview changes
- Verbose mode to show detailed information about unused imports
- Automatically excludes
node_modules
,.git
,dist
, andbuild
directories
How It Works
Import Cleaner analyzes your code using a two-pass approach:
- First pass: Collects all import statements and imported identifiers
- Second pass: Tracks which identifiers are actually used in the code
- Compares the two to identify unused imports
- Removes unused imports while preserving code structure
Limitations
- Currently doesn't detect imports used only in type annotations
- Doesn't track dynamic imports or requires
- May not correctly analyze code with advanced patterns or non-standard usage
License
MIT