Skip to content

Commit 4a12cf3

Browse files
authored
Merge pull request #16 from willybrauner/prevent-drop-on-compilation-3
Bypass dropping of console.log from the build process
2 parents e9eb08e + fd986db commit 4a12cf3

6 files changed

Lines changed: 22 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Tiny debug tool (~500 bytes) for terminal and browser inspired by [debug-js/debu
1313
## Motivation
1414

1515
`@wbe/debug` was built in order to be as light as possible for terminal and browser,
16-
as the same way as the great visionmedia/debug tool.
16+
as the same way as the great debug-js/debug tool.
1717

1818
## Installation
1919

examples/browser/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ console.log("isProd", isProd)
66
export default defineConfig({
77
esbuild: {
88
pure: isProd ? ["console.log"] : [],
9-
//drop: isProd ? ["console"] : [],
9+
//drop: ["console"],
1010
},
1111
})

examples/node/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const debug = require("@wbe/debug")
1+
import debug from "@wbe/debug"
22
const str = "hello debug!"
33

44
debug("01-namespace")(str)

examples/node/package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
{
22
"name": "node",
3-
"version": "1.0.0",
4-
"description": "",
53
"main": "index.js",
4+
"type": "module",
65
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
6+
"test": "echo \"Error: no test specified\" && exit 1",
7+
"start": "node index.js"
88
},
9-
"keywords": [],
10-
"author": "",
11-
"license": "ISC",
129
"dependencies": {
1310
"@wbe/debug": "workspace:*"
1411
}

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{
22
"name": "@wbe/debug",
33
"version": "1.2.0",
4-
"description": "Tiny debug tool (~500 bytes) for terminal and browser inspired by visionmedia/debug API.",
4+
"description": "Tiny debug tool (~500 bytes) for terminal and browser inspired by debug-js/debug API.",
55
"author": "Willy Brauner",
66
"license": "MIT",
77
"type": "module",
8-
"main": "./dist/index.cjs",
9-
"module": "./dist/index.js",
10-
"types": "./dist/index.d.ts",
8+
"exports": {
9+
".": {
10+
"types": "./dist/index.d.ts",
11+
"import": "./dist/index.js"
12+
}
13+
},
1114
"sideEffects": false,
1215
"repository": {
1316
"type": "git",

src/debug.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,33 @@ import { isBrowser, stringToRgb } from "./helpers"
55
* debug
66
*/
77
// prettier-ignore
8-
export const debug = (namespace?: string) => (...rest: any[]): void =>
9-
{
8+
export const debug = (namespace?: string) => (...rest: any[]): void => {
109
const rgb = stringToRgb(namespace)
1110

1211
const showLog = (value: string): boolean =>
1312
value?.includes(":*")
1413
? namespace.startsWith( value.split(":*")[0])
1514
: value === namespace || value === "*"
1615

16+
// Allow to bypass dropping of console.log from the build process
17+
// tested with esbuild config: pure: ["console.log"] or drop: ["console"]
18+
const log = console['log']
19+
1720
if (isBrowser)
1821
{
1922
showLog(localStorage.getItem("debug"))
2023
&&
21-
console.log(
24+
log(
2225
namespace && `%c${namespace}`, `color: rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`,
2326
...rest
2427
)
2528
}
29+
2630
else
27-
{
31+
{
2832
showLog(process.env.DEBUG)
2933
&&
30-
console.log(
34+
log(
3135
namespace && couleur.bold(couleur.rgb(rgb[0], rgb[1], rgb[2])(namespace)),
3236
...rest
3337
)

0 commit comments

Comments
 (0)