Skip to content

Commit 1acaede

Browse files
committed
Can use arbitrary varaible
1 parent f7d97b2 commit 1acaede

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

README.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,63 @@ Plus, [see the docs](https://substrate-system.github.io/debug/) generated by typ
3939
## config
4040

4141
### namespace
42-
If you pass in a variable of the form `VITE_DEBUG=example`, then this will log
43-
any instances created with the `example` namespace:
42+
Works with `vite` or other systems. This will look at `import.meta.env`, or
43+
an arbitrary object you can pass in.
4444

4545
```js
4646
import Debug from '@substrate-system/debug'
47+
48+
// look at `import.meta.env.VITE_DEBUG`
4749
const debug = Debug('example')
50+
51+
// or call with your own env object
52+
const debug = Debug('example', { DEBUG: 'example' })
4853
```
4954

5055
If you create an instance without passing in a `namespace` string, then this
51-
will log iff the website is in `DEV` mode (if `import.meta.env.DEV` is true).
56+
will log iff anything other than `false` is passed as an argument.
57+
58+
```js
59+
import Debug from '@substrate-system/debug'
60+
61+
// log b/c we are not calling with `false`
62+
const debug = Debug()
63+
64+
debug('hello')
65+
```
66+
67+
You can use any variable as debug status:
68+
69+
```js
70+
import Debug from '@substrate-system/debug'
71+
72+
// in Vite
73+
const debug = Debug(!!(import.meta.env && import.meta.env.DEV))
74+
75+
// in an arbitrary server
76+
const debug = Debug(window.EXAMPLE_DEBUG_MODE)
77+
```
5278
5379
Use an env variable of `*` to log everything.
5480
81+
82+
### NODE_ENV + Vite
83+
5584
Build the site with a `NODE_ENV` variable to set `import.meta.env.DEV`:
5685
5786
```sh
5887
NODE_ENV=development vite build
5988
```
6089
61-
This will work too. Any value of `NODE_ENV`, except `production`, wil equate to
90+
Any value of `NODE_ENV`, except `production`, wil equate to
6291
`import.meta.env.DEV` being true.
6392
6493
```sh
6594
NODE_ENV=staging vite build
6695
```
6796
97+
----------------------------------------------------------------------
98+
6899
## install
69100
70101
```sh
@@ -74,8 +105,10 @@ npm i -D @substrate-system/debug
74105
Use this with [vite](https://vitejs.dev/) in the [browser](#browser), or
75106
in [node](#node-JS).
76107
108+
77109
------------------------------------------------------------------
78110
111+
79112
## Node JS
80113
Run your script with an env variable, `DEBUG`.
81114
@@ -116,6 +149,9 @@ Start a `vite` server and log some things. This uses [the example directory](./e
116149
npm start
117150
```
118151
152+
153+
## test
154+
119155
### node
120156
Run tests:
121157

example/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const debug4 = Debug('bazzz')
2222
debug4('bazzzzzzz') // should not log
2323

2424
const debug5 = Debug()
25-
debug5('debug 5 should be the same color as debug 2')
25+
debug5("debug 5 should be a different color, because it's a different instance.")
2626

2727
// testing the * variable
2828
const debug7 = Debug('quxxx')

example/static/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const debugThree = Debug(true)
1010

1111
debugThree('three')
1212

13+
const debugFalse = Debug(false)
14+
debugFalse('should not see this')
15+
1316
// @ts-expect-error dev
1417
window.debug = debug
1518

@@ -20,3 +23,9 @@ setTimeout(() => {
2023
}, 1000)
2124

2225
debugTwo('...aaa again')
26+
27+
// need to use the boolean coersion, else it will pass in `undefined`
28+
const debugFive = Debug(!!(import.meta.env && import.meta.env.DEV))
29+
30+
// will not see this
31+
debugFive('five!')

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
"build-docs": "typedoc ./src/browser.ts ./src/node.ts",
3030
"toc": "markdown-toc --maxdepth 3 -i README.md",
3131
"start": "vite --mode=staging",
32+
"start:star": "VITE_DEBUG=\"*\" vite",
33+
"start:vite-production": "NODE_EN\"production\" vite",
34+
"start:bar-production": "NODE_ENV=\"production\" VITE_DEBUG=\"barrr\" vite",
35+
"start:bar-dev": "VITE_DEBUG=\"barrr\" vite",
3236
"start:static": "npm run build:static && http-server -p 8888 example/static",
3337
"build:static": "esbuild ./example/static/index.ts --bundle > ./example/static/bundle.js",
3438
"preversion": "npm run lint",

src/browser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function isEnabled (
2121
namespace:string,
2222
env?:ImportMetaEnv|Record<string, string>
2323
):boolean {
24-
if (env && (env.VITE_DEBUG || env.DEBUG === '*')) return true
24+
if (env && (env.VITE_DEBUG === '*' || env.DEBUG === '*')) return true
2525

2626
// if we were not called with a namespace
2727
if (namespace === 'DEV') {

0 commit comments

Comments
 (0)