@tomatobybike/decomment
v0.1.6
Published
Safely remove comments from JavaScript, TypeScript, JSX, TSX, and Vue files using AST.
Maintainers
Readme
📦 decomment
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
- AST-based removal of single-line (
//) and block (/* */) comments - Preserves comments with specified prefixes (e.g.
eslint-,@license) - Supports JavaScript, MJS, JSX, TSX, and Vue (script section)
- Glob patterns for batch processing
- Optional configuration via
decomment.config.js - Dry-run mode to preview changes
- Statistics output (removed / kept comments)
- Automatic backup with full rollback support
- Cross-platform (Windows / macOS / Linux)
Installation
Global (recommended)
npm install -g @tomatobybike/decommentor
yarn global add @tomatobybike/decomment --ignore-enginesYarn v1 global install is not recommended due to legacy dependency constraints.
Local development
yarn add @tomatobybike/decommentUsage
# Remove comments
decomment [options] [files/globs]
# Restore files from backups
decomment restore
# Remove all backup files
decomment cleanIf 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, @preserveExamples
Remove comments from the current project (default behavior)
decommentEquivalent to:
decomment "**/*.{js,mjs,jsx,tsx,vue}"Automatically excludes:
node_modulesdistbuild.git
Preview changes (dry-run)
decomment "src/**/*.js" --dry-run --statsProcess multiple file types
decomment "src/**/*.{js,mjs,jsx,tsx,vue}" --statsPreserve specific comment prefixes
decomment "src/**/*.{js,ts}" --keep eslint-,@license --statsConfiguration (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
- CLI file/glob arguments
config.include- Default glob (
**/*.{js,mjs,jsx,tsx,vue})
Backup & Safety
For each processed file, decomment creates a single backup:
<file>.decomment.bak- The backup is overwritten on subsequent runs
- Designed for intentional, one-shot operations
- Guarantees full reversibility
Restore & Cleanup
Restore files from backup
decomment restoreRestores files from their .decomment.bak backups.
Remove all backup files
decomment cleanDeletes 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
- Regex-based tools do not understand syntax and can break code
- Build tools modify structure, formatting, and semantics
- decomment removes comments — and nothing else
If you care about safety, reviewability, and deterministic output, this tool is built for you.
🏆 When to Use decomment
- You want zero behavior changes
- You need auditable CI output
- You must preserve ESLint and license comments
- You work with modern JS / TS / Vue codebases
- You want safe and reversible operations
🚫 When NOT to Use decomment
- You just need quick text cleanup
- You already minify source code
- You do not care about accidental removals
npm Scripts Integration (Recommended)
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-commitExample .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
