Skip to content

Commit 1e0c9b0

Browse files
Copilothotlong
andcommitted
Fix Vite browser bundling - make Node.js module imports conditional
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 6dc6e9a commit 1e0c9b0

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

packages/core/src/logger.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { createRequire } from 'module';
21
import type { LoggerConfig, LogLevel } from '@objectstack/spec/system';
32
import type { Logger } from '@objectstack/spec/contracts';
43

5-
const require = createRequire(import.meta.url);
6-
74
/**
85
* Universal Logger Implementation
96
*
@@ -24,6 +21,7 @@ export class ObjectLogger implements Logger {
2421
private isNode: boolean;
2522
private pinoLogger?: any; // Pino logger instance for Node.js
2623
private pinoInstance?: any; // Base Pino instance for creating child loggers
24+
private require?: any; // CommonJS require function for Node.js
2725

2826
constructor(config: Partial<LoggerConfig> = {}) {
2927
// Detect runtime environment
@@ -56,8 +54,13 @@ export class ObjectLogger implements Logger {
5654
if (!this.isNode) return;
5755

5856
try {
57+
// Create require function dynamically for Node.js (avoids bundling issues in browser)
58+
// @ts-ignore - dynamic import of Node.js module
59+
const { createRequire } = eval('require("module")');
60+
this.require = createRequire(import.meta.url);
61+
5962
// Synchronous import for Pino using createRequire (works in ESM)
60-
const pino = require('pino');
63+
const pino = this.require('pino');
6164

6265
// Build Pino options
6366
const pinoOptions: any = {
@@ -81,7 +84,7 @@ export class ObjectLogger implements Logger {
8184
// Check if pino-pretty is available
8285
let hasPretty = false;
8386
try {
84-
require.resolve('pino-pretty');
87+
this.require.resolve('pino-pretty');
8588
hasPretty = true;
8689
} catch (e) {
8790
// ignore

packages/core/src/security/plugin-signature-verifier.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import type { PluginMetadata } from '../plugin-loader.js';
55
let cryptoModule: typeof import('crypto') | null = null;
66
if (typeof (globalThis as any).window === 'undefined') {
77
try {
8-
// Dynamic import for Node.js crypto module
9-
// eslint-disable-next-line @typescript-eslint/no-var-requires
10-
cryptoModule = require('crypto');
8+
// Dynamic import for Node.js crypto module (using eval to avoid bundling issues)
9+
// @ts-ignore - dynamic require for Node.js
10+
cryptoModule = eval('require("crypto")');
1111
} catch {
1212
// Crypto module not available (e.g., browser environment)
1313
}

0 commit comments

Comments
 (0)