Skip to content

Commit 0210c17

Browse files
committed
feat: restructure logger module and update build configuration for improved browser compatibility
1 parent 3b4d059 commit 0210c17

4 files changed

Lines changed: 48 additions & 6 deletions

File tree

packages/client/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import {
8787
WellKnownCapabilities,
8888
ApiRoutes,
8989
} from '@objectstack/spec/api';
90-
import { Logger, createLogger } from '@objectstack/core';
90+
import { Logger, createLogger } from '@objectstack/core/logger';
9191
import { RealtimeAPI } from './realtime-api';
9292

9393
/**

packages/core/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
"types": "./dist/index.d.ts",
1212
"import": "./dist/index.js",
1313
"require": "./dist/index.cjs"
14+
},
15+
"./logger": {
16+
"types": "./dist/logger.d.ts",
17+
"import": "./dist/logger.js",
18+
"require": "./dist/logger.cjs"
1419
}
1520
},
1621
"scripts": {
17-
"build": "tsup --config ../../tsup.config.ts",
22+
"build": "tsup",
1823
"test": "vitest run",
1924
"test:watch": "vitest"
2025
},

packages/core/src/logger.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import type { LoggerConfig, LogLevel } from '@objectstack/spec/system';
44
import type { Logger } from '@objectstack/spec/contracts';
55

6+
// Re-export the contract type so consumers can do
7+
// `import type { Logger } from '@objectstack/core/logger'` without also
8+
// pulling `@objectstack/spec` into their bundle graph manually.
9+
export type { Logger };
10+
611
const LEVEL_ORDER: Record<LogLevel, number> = {
712
debug: 0,
813
info: 1,
@@ -115,10 +120,23 @@ export class ObjectLogger implements Logger {
115120

116121
const out = line + '\n';
117122

118-
if (level === 'error' || level === 'fatal') {
119-
process.stderr?.write(out);
120-
} else {
121-
process.stdout?.write(out);
123+
// Browser-safe output: prefer process streams when available, otherwise
124+
// fall back to console. The previous unguarded `process.stderr?.write`
125+
// throws `ReferenceError: process is not defined` in browsers because
126+
// `process` itself is the missing global, not just its `stderr` field.
127+
if (typeof process !== 'undefined' && (process as any).stderr) {
128+
if (level === 'error' || level === 'fatal') {
129+
(process as any).stderr.write(out);
130+
} else {
131+
(process as any).stdout?.write(out);
132+
}
133+
} else if (typeof console !== 'undefined') {
134+
const fn =
135+
level === 'error' || level === 'fatal' ? console.error
136+
: level === 'warn' ? console.warn
137+
: level === 'debug' ? console.debug
138+
: console.log;
139+
fn(line);
122140
}
123141

124142
if (this.fileStream) {

packages/core/tsup.config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineConfig } from 'tsup';
2+
3+
/**
4+
* `@objectstack/core` ships two entry points:
5+
* - `index.ts` — full microkernel (Node-only; pulls in plugin sandbox,
6+
* filesystem helpers, etc).
7+
* - `logger.ts` — tiny browser-safe logger reused by `@objectstack/client`
8+
* so importing the client SDK in a browser bundle does NOT drag in
9+
* Node-only kernel internals (e.g. `process.cpuUsage()`).
10+
*/
11+
export default defineConfig({
12+
entry: ['src/index.ts', 'src/logger.ts'],
13+
splitting: false,
14+
sourcemap: true,
15+
clean: true,
16+
dts: true,
17+
format: ['esm', 'cjs'],
18+
target: 'es2020',
19+
});

0 commit comments

Comments
 (0)