console-sniper
v1.0.0
Published
Remove console.* statements from JS/TS source code using AST parsing. Works as a Vite plugin and standalone CLI.
Maintainers
Readme
🎯 console-sniper
Remove
console.*statements from JavaScript and TypeScript source code using AST parsing — no regex, no string hacks, no surprises.
Why console-sniper?
Most tools that remove console statements use regular expressions — which are brittle, break on multiline calls, and can corrupt your code.
console-sniper uses Babel's AST parser to:
- Understand your code's structure, not just its text
- Safely remove entire expression statements (including semicolons)
- Support JS, TS, JSX, TSX, ESM, and CJS
- Handle edge cases like
console["log"](), decorators, optional chaining, etc.
Install
npm install console-sniper
# or
pnpm add console-sniper
# or
yarn add console-sniperUsage
1. Vite Plugin
// vite.config.ts
import { defineConfig } from "vite";
import consoleSniper from "console-sniper/vite";
export default defineConfig({
plugins: [
consoleSniper({
methods: ["log", "warn", "error", "info", "debug"],
removeComments: true,
productionOnly: true, // Only strip in production builds
}),
],
});2. CLI
# Strip console.* from all files in src/
console-sniper src/
# Only remove specific methods
console-sniper src/ --methods log,warn
# Preview changes without modifying files
console-sniper src/ --dry-run
# Exclude patterns
console-sniper src/ --exclude "**/*.test.ts"
# Show all files (including unchanged)
console-sniper src/ --verbose3. Programmatic API
import { stripConsoleFromCode } from "console-sniper";
const sourceCode = `
console.log("Hello!");
const x = 1 + 2;
console.warn("watch out");
`;
const { code, removedCount, removedMethods, changed } = stripConsoleFromCode(
sourceCode,
{
methods: ["log", "warn"],
removeComments: true,
}
);
console.log(code); // → "const x = 1 + 2;"
console.log(removedCount); // → 2
console.log(changed); // → trueOptions
StripConsoleOptions
| Option | Type | Default | Description |
|------------------|------------|------------------------------------|----------------------------------------------|
| methods | string[] | ["log","warn","error","info","debug"] | Which console methods to remove |
| removeComments | boolean | true | Remove comments that reference console |
| include | RegExp[] | [] | File path patterns to include (plugin/CLI) |
| exclude | RegExp[] | [] | File path patterns to exclude (plugin/CLI) |
| silent | boolean | false | Suppress all logging output |
Vite-specific options (VitePluginOptions)
| Option | Type | Default | Description |
|-----------------|-----------|---------|-----------------------------------------------------|
| productionOnly| boolean | true | Only strip during production builds (vite build) |
CLI Reference
Usage: console-sniper [targets...] [options]
Arguments:
targets Files or directories to process (default: "src")
Options:
-v, --version Print version
-m, --methods <list> Comma-separated methods to remove (default: log,warn,error,info,debug)
--no-comments Keep console-related comments
-e, --exclude <glob> Exclude patterns (repeatable)
-d, --dry-run Preview without modifying files
-s, --silent Suppress all output
--verbose Show details for unchanged files too
--no-banner Skip the ASCII banner
-h, --help Show helpHow It Works
Source Code
│
▼
@babel/parser → AST
│
▼
@babel/traverse (finds console.* ExpressionStatements)
│
▼
nodePath.remove() (safely removes matched nodes)
│
▼
Comment filtering (removes "console" comments from all nodes)
│
▼
@babel/generator → Transformed Source CodeArchitecture
console-sniper/
├── src/
│ ├── core/ ← Pure AST engine (no Vite, no CLI, no FS)
│ │ ├── stripConsole.ts ← The main strip function
│ │ ├── types.ts ← All TypeScript types
│ │ ├── constants.ts ← Default values
│ │ └── utils.ts ← Pure helpers
│ │
│ ├── vite/ ← Vite plugin (thin wrapper over core)
│ │ └── vitePlugin.ts
│ │
│ ├── cli/ ← CLI tool (commander + file I/O)
│ │ ├── cli.ts
│ │ ├── fileScanner.ts
│ │ └── logger.ts
│ │
│ └── shared/ ← Shared UI (banner, etc.)
│ └── banner.tsThe core engine is intentionally isolated — it has no dependency on Vite, Node.js FS, or the CLI. This makes it easy to add:
- Rollup plugin
- Webpack plugin
- esbuild plugin
- Bun plugin
Supported Syntax
- ✅ JavaScript (
.js,.mjs,.cjs) - ✅ TypeScript (
.ts,.mts,.cts) - ✅ JSX (
.jsx) - ✅ TSX (
.tsx) - ✅ ESM (
import/export) - ✅ CommonJS (
require/module.exports) - ✅ Decorators (
@Injectable()) - ✅ Optional chaining (
?.) - ✅ Nullish coalescing (
??) - ✅ Top-level
await - ✅ Bracket notation (
console["log"]())
Contributing
- Fork the repo
npm installnpm test— make sure tests pass- Make your changes
npm testagain- Open a PR!
License
MIT © Bakioui Souhail
