decomment

πŸ“¦ decomment

npm version downloads license GitHub stars issues

decomment is a CLI tool to safely remove comments from JavaScript, TypeScript, JSX, TSX, and Vue files.

It performs AST-based comment removal to ensure code behavior is never changed. The tool is designed for production codebases, CI pipelines, and teams that care about safety, auditability, and reversibility.

Remove comments β€” and nothing else.


Features


Installation

npm install -g @tomatobybike/decomment

or

yarn global add @tomatobybike/decomment --ignore-engines

Yarn v1 global install is not recommended due to legacy dependency constraints.

Local development

yarn add @tomatobybike/decomment

Usage

# Remove comments
decomment [options] [files/globs]

# Restore files from backups
decomment restore

# Remove all backup files
decomment clean

If no files or globs are provided, decomment scans the current directory by default.


Options

Option Description
--dry-run Preview changes without writing files
--keep Comma-separated comment prefixes to preserve
--stats Show number of comments removed and kept
-h, --help Show help message

Default preserved prefixes:

eslint-, @license, @preserve

Examples

Remove comments from the current project (default behavior)

decomment

Equivalent to:

decomment "**/*.{js,mjs,jsx,tsx,vue}"

Automatically excludes:


Preview changes (dry-run)

decomment "src/**/*.js" --dry-run --stats

Process multiple file types

decomment "src/**/*.{js,mjs,jsx,tsx,vue}" --stats

Preserve specific comment prefixes

decomment "src/**/*.{js,ts}" --keep eslint-,@license --stats

Configuration (decomment.config.js)

An optional configuration file can be placed at the project root.

export default {
  include: ["src/**/*.{js,mjs,jsx,tsx,vue}", "test/**/*.{js,mjs,jsx,tsx,vue}"],
  keep: ["eslint-", "@license", "@preserve"],
};

Config precedence

  1. CLI file/glob arguments
  2. config.include
  3. Default glob (**/*.{js,mjs,jsx,tsx,vue})

Backup & Safety

For each processed file, decomment creates a single backup:

<file>.decomment.bak

Restore & Cleanup

Restore files from backup

decomment restore

Restores files from their .decomment.bak backups.


Remove all backup files

decomment clean

Deletes all .decomment.bak files in the matched scope.


πŸ” Comparison with Other Tools

Feature / Tool decomment strip-comments comment-strip Babel / esbuild / Terser
AST-based (safe) βœ… Yes ❌ Regex-based ❌ Regex-based βœ… Yes
Preserves semantics βœ… Guaranteed ❌ Risky ❌ Risky ❌ Not guaranteed
Removes only comments βœ… Yes ⚠️ Mostly ⚠️ Mostly ❌ No (build output)
JSX / TSX support βœ… Yes ⚠️ Partial ⚠️ Partial βœ… Yes
Vue SFC support βœ… Script section only ❌ No ❌ No ⚠️ Limited
Preserve prefixes βœ… Yes (--keep) ❌ No ❌ No ⚠️ Limited
Dry-run mode βœ… Yes ❌ No ❌ No ❌ No
Statistics output βœ… Yes ❌ No ❌ No ❌ No
Backup & rollback βœ… Yes ❌ No ❌ No ❌ No
Glob batch processing βœ… Yes ⚠️ Limited ⚠️ Limited ⚠️ Build-specific
Config file support βœ… Yes ❌ No ❌ No ❌ No
CI / pre-commit ready βœ… Designed for it ❌ No ❌ No ⚠️ Not intended
Risk of breaking code Very Low Medium Medium High
Primary use case Production codebase hygiene Small scripts Small scripts Build & minification

🧠 Why decomment Exists

If you care about safety, reviewability, and deterministic output, this tool is built for you.


πŸ† When to Use decomment


🚫 When NOT to Use decomment


decomment is designed to work naturally with npm scripts for safe and repeatable workflows.

{
  "scripts": {
    "decomment": "decomment",
    "decomment:check": "decomment --dry-run --stats",
    "decomment:fix": "decomment --stats",
    "decomment:restore": "decomment restore",
    "decomment:clean": "decomment clean"
  }
}

Common workflows

# Preview comment removal (no file changes)
npm run decomment:check

# Remove comments and create backups
npm run decomment:fix

# Restore all files from backups
npm run decomment:restore

# Remove all .decomment.bak files
npm run decomment:clean


πŸͺ Pre-commit Hook (Husky)(Optional / AdvancedοΌ‰

Pre-commit Hook (Husky) – Optional

You can integrate decomment into a pre-commit workflow to automatically remove comments before each commit.

⚠️ This is optional and recommended only for teams that already use Husky.

Setup

npm install -D husky
npx husky install
npx husky add .husky/pre-commit

Example .husky/pre-commit

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

echo "🧹 Running decomment..."

npx decomment "src/**/*.{js,mjs,jsx,tsx,vue}" \
  --keep eslint-,@license,@preserve \
  --stats

License

MIT License