Skip to content

Commit 8d0693a

Browse files
committed
feat!: simplify runtime entries and use plain as the base
BREAKING CHANGE: JSR defaults to the browser implementation and no longer exports ./node; use ./ansi for terminal output. Remove legacy main/module fields and the browser.min.js CDN build. closes #7
1 parent 6de31cd commit 8d0693a

8 files changed

Lines changed: 131 additions & 147 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ A lightweight JavaScript debugging utility, forked from [debug](https://www.npmj
1717
<!-- Run `pnpm size` to update the minified + gzip (level 9, decimal kB) entry sizes below. Node built-ins remain external. -->
1818

1919
- ✨ Minimal footprint
20-
- 1.50 kB minified + gzipped (plain)
21-
- 1.48 kB minified + gzipped (browser)
20+
- 1.08 kB minified + gzipped (plain)
21+
- 1.52 kB minified + gzipped (browser)
2222
- 1.73 kB minified + gzipped (ansi)
2323
- 📦 Zero dependencies
2424
- 📝 Full TypeScript support
@@ -34,32 +34,13 @@ A lightweight JavaScript debugging utility, forked from [debug](https://www.npmj
3434
npm install obug
3535
```
3636

37-
### Runtime entries
37+
### Runtimes
3838

39-
The default npm entry selects an implementation through conditional exports:
39+
`obug` automatically selects ANSI output for Node.js/Bun/Deno, plain console output for React Native/Expo and Cloudflare Workers/Vercel Edge/Fastly Compute, and CSS console colors for browsers.
4040

41-
| Runtime | Entry | Output |
42-
| ----------------------------------------------- | ------ | ----------------------------------- |
43-
| Node.js, Bun, Deno | `obug` | ANSI output to `stderr` |
44-
| React Native / Expo | `obug` | React Native console output |
45-
| Cloudflare Workers, Vercel Edge, Fastly Compute | `obug` | Plain console output |
46-
| Browsers | `obug` | Browser-style output to the console |
41+
To choose explicitly, import `obug/ansi`, `obug/plain`, or `obug/browser`.
4742

48-
Use an explicit entry when a bundler does not provide the appropriate export condition:
49-
50-
```ts
51-
import { createDebug } from 'obug/ansi' // terminal / ANSI output
52-
import { createDebug } from 'obug/browser' // browser-style console output
53-
import { createDebug } from 'obug/plain' // plain console output
54-
```
55-
56-
JSR does not provide conditional exports. Its default entry remains the ANSI implementation, and the existing `node` subpath is retained as an alias. Use explicit subpaths for browser or plain console output:
57-
58-
```ts
59-
import { createDebug } from 'jsr:@sxzz/obug' // terminal / ANSI output
60-
import { createDebug } from 'jsr:@sxzz/obug/browser' // browser-style console output
61-
import { createDebug } from 'jsr:@sxzz/obug/plain' // plain console output
62-
```
43+
JSR defaults to the browser implementation without automatic selection. Use `jsr:@sxzz/obug/ansi` for terminals or `jsr:@sxzz/obug/plain` for plain output.
6344

6445
## Usage
6546

‎jsr.json‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
"version": "2.2.1",
55
"license": "MIT",
66
"exports": {
7-
".": "./src/ansi.ts",
7+
".": "./src/browser.ts",
88
"./browser": "./src/browser.ts",
99
"./ansi": "./src/ansi.ts",
10-
"./node": "./src/ansi.ts",
1110
"./plain": "./src/plain.ts"
1211
},
1312
"publish": {

‎package.json‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
"./plain": "./dist/plain.js",
3434
"./package.json": "./package.json"
3535
},
36-
"main": "./dist/ansi.js",
37-
"module": "./dist/ansi.js",
38-
"unpkg": "./dist/browser.min.js",
39-
"jsdelivr": "./dist/browser.min.js",
4036
"react-native": "./dist/plain.js",
4137
"types": "./dist/browser.d.ts",
4238
"files": [

‎scripts/bundle-size.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ try {
2121
for (const name of ['plain', 'browser', 'ansi']) {
2222
const entryDir = join(outDir, name)
2323
await build({
24-
...buildConfig[0],
24+
...buildConfig,
2525
cwd: root,
2626
config: false,
2727
entry: { [name]: join(root, 'src', `${name}.ts`) },

‎src/browser.ts‎

Lines changed: 5 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import {
2-
createDebug as _createDebug,
3-
enable as _enable,
4-
disable,
5-
enabled,
6-
namespaces,
7-
} from './core.ts'
8-
import { humanize, selectColor } from './utils.ts'
1+
import { createDebug as createPlainDebug } from './plain.ts'
2+
import { selectColor } from './utils.ts'
93
import type { Debugger, DebugOptions } from './types.ts'
104

115
const colors: string[] = [
@@ -131,89 +125,16 @@ function formatArgs(
131125
args.splice(lastC, 0, c)
132126
}
133127

134-
/**
135-
* Invokes `console.debug()` when available.
136-
* No-op when `console.debug` is not a "function".
137-
* If `console.debug` is not available, falls back
138-
* to `console.log`.
139-
*/
140-
const log = console.debug || console.log || (() => {})
141-
142-
const defaultOptions: Omit<Required<DebugOptions>, 'color'> = {
143-
useColors: true,
144-
145-
formatArgs,
146-
formatters: {
147-
/**
148-
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
149-
*/
150-
j(v) {
151-
try {
152-
return JSON.stringify(v)
153-
} catch (error: any) {
154-
return `[UnexpectedJSONParseError]: ${error.message}`
155-
}
156-
},
157-
},
158-
inspectOpts: {},
159-
humanize,
160-
161-
log,
162-
}
163-
164128
export function createDebug(
165129
namespace: string,
166130
options?: DebugOptions,
167131
): Debugger {
168132
const color = (options && options.color) ?? selectColor(colors, namespace)
169-
return _createDebug(
133+
return createPlainDebug(
170134
namespace,
171-
Object.assign(defaultOptions, { color }, options),
135+
Object.assign({ useColors: true, color, formatArgs }, options),
172136
)
173137
}
174138

175-
function load(): string {
176-
let r: string | null | undefined
177-
try {
178-
// TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context.
179-
r = localStorage.getItem('debug') || localStorage.getItem('DEBUG')
180-
} catch {
181-
// Swallow
182-
// XXX (@Qix-) should we be logging these?
183-
}
184-
185-
// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
186-
if (!r && typeof process !== 'undefined' && 'env' in process) {
187-
r = process.env.DEBUG
188-
}
189-
190-
return r || ''
191-
}
192-
193-
function save(namespaces: string) {
194-
try {
195-
if (namespaces) {
196-
localStorage.setItem('debug', namespaces)
197-
} else {
198-
localStorage.removeItem('debug')
199-
}
200-
} catch {
201-
// Swallow
202-
// XXX (@Qix-) should we be logging these?
203-
}
204-
}
205-
206-
/**
207-
* Enables a debug mode by namespaces. This can include modes
208-
* separated by a colon and wildcards.
209-
*/
210-
function enable(namespaces: string): void {
211-
save(namespaces)
212-
_enable(namespaces)
213-
}
214-
215-
// eslint-disable-next-line unicorn/no-top-level-side-effects
216-
_enable(load())
217-
139+
export { disable, enable, enabled, namespaces } from './plain.ts'
218140
export type * from './types.ts'
219-
export { disable, enable, enabled, namespaces }

‎src/plain.ts‎

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,101 @@
11
import {
2-
createDebug as createBrowserDebug,
2+
createDebug as _createDebug,
3+
enable as _enable,
34
disable,
4-
enable,
55
enabled,
66
namespaces,
7-
} from './browser.ts'
7+
} from './core.ts'
8+
import { humanize } from './utils.ts'
89
import type { Debugger, DebugOptions } from './types.ts'
910

11+
function formatArgs(
12+
this: Debugger,
13+
diff: number,
14+
args: [string, ...any[]],
15+
): void {
16+
args[0] = `${this.namespace} ${args[0]} +${this.humanize(diff)}`
17+
}
18+
19+
/**
20+
* Invokes `console.debug()` when available.
21+
* No-op when `console.debug` is not a "function".
22+
* If `console.debug` is not available, falls back
23+
* to `console.log`.
24+
*/
25+
const log = console.debug || console.log || (() => {})
26+
27+
const defaultOptions: Required<DebugOptions> = {
28+
useColors: false,
29+
color: '',
30+
31+
formatArgs,
32+
formatters: {
33+
/**
34+
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
35+
*/
36+
j(v) {
37+
try {
38+
return JSON.stringify(v)
39+
} catch (error: any) {
40+
return `[UnexpectedJSONParseError]: ${error.message}`
41+
}
42+
},
43+
},
44+
inspectOpts: {},
45+
humanize,
46+
47+
log,
48+
}
49+
1050
export function createDebug(
1151
namespace: string,
1252
options?: DebugOptions,
1353
): Debugger {
14-
return createBrowserDebug(
15-
namespace,
16-
Object.assign({}, options, { useColors: false }),
17-
)
54+
return _createDebug(namespace, Object.assign({}, defaultOptions, options))
55+
}
56+
57+
function load(): string {
58+
let r: string | null | undefined
59+
try {
60+
// TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context.
61+
r = localStorage.getItem('debug') || localStorage.getItem('DEBUG')
62+
} catch {
63+
// Swallow
64+
// XXX (@Qix-) should we be logging these?
65+
}
66+
67+
// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
68+
if (!r && typeof process !== 'undefined' && 'env' in process) {
69+
r = process.env.DEBUG
70+
}
71+
72+
return r || ''
73+
}
74+
75+
function save(namespaces: string) {
76+
try {
77+
if (namespaces) {
78+
localStorage.setItem('debug', namespaces)
79+
} else {
80+
localStorage.removeItem('debug')
81+
}
82+
} catch {
83+
// Swallow
84+
// XXX (@Qix-) should we be logging these?
85+
}
86+
}
87+
88+
/**
89+
* Enables a debug mode by namespaces. This can include modes
90+
* separated by a colon and wildcards.
91+
*/
92+
function enable(namespaces: string): void {
93+
save(namespaces)
94+
_enable(namespaces)
1895
}
1996

97+
// eslint-disable-next-line unicorn/no-top-level-side-effects
98+
_enable(load())
99+
20100
export type * from './types.ts'
21101
export { disable, enable, enabled, namespaces }

‎tests/browser.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,22 @@ describe('browser runtime', () => {
1010
it('does not emit CSS console formatting in React Native', () => {
1111
expect(createReactNativeDebug('react-native').useColors).toBe(false)
1212
})
13+
14+
it('keeps plain output independent of browser debugger options', () => {
15+
const messages: any[][] = []
16+
const plain = createReactNativeDebug('plain', {
17+
log: (...args) => messages.push(args),
18+
humanize: () => '0ms',
19+
})
20+
plain.enabled = true
21+
const browser = createDebug('browser', { useColors: true })
22+
plain('hello %j', { ok: true })
23+
const child = plain.extend('child')
24+
child.enabled = true
25+
child('world')
26+
expect(messages[0]).toEqual(['plain hello {"ok":true} +0ms'])
27+
expect(messages[1]).toEqual(['plain:child world +0ms'])
28+
expect(browser.useColors).toBe(true)
29+
expect(createReactNativeDebug('another').useColors).toBe(false)
30+
})
1331
})

‎tsdown.config.ts‎

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
11
import type { UserConfig } from 'tsdown'
22

3-
const config: UserConfig[] = [
4-
{
5-
entry: ['./src/{ansi,browser,plain}.ts'],
6-
platform: 'neutral',
7-
target: 'es2015',
8-
dts: true,
9-
deps: {
10-
neverBundle(id, importer) {
11-
if (importer?.endsWith('ansi.ts') && id.startsWith('node:')) {
12-
return true
13-
}
14-
},
3+
const config: UserConfig = {
4+
entry: ['./src/{ansi,browser,plain}.ts'],
5+
platform: 'neutral',
6+
target: 'es2015',
7+
dts: true,
8+
deps: {
9+
neverBundle(id, importer) {
10+
if (importer?.endsWith('ansi.ts') && id.startsWith('node:')) {
11+
return true
12+
}
1513
},
16-
hash: false,
17-
minify: 'dce-only',
1814
},
19-
{
20-
entry: {
21-
'browser.min': './src/browser.ts',
22-
},
23-
platform: 'browser',
24-
target: 'es2015',
25-
dts: false,
26-
minify: true,
27-
},
28-
]
15+
hash: false,
16+
minify: 'dce-only',
17+
}
2918

3019
export default config

0 commit comments

Comments
 (0)