console-sniper 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Bakioui Souhail
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,226 @@
1
+ # 🎯 console-sniper
2
+
3
+ > Remove `console.*` statements from JavaScript and TypeScript source code using **AST parsing** — no regex, no string hacks, no surprises.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/console-sniper)](https://www.npmjs.com/package/console-sniper)
6
+ [![license](https://img.shields.io/npm/l/console-sniper)](./LICENSE)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.x-blue)](https://www.typescriptlang.org/)
8
+
9
+ ---
10
+
11
+ ## Why console-sniper?
12
+
13
+ Most tools that remove console statements use **regular expressions** — which are brittle, break on multiline calls, and can corrupt your code.
14
+
15
+ `console-sniper` uses **Babel's AST parser** to:
16
+ - Understand your code's structure, not just its text
17
+ - Safely remove entire expression statements (including semicolons)
18
+ - Support JS, TS, JSX, TSX, ESM, and CJS
19
+ - Handle edge cases like `console["log"]()`, decorators, optional chaining, etc.
20
+
21
+ ---
22
+
23
+ ## Install
24
+
25
+ ```bash
26
+ npm install console-sniper
27
+ # or
28
+ pnpm add console-sniper
29
+ # or
30
+ yarn add console-sniper
31
+ ```
32
+
33
+ ---
34
+
35
+ ## Usage
36
+
37
+ ### 1. Vite Plugin
38
+
39
+ ```ts
40
+ // vite.config.ts
41
+ import { defineConfig } from "vite";
42
+ import consoleSniper from "console-sniper/vite";
43
+
44
+ export default defineConfig({
45
+ plugins: [
46
+ consoleSniper({
47
+ methods: ["log", "warn", "error", "info", "debug"],
48
+ removeComments: true,
49
+ productionOnly: true, // Only strip in production builds
50
+ }),
51
+ ],
52
+ });
53
+ ```
54
+
55
+ ### 2. CLI
56
+
57
+ ```bash
58
+ # Strip console.* from all files in src/
59
+ console-sniper src/
60
+
61
+ # Only remove specific methods
62
+ console-sniper src/ --methods log,warn
63
+
64
+ # Preview changes without modifying files
65
+ console-sniper src/ --dry-run
66
+
67
+ # Exclude patterns
68
+ console-sniper src/ --exclude "**/*.test.ts"
69
+
70
+ # Show all files (including unchanged)
71
+ console-sniper src/ --verbose
72
+ ```
73
+
74
+ ### 3. Programmatic API
75
+
76
+ ```ts
77
+ import { stripConsoleFromCode } from "console-sniper";
78
+
79
+ const sourceCode = `
80
+ console.log("Hello!");
81
+ const x = 1 + 2;
82
+ console.warn("watch out");
83
+ `;
84
+
85
+ const { code, removedCount, removedMethods, changed } = stripConsoleFromCode(
86
+ sourceCode,
87
+ {
88
+ methods: ["log", "warn"],
89
+ removeComments: true,
90
+ }
91
+ );
92
+
93
+ console.log(code); // → "const x = 1 + 2;"
94
+ console.log(removedCount); // → 2
95
+ console.log(changed); // → true
96
+ ```
97
+
98
+ ---
99
+
100
+ ## Options
101
+
102
+ ### `StripConsoleOptions`
103
+
104
+ | Option | Type | Default | Description |
105
+ |------------------|------------|------------------------------------|----------------------------------------------|
106
+ | `methods` | `string[]` | `["log","warn","error","info","debug"]` | Which console methods to remove |
107
+ | `removeComments` | `boolean` | `true` | Remove comments that reference `console` |
108
+ | `include` | `RegExp[]` | `[]` | File path patterns to include (plugin/CLI) |
109
+ | `exclude` | `RegExp[]` | `[]` | File path patterns to exclude (plugin/CLI) |
110
+ | `silent` | `boolean` | `false` | Suppress all logging output |
111
+
112
+ ### Vite-specific options (`VitePluginOptions`)
113
+
114
+ | Option | Type | Default | Description |
115
+ |-----------------|-----------|---------|-----------------------------------------------------|
116
+ | `productionOnly`| `boolean` | `true` | Only strip during production builds (`vite build`) |
117
+
118
+ ---
119
+
120
+ ## CLI Reference
121
+
122
+ ```
123
+ Usage: console-sniper [targets...] [options]
124
+
125
+ Arguments:
126
+ targets Files or directories to process (default: "src")
127
+
128
+ Options:
129
+ -v, --version Print version
130
+ -m, --methods <list> Comma-separated methods to remove (default: log,warn,error,info,debug)
131
+ --no-comments Keep console-related comments
132
+ -e, --exclude <glob> Exclude patterns (repeatable)
133
+ -d, --dry-run Preview without modifying files
134
+ -s, --silent Suppress all output
135
+ --verbose Show details for unchanged files too
136
+ --no-banner Skip the ASCII banner
137
+ -h, --help Show help
138
+ ```
139
+
140
+ ---
141
+
142
+ ## How It Works
143
+
144
+ ```
145
+ Source Code
146
+
147
+
148
+ @babel/parser → AST
149
+
150
+
151
+ @babel/traverse (finds console.* ExpressionStatements)
152
+
153
+
154
+ nodePath.remove() (safely removes matched nodes)
155
+
156
+
157
+ Comment filtering (removes "console" comments from all nodes)
158
+
159
+
160
+ @babel/generator → Transformed Source Code
161
+ ```
162
+
163
+ ---
164
+
165
+ ## Architecture
166
+
167
+ ```
168
+ console-sniper/
169
+ ├── src/
170
+ │ ├── core/ ← Pure AST engine (no Vite, no CLI, no FS)
171
+ │ │ ├── stripConsole.ts ← The main strip function
172
+ │ │ ├── types.ts ← All TypeScript types
173
+ │ │ ├── constants.ts ← Default values
174
+ │ │ └── utils.ts ← Pure helpers
175
+ │ │
176
+ │ ├── vite/ ← Vite plugin (thin wrapper over core)
177
+ │ │ └── vitePlugin.ts
178
+ │ │
179
+ │ ├── cli/ ← CLI tool (commander + file I/O)
180
+ │ │ ├── cli.ts
181
+ │ │ ├── fileScanner.ts
182
+ │ │ └── logger.ts
183
+ │ │
184
+ │ └── shared/ ← Shared UI (banner, etc.)
185
+ │ └── banner.ts
186
+ ```
187
+
188
+ The core engine is **intentionally isolated** — it has no dependency on Vite, Node.js FS, or the CLI. This makes it easy to add:
189
+
190
+ - Rollup plugin
191
+ - Webpack plugin
192
+ - esbuild plugin
193
+ - Bun plugin
194
+
195
+ ---
196
+
197
+ ## Supported Syntax
198
+
199
+ - ✅ JavaScript (`.js`, `.mjs`, `.cjs`)
200
+ - ✅ TypeScript (`.ts`, `.mts`, `.cts`)
201
+ - ✅ JSX (`.jsx`)
202
+ - ✅ TSX (`.tsx`)
203
+ - ✅ ESM (`import`/`export`)
204
+ - ✅ CommonJS (`require`/`module.exports`)
205
+ - ✅ Decorators (`@Injectable()`)
206
+ - ✅ Optional chaining (`?.`)
207
+ - ✅ Nullish coalescing (`??`)
208
+ - ✅ Top-level `await`
209
+ - ✅ Bracket notation (`console["log"]()`)
210
+
211
+ ---
212
+
213
+ ## Contributing
214
+
215
+ 1. Fork the repo
216
+ 2. `npm install`
217
+ 3. `npm test` — make sure tests pass
218
+ 4. Make your changes
219
+ 5. `npm test` again
220
+ 6. Open a PR!
221
+
222
+ ---
223
+
224
+ ## License
225
+
226
+ [MIT](./LICENSE) © Bakioui Souhail