@fe-fast/code-sweeper
Version:
A lightweight JavaScript/TypeScript code cleaning tool
307 lines (231 loc) โข 6.97 kB
Markdown
> Lightweight JavaScript/TypeScript code cleanup tool
[](https://www.npmjs.com/package/code-sweeper)
[](https://opensource.org/licenses/MIT)
[](https://www.typescriptlang.org/)
Code Sweeper is a tool focused on automatically cleaning redundant code in projects, filling the code cleanup gaps that ESLint and Prettier cannot cover.
- ๐ **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
```bash
npm install -g code-sweeper
npm install --save-dev code-sweeper
```
```bash
code-sweeper analyze
code-sweeper clean --dry-run
code-sweeper clean
code-sweeper config --init
```
For seamless integration into your build process, Code Sweeper provides official plugins for major build tools.
```bash
npm install @fe-fast/code-sweeper --save-dev
pnpm add -D @fe-fast/code-sweeper
yarn add -D @fe-fast/code-sweeper
```
```javascript
// 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',
},
}),
],
};
```
```typescript
// 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',
},
}),
],
});
```
```javascript
// rollup.config.js
import codeSweeperPlugin from '@fe-fast/code-sweeper/rollup';
export default {
// ... other configs
plugins: [
codeSweeperPlugin({
rules: {
removeConsoleLog: process.env.NODE_ENV === 'production',
},
}),
],
};
```
All plugins support the following options:
```typescript
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](./examples) directory.
Analyze code issues in the project without making any modifications.
```bash
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 format
```
### `clean` - Code Cleanup
Execute code cleanup operations.
```bash
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 prompts
```
### `config` - Configuration Management
Manage cleanup rule configurations.
```bash
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:
```json
{
"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
}
}
```
```bash
$ 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 first
```
```bash
$ 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
```
```bash
git clone https://github.com/william-xue/code-sweeper.git
cd code-sweeper
npm install
npm run build
npm test
npm run dev
```
Welcome to submit Issues and Pull Requests!
1. Fork this project
2. Create a feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## ๐ Acknowledgments
- [Babel](https://babeljs.io/) - AST parsing and transformation
- [TypeScript](https://www.typescriptlang.org/) - Type support
- [Commander.js](https://github.com/tj/commander.js/) - CLI framework
---
**Make code cleaner, make development more efficient!** ๐