@fe-fast/code-sweeper
v1.0.1
Published
A lightweight JavaScript/TypeScript code cleaning tool
Maintainers
Readme
🧹 Code Sweeper
Lightweight JavaScript/TypeScript code cleanup tool
Code Sweeper is a tool focused on automatically cleaning redundant code in projects, filling the code cleanup gaps that ESLint and Prettier cannot cover.
✨ Features
- 🔍 Smart Analysis: AST-based static analysis for precise identification of unused code
- 🧹 One-click Cleanup: Remove unused imports, variables, and functions
- 🐛 Debug Cleanup: Automatically remove console.log and debugger statements
- ⚙️ Flexible Configuration: Support custom cleanup rules and file filtering
- 🚀 Multi-framework Support: Compatible with Vue, React, and TypeScript projects
- 📊 Detailed Reports: Provide before-and-after cleanup analysis
- 🔌 Build Tool Integration: Support for Webpack, Vite, and Rollup plugins
🚀 Quick Start
Installation
# Global installation
npm install -g code-sweeper
# Or install in project
npm install --save-dev code-sweeperBasic Usage (CLI)
# Analyze code issues
code-sweeper analyze
# Clean code (preview mode)
code-sweeper clean --dry-run
# Execute cleanup
code-sweeper clean
# Initialize configuration file
code-sweeper config --init🔌 Build Tool Integration
For seamless integration into your build process, Code Sweeper provides official plugins for major build tools.
Installation
npm install @fe-fast/code-sweeper --save-dev
# or
pnpm add -D @fe-fast/code-sweeper
# or
yarn add -D @fe-fast/code-sweeperWebpack
// webpack.config.js
const { CodeSweeperWebpackPlugin } = require('@fe-fast/code-sweeper/webpack');
module.exports = {
// ... other configs
plugins: [
new CodeSweeperWebpackPlugin({
// options
rules: {
removeConsoleLog: process.env.NODE_ENV === 'production',
},
}),
],
};Vite
// vite.config.ts
import { defineConfig } from 'vite';
import codeSweeperPlugin from '@fe-fast/code-sweeper/vite';
export default defineConfig({
plugins: [
codeSweeperPlugin({
rules: {
removeConsoleLog: process.env.NODE_ENV === 'production',
},
}),
],
});Rollup
// rollup.config.js
import codeSweeperPlugin from '@fe-fast/code-sweeper/rollup';
export default {
// ... other configs
plugins: [
codeSweeperPlugin({
rules: {
removeConsoleLog: process.env.NODE_ENV === 'production',
},
}),
],
};Plugin Options
All plugins support the following options:
interface PluginOptions {
include?: string[];
exclude?: string[];
dryRun?: boolean;
skipConfirmation?: boolean;
rules?: {
removeUnusedImports?: boolean;
removeUnusedVariables?: boolean;
removeConsoleLog?: boolean;
removeDebugger?: boolean;
formatCode?: boolean;
renameToCamelCase?: boolean;
};
}For more examples, check out the examples directory.
📋 Command Reference
analyze - Code Analysis
Analyze code issues in the project without making any modifications.
code-sweeper analyze [options]
Options:
-p, --path <path> Target directory path (default: current directory)
-c, --config <file> Configuration file path
--json Output results in JSON formatclean - Code Cleanup
Execute code cleanup operations.
code-sweeper clean [options]
Options:
-p, --path <path> Target directory path (default: current directory)
-c, --config <file> Configuration file path
--dry-run Preview mode, don't actually modify files
--imports Only clean unused imports
--variables Only clean unused variables
--console Only remove console statements
--debugger Only remove debugger statements
-y, --yes Skip confirmation promptsconfig - Configuration Management
Manage cleanup rule configurations.
code-sweeper config [options]
Options:
--init Initialize configuration file
--show Show current configuration⚙️ Configuration File
Create a .code-sweeper.json file in your project root:
{
"rules": {
"removeUnusedImports": true,
"removeUnusedVariables": true,
"removeConsoleLog": true,
"removeDebugger": true,
"formatCode": false,
"renameToCamelCase": false
},
"include": [
"src/**/*.{js,ts,jsx,tsx}",
"components/**/*.{js,ts,jsx,tsx}"
],
"exclude": [
"node_modules/**",
"dist/**",
"build/**",
"*.min.js"
],
"parser": {
"typescript": true,
"jsx": true,
"decorators": true,
"classProperties": true
}
}📊 Usage Examples
Analysis Result Example
$ code-sweeper analyze
🔍 Code Sweeper - Analyzing your code...
📁 Target path: /Users/project/src
📊 Code Analysis Report
══════════════════════════════════════════════════
📈 Summary:
• Total files scanned: 45
• Files with issues: 12
• Total issues found: 28
🔍 Issue Breakdown:
• Unused imports: 15
• Unused variables: 8
• Console statements: 3
• Debugger statements: 2
💡 Recommendations:
• Run code-sweeper clean to fix these issues
• Use --dry-run flag to preview changes firstCleanup Result Example
$ code-sweeper clean
🧹 Code Sweeper - Cleaning your code...
✅ Cleaning completed!
📊 Summary:
• Files modified: 8
• Issues fixed: 23
• Lines removed: 45
• Estimated size reduction: ~2.1KB🛠️ Development
# Clone the project
git clone https://github.com/william-xue/code-sweeper.git
cd code-sweeper
# Install dependencies
npm install
# Build project
npm run build
# Run tests
npm test
# Local development
npm run dev🤝 Contributing
Welcome to submit Issues and Pull Requests!
- Fork this project
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Babel - AST parsing and transformation
- TypeScript - Type support
- Commander.js - CLI framework
Make code cleaner, make development more efficient! 🚀
