import-unifier
v1.0.2
Published
Generic import consolidator for TypeScript projects. Merge imports from a source path into a target file.
Maintainers
Readme
import-unifier
Generic Import Consolidator for TypeScript Projects
import-unifier helps streamline imports by consolidating them from a source path into a single target file. It works seamlessly with any TypeScript project, keeping your imports clean, consistent, and maintainable.
✨ Features
- 🔄 Consolidates imports from any source path into one target file
- 🔗 Merges named and default imports automatically
- 📁 Creates the target file automatically if it doesn't exist
- 🎯 Supports TypeScript (.ts) and TSX (.tsx) files
- 🛠️ Provides both CLI and programmatic usage
- 📊 Logs processed files for easy tracking
- 🧹 Improves codebase maintainability by avoiding duplicate imports and clutter
📦 Installation
# Using npm
npm install import-unifier --save-dev
# Using yarn
yarn add import-unifier --dev🚀 Usage
Programmatic Usage
import { consolidateImports } from "import-unifier";
consolidateImports({
tsConfigPath: "tsconfig.json", // optional (default: tsconfig.json)
sourceGlob: "src/**/*.tsx", // optional (default: src/**/*.ts{,x})
sourcePath: "src/components/", // required: folder to consolidate from
targetPath: "src/shared/components/index", // required: target file path
log: true // optional (default: true)
});How it works:
- Scans all files under
sourceGlob - Collects imports from
sourcePath - Rewrites imports to point to
targetPath - Merges imports into the target file (
index.ts)
CLI Usage
npx consolidate-imports <tsConfigPath?> <sourcePath> <targetPath>Arguments:
tsConfigPath(optional): Path to your TypeScript config (default:tsconfig.json)sourcePath(required): Folder or module path to consolidate imports fromtargetPath(required): File path where consolidated imports will be merged
Example:
npx consolidate-imports tsconfig.json src/components/ src/shared/components/index📋 Example
Before consolidation:
// src/pages/Home.tsx
import { Button } from "src/components/Button";
import { Card } from "src/components/Card";
import { Modal } from "src/components/Modal";
// src/pages/Dashboard.tsx
import { Button } from "src/components/Button";
import { Table } from "src/components/Table";After running import-unifier:
// src/pages/Home.tsx
import { Button, Card, Modal } from "src/shared/components/index";
// src/pages/Dashboard.tsx
import { Button, Table } from "src/shared/components/index";
// src/shared/components/index.ts (auto-generated)
export { Button } from "../../components/Button";
export { Card } from "../../components/Card";
export { Modal } from "../../components/Modal";
export { Table } from "../../components/Table";All imports from the source folder are merged into a single import pointing to the target file, making your codebase cleaner and more maintainable.
🎯 Use Cases
- Component Libraries: Consolidate component imports into a single index file
- Utility Functions: Merge utility imports from scattered files
- API Services: Centralize API service imports
- Constants: Unify constant imports across your project
- Types: Consolidate TypeScript type imports
⚙️ Configuration Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| tsConfigPath | string | "tsconfig.json" | Path to TypeScript configuration file |
| sourceGlob | string | "src/**/*.ts{,x}" | Glob pattern to match files for processing |
| sourcePath | string | Required | Source folder/path to consolidate imports from |
| targetPath | string | Required | Target file path for consolidated imports |
| log | boolean | true | Enable logging of processed files |
📝 License
MIT License © [Your Name]
🔧 Requirements
- Node.js >= 14.0.0
- TypeScript >= 4.0.0
Made with ❤️ for cleaner TypeScript codebases
