Skip to content

KID-joker/unplugin-drop-committed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

unplugin-drop-committed

npm version License

πŸ”Œ An unplugin that automatically removes committed code (like console.log) from your codebase based on Git history.

Features

✨ 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

Installation

npm install unplugin-drop-committed --save-dev

Usage

Vite

// vite.config.ts
import DropCommitted from 'unplugin-drop-committed/vite'

export default defineConfig({
  plugins: [
    DropCommitted({
      mode: 'strict',
      removeMethods: ['console.log'],
    }),
  ],
})

Webpack

// webpack.config.js
module.exports = {
  plugins: [
    require('unplugin-drop-committed/webpack')({
      mode: 'strict',
      removeMethods: ['console.log'],
    }),
  ],
}

Rollup

// rollup.config.js
import DropCommitted from 'unplugin-drop-committed/rollup'

export default {
  plugins: [
    DropCommitted({
      mode: 'strict',
      removeMethods: ['console.log'],
    }),
  ],
}

Configuration

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
}

Removal Modes

Strict Mode (Default)

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') // ❌ Kept

File Mode

Removes 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.

User Mode

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.

Time Mode

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'

Advanced Usage

Custom Method Names

Remove any method calls, including nested ones:

DropCommitted({
  removeMethods: [
    'console.log',
    'console.debug',
    'logger.info',
    'debug.trace',
  ],
})

File Patterns

Customize which files to process:

DropCommitted({
  include: [/\.[jt]sx?$/, /\.vue$/],
  exclude: ['**/*.test.js', '**/*.spec.ts'],
})

Vue and Svelte Support

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>

How It Works

  1. Environment Check: Only runs in development mode
  2. Git Integration: Uses Git blame and status to determine removal
  3. AST Parsing: Uses Babel to accurately find method calls
  4. Smart Replacement: Replaces calls with (() => {}) to maintain code structure
  5. Caching: Caches Git operations for better performance

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

Requirements

  • Node.js >= 18
  • Git repository
  • Development environment (automatically skipped in production)

Examples

Remove old debug logs

DropCommitted({
  mode: 'time',
  expiration: '90d',
  removeMethods: ['console.log', 'console.debug'],
})

Clean up team logs

DropCommitted({
  mode: 'user',
  removeMethods: ['console.log', 'debugger'],
})

Production-ready files only

DropCommitted({
  mode: 'file',
  removeMethods: ['console.log', 'console.warn'],
})

License

MIT License Β© 2024 KID-joker

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Related

  • unplugin - Unified plugin system for build tools

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published