π An unplugin that automatically removes committed code (like console.log) from your codebase based on Git history.
β¨ Multiple Removal Modes: Choose how to identify code to remove
- π― Strict Mode: Remove code on committed lines
- π File Mode: Remove code from committed files
- π€ User Mode: Remove code from other authors
- β° Time Mode: Remove code older than a specified time
π Framework Agnostic: Works with Vite, Webpack, Rollup, Rspack, and more
π¨ File Type Support: JavaScript, TypeScript, JSX, Vue, and Svelte
β‘ Performance Optimized: Git operation caching and incremental processing
π‘οΈ Dev Only: Automatically disabled in production builds
npm install unplugin-drop-committed --save-dev// vite.config.ts
import DropCommitted from 'unplugin-drop-committed/vite'
export default defineConfig({
plugins: [
DropCommitted({
mode: 'strict',
removeMethods: ['console.log'],
}),
],
})// webpack.config.js
module.exports = {
plugins: [
require('unplugin-drop-committed/webpack')({
mode: 'strict',
removeMethods: ['console.log'],
}),
],
}// rollup.config.js
import DropCommitted from 'unplugin-drop-committed/rollup'
export default {
plugins: [
DropCommitted({
mode: 'strict',
removeMethods: ['console.log'],
}),
],
}interface Options {
/**
* Removal mode
* @default 'strict'
*/
mode?: 'strict' | 'file' | 'user' | 'time'
/**
* Method names to remove (supports dot notation)
* @default ['console.log']
*/
removeMethods?: string[]
/**
* File inclusion patterns
* @default [/\.[jt]sx?$/, /\.vue$/, /\.vue\?vue/, /\.svelte$/]
*/
include?: (string | RegExp)[]
/**
* File exclusion patterns
* @default []
*/
exclude?: (string | RegExp)[]
/**
* Expiration time for 'time' mode
* Supports: ISO dates, relative time (e.g., '30d', '1y', '6M')
*/
expiration?: string
}Removes method calls on lines that have been committed to Git.
DropCommitted({
mode: 'strict',
removeMethods: ['console.log'],
})Example:
// If this line is committed:
console.log('This will be removed') // β
Removed
// If this line is uncommitted:
console.log('This will stay') // β KeptRemoves all method calls from files that are fully committed (not modified, added, or deleted).
DropCommitted({
mode: 'file',
})Use case: Clean up debug logs from stable files while keeping them in files you're actively working on.
Removes method calls authored by other developers (based on Git blame).
DropCommitted({
mode: 'user',
})Use case: Remove debug logs from other team members while keeping your own.
Removes method calls older than a specified time.
DropCommitted({
mode: 'time',
expiration: '30d', // Remove logs older than 30 days
})Supported formats:
- ISO dates:
'2024-01-01' - Relative days:
'30d' - Relative months:
'6M' - Relative years:
'1y'
Remove any method calls, including nested ones:
DropCommitted({
removeMethods: [
'console.log',
'console.debug',
'logger.info',
'debug.trace',
],
})Customize which files to process:
DropCommitted({
include: [/\.[jt]sx?$/, /\.vue$/],
exclude: ['**/*.test.js', '**/*.spec.ts'],
})The plugin automatically extracts and processes <script> blocks:
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
mounted() {
console.log('Component mounted') // Will be removed if committed
}
}
</script>- Environment Check: Only runs in development mode
- Git Integration: Uses Git blame and status to determine removal
- AST Parsing: Uses Babel to accurately find method calls
- Smart Replacement: Replaces calls with
(() => {})to maintain code structure - Caching: Caches Git operations for better performance
The plugin implements several optimizations:
- β Git operation results are cached
- β File modification time tracking for cache invalidation
- β Incremental processing of changed files
- β Automatic skip in production builds
- Node.js >= 18
- Git repository
- Development environment (automatically skipped in production)
DropCommitted({
mode: 'time',
expiration: '90d',
removeMethods: ['console.log', 'console.debug'],
})DropCommitted({
mode: 'user',
removeMethods: ['console.log', 'debugger'],
})DropCommitted({
mode: 'file',
removeMethods: ['console.log', 'console.warn'],
})MIT License Β© 2024 KID-joker
Contributions are welcome! Please feel free to submit a Pull Request.
- unplugin - Unified plugin system for build tools