npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

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.

Readme

🎯 console-sniper

Remove console.* statements from JavaScript and TypeScript source code using AST parsing — no regex, no string hacks, no surprises.

npm version license TypeScript


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-sniper

Usage

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/ --verbose

3. 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);      // → true

Options

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 help

How 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 Code

Architecture

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

The 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

  1. Fork the repo
  2. npm install
  3. npm test — make sure tests pass
  4. Make your changes
  5. npm test again
  6. Open a PR!

License

MIT © Bakioui Souhail