Skip to content

Commit 0e828cd

Browse files
sxzzrexxars
andcommitted
feat: add runtime-specific debug entries
Co-authored-by: Espen Hovlandsdal <[email protected]> closes #1, closes #6
1 parent 4a0bca9 commit 0e828cd

9 files changed

Lines changed: 80 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ A lightweight JavaScript debugging utility, forked from [debug](https://www.npmj
3131
npm install obug
3232
```
3333

34+
### Runtime entries
35+
36+
The default npm entry selects an implementation through conditional exports:
37+
38+
| Runtime | Entry | Output |
39+
| ----------------------------------------------- | ------ | ----------------------------------- |
40+
| Node.js, Bun, Deno | `obug` | ANSI output to `stderr` |
41+
| React Native / Expo | `obug` | React Native console output |
42+
| Cloudflare Workers, Vercel Edge, Fastly Compute | `obug` | Plain console output |
43+
| Browsers | `obug` | Browser-style output to the console |
44+
45+
Use an explicit entry when a bundler does not provide the appropriate export condition:
46+
47+
```ts
48+
import { createDebug } from 'obug/ansi' // terminal / ANSI output
49+
import { createDebug } from 'obug/browser' // browser-style console output
50+
import { createDebug } from 'obug/plain' // plain console output
51+
```
52+
53+
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:
54+
55+
```ts
56+
import { createDebug } from 'jsr:@sxzz/obug' // terminal / ANSI output
57+
import { createDebug } from 'jsr:@sxzz/obug/browser' // browser-style console output
58+
import { createDebug } from 'jsr:@sxzz/obug/plain' // plain console output
59+
```
60+
3461
## Usage
3562
3663
```ts

‎jsr.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
"version": "2.1.4",
55
"license": "MIT",
66
"exports": {
7-
".": "./src/node.ts",
7+
".": "./src/ansi.ts",
88
"./browser": "./src/browser.ts",
9-
"./node": "./src/node.ts"
9+
"./ansi": "./src/ansi.ts",
10+
"./node": "./src/ansi.ts",
11+
"./plain": "./src/plain.ts"
1012
},
1113
"publish": {
1214
"include": [

‎package.json‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,24 @@
2020
"sideEffects": false,
2121
"exports": {
2222
".": {
23+
"workerd": "./dist/plain.js",
24+
"edge-light": "./dist/plain.js",
25+
"fastly": "./dist/plain.js",
2326
"browser": "./dist/browser.js",
24-
"default": "./dist/node.js"
27+
"react-native": "./dist/plain.js",
28+
"node": "./dist/ansi.js",
29+
"default": "./dist/browser.js"
2530
},
31+
"./ansi": "./dist/ansi.js",
32+
"./browser": "./dist/browser.js",
33+
"./plain": "./dist/plain.js",
2634
"./package.json": "./package.json"
2735
},
28-
"main": "./dist/node.js",
29-
"module": "./dist/node.js",
36+
"main": "./dist/ansi.js",
37+
"module": "./dist/ansi.js",
3038
"unpkg": "./dist/browser.min.js",
3139
"jsdelivr": "./dist/browser.min.js",
40+
"react-native": "./dist/plain.js",
3241
"types": "./dist/browser.d.ts",
3342
"files": [
3443
"dist"
File renamed without changes.

‎src/plain.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {
2+
createDebug as createBrowserDebug,
3+
disable,
4+
enable,
5+
enabled,
6+
namespaces,
7+
} from './browser.ts'
8+
import type { Debugger, DebugOptions } from './types.ts'
9+
10+
export function createDebug(
11+
namespace: string,
12+
options?: DebugOptions,
13+
): Debugger {
14+
return createBrowserDebug(namespace, { ...options, useColors: false })
15+
}
16+
17+
export type * from './types.ts'
18+
export { disable, enable, enabled, namespaces }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { formatWithOptions } from 'node:util'
22
import { beforeEach, describe, expect, it, vi } from 'vitest'
3-
import { createDebug, enable } from '../src/node'
3+
import { createDebug, enable } from '../src/ansi'
44
import type { InspectOptions } from '../src/types'
55

66
vi.mock('node:util', async (importActual) => {
@@ -17,7 +17,7 @@ beforeEach(() => {
1717
vi.clearAllMocks()
1818
})
1919

20-
describe('debug node', () => {
20+
describe('debug ansi', () => {
2121
describe('formatting options', () => {
2222
it('calls util.formatWithOptions', () => {
2323
enable('*')

‎tests/basic.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { assert, describe, it } from 'vitest'
22

33
const { createDebug, enable, disable } = (await import(
4-
/* @vite-ignore */ globalThis.process ? '../src/node.ts' : '../src/browser.ts'
5-
)) as typeof import('../src/node')
4+
/* @vite-ignore */ globalThis.process ? '../src/ansi.ts' : '../src/browser.ts'
5+
)) as typeof import('../src/ansi')
66

77
describe('basic', () => {
88
it('passes a basic sanity check', () => {

‎tests/browser.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { createDebug } from '../src/browser'
3+
import { createDebug as createReactNativeDebug } from '../src/plain'
4+
5+
describe('browser runtime', () => {
6+
it('uses CSS console formatting by default', () => {
7+
expect(createDebug('browser').useColors).toBe(true)
8+
})
9+
10+
it('does not emit CSS console formatting in React Native', () => {
11+
expect(createReactNativeDebug('react-native').useColors).toBe(false)
12+
})
13+
})

‎tsdown.config.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { defineConfig } from 'tsdown'
22

33
export default defineConfig([
44
{
5-
entry: ['./src/{browser,node}.ts'],
5+
entry: ['./src/{ansi,browser,plain}.ts'],
66
platform: 'neutral',
77
target: 'es2015',
88
dts: true,
99
deps: {
1010
neverBundle(id, importer) {
11-
if (importer?.endsWith('node.ts') && id.startsWith('node:')) {
11+
if (importer?.endsWith('ansi.ts') && id.startsWith('node:')) {
1212
return true
1313
}
1414
},

0 commit comments

Comments
 (0)