Skip to content

Commit 0c811a4

Browse files
committed
feat(vite): add core entry resolution for dynamic import\n\n- Introduced a resolveCoreEntry function to dynamically resolve the core module path, enhancing flexibility in module loading.\n- Updated client module initialization to use the resolved core entry, improving compatibility with different module formats.
1 parent 98ae575 commit 0c811a4

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/vite/src/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import ansis from 'ansis';
33
import type { BrowserLogLevel } from '@browser-echo/core';
44
import { mkdirSync, appendFileSync, existsSync, readFileSync } from 'node:fs';
55
import { join as joinPath, dirname } from 'node:path';
6+
import { createRequire } from 'node:module';
7+
const __require = createRequire(import.meta.url);
68

79
export interface BrowserLogsToTerminalOptions {
810
enabled?: boolean;
@@ -291,6 +293,18 @@ function colorize(level: BrowserLogLevel, message: string): string {
291293

292294
type ClientPayload = { sessionId?: string; entries: Array<{ level: BrowserLogLevel | string; text: string; time?: number; stack?: string; source?: string; tag?: string; }>; };
293295

296+
function resolveCoreEntry(): string {
297+
try {
298+
const p = __require.resolve('@browser-echo/core/dist/index.mjs');
299+
return '/@fs/' + p.replace(/\\/g, '/');
300+
} catch {}
301+
try {
302+
const p = __require.resolve('@browser-echo/core');
303+
return '/@fs/' + p.replace(/\\/g, '/');
304+
} catch {}
305+
return '';
306+
}
307+
294308
function makeClientModule(options: Required<BrowserLogsToTerminalOptions>) {
295309
const payload = {
296310
route: options.route,
@@ -301,8 +315,12 @@ function makeClientModule(options: Required<BrowserLogsToTerminalOptions>) {
301315
stackMode: options.stackMode,
302316
networkLogs: options.networkLogs,
303317
};
318+
const coreEntry = resolveCoreEntry();
319+
const importLine = coreEntry
320+
? `import { initBrowserEcho } from '${coreEntry}';`
321+
: `import { initBrowserEcho } from '@browser-echo/core';`;
304322
const code = [
305-
`import { initBrowserEcho } from '@browser-echo/core';`,
323+
importLine,
306324
`if (typeof window !== 'undefined') {`,
307325
` initBrowserEcho(${JSON.stringify(payload)});`,
308326
`}`

0 commit comments

Comments
 (0)