-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
83 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// turbo-console-disable | ||
/* eslint-disable no-console */ | ||
import { env } from 'node:process' | ||
|
||
/** | ||
* @typedef {'log' | 'error' | 'warn' | 'info' | 'table' | 'dir'} TCMethod | ||
*/ | ||
|
||
/** | ||
* @typedef {Record<TCMethod, (...args: any[]) => void>} TConsole | ||
*/ | ||
|
||
/** | ||
* Generates a fetch URL for logging messages. | ||
* @param {any[]} args - The arguments to log. | ||
* @param {TCMethod} method - The logging method. | ||
* @returns {string} The generated fetch URL. | ||
*/ | ||
function generateFetchUrl(args, method) { | ||
const port = env.UNPLUGIN_TURBO_CONSOLE_SERVER_PORT | ||
|
||
if (!port) { | ||
console.warn(`[UNPLUGIN_TURBO_CONSOLE]: UNPLUGIN_TURBO_CONSOLE_SERVER_PORT env not found`) | ||
} | ||
|
||
return `http://localhost:${port || 3070}/send?m=${JSON.stringify(args)}&t=${method}` | ||
} | ||
|
||
/** | ||
* Handles client-side logging. | ||
* @param {TCMethod} method - The logging method. | ||
* @param {...any} args - The arguments to log. | ||
*/ | ||
function handleClient(method, ...args) { | ||
console[method](...args) | ||
if (globalThis.window || env.NODE_ENV === 'production') { | ||
return | ||
} | ||
|
||
fetch(generateFetchUrl(args, method)).catch(() => {}) | ||
} | ||
|
||
/** @type {TConsole} */ | ||
const client = { | ||
log: (...args) => handleClient('log', ...args), | ||
error: (...args) => handleClient('error', ...args), | ||
warn: (...args) => handleClient('warn', ...args), | ||
info: (...args) => handleClient('info', ...args), | ||
table: (...args) => handleClient('table', ...args), | ||
dir: (...args) => handleClient('dir', ...args), | ||
} | ||
|
||
/** | ||
* Handles server-side logging. | ||
* @param {TCMethod} method - The logging method. | ||
* @param {...any} args - The arguments to log. | ||
*/ | ||
function handleServer(method, ...args) { | ||
console[method](...args) | ||
const socket = globalThis?.window?.UNPLUGIN_TURBO_CONSOLE_CLIENT_SOCKET | ||
if (socket && socket.readyState === WebSocket.OPEN) { | ||
socket.send(JSON.stringify({ m: JSON.stringify(args), t: method })) | ||
} | ||
} | ||
|
||
/** @type {TConsole} */ | ||
const server = { | ||
log: (...args) => handleServer('log', ...args), | ||
error: (...args) => handleServer('error', ...args), | ||
warn: (...args) => handleServer('warn', ...args), | ||
info: (...args) => handleServer('info', ...args), | ||
table: (...args) => handleServer('table', ...args), | ||
dir: (...args) => handleServer('dir', ...args), | ||
} | ||
|
||
export { client, server } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,8 @@ | ||
import { fileURLToPath } from 'node:url' | ||
import { dirname, resolve } from 'pathe' | ||
|
||
function getImportMetaUrl() { | ||
return typeof document === 'undefined' | ||
? new URL(`file:${__filename}`).href | ||
: (document.currentScript && (document.currentScript as any).src) | ||
|| new URL('main.js', document.baseURI).href | ||
} | ||
|
||
export const importMetaUrl = /* @__PURE__ */ getImportMetaUrl() | ||
|
||
export const DIR_DIST = typeof __dirname !== 'undefined' | ||
? __dirname | ||
: dirname(fileURLToPath(importMetaUrl)) | ||
: dirname(fileURLToPath(import.meta.url)) | ||
|
||
export const CLIENT_DIR = resolve(DIR_DIST, './client') |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters