diff --git a/examples/vite-vue3/src/App.vue b/examples/vite-vue3/src/App.vue index 46c38e4..21b06d0 100644 --- a/examples/vite-vue3/src/App.vue +++ b/examples/vite-vue3/src/App.vue @@ -3,9 +3,9 @@ diff --git a/helper.js b/helper.js new file mode 100644 index 0000000..bea5224 --- /dev/null +++ b/helper.js @@ -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 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 } diff --git a/package.json b/package.json index f39d2ff..e80cd18 100644 --- a/package.json +++ b/package.json @@ -71,9 +71,7 @@ "require": "./dist/types.cjs" }, "./helper": { - "types": "./dist/helper.d.ts", - "import": "./dist/helper.js", - "require": "./dist/helper.cjs" + "import": "./helper.js" }, "./*": "./*", "./client": { @@ -93,7 +91,8 @@ }, "files": [ "client.d.ts", - "dist" + "dist", + "helper.js" ], "scripts": { "build": "rimraf dist && tsup", diff --git a/scripts/postbuild.ts b/scripts/postbuild.ts index 26bcd4d..3faca44 100644 --- a/scripts/postbuild.ts +++ b/scripts/postbuild.ts @@ -20,11 +20,6 @@ async function run() { await fs.writeFile(file, code) } - const helperFile = join(dirname(fileURLToPath(import.meta.url)), '../dist/helper.js') - let helperCode = await fs.readFile(helperFile, 'utf8') - helperCode = `// turbo-console-disable\n${helperCode}` - await fs.writeFile(helperFile, helperCode) - const source = join(dirname(fileURLToPath(import.meta.url)), '../src/core/client') const target = join(dirname(fileURLToPath(import.meta.url)), '../dist/client') diff --git a/src/core/dir.ts b/src/core/dir.ts index 72117a3..5bd5c9f 100644 --- a/src/core/dir.ts +++ b/src/core/dir.ts @@ -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') diff --git a/src/helper.ts b/src/helper.ts deleted file mode 100644 index 2544e19..0000000 --- a/src/helper.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { env } from 'node:process' -import { PLUGIN_NAME } from './core/constants' -import type { TCMethod, TConsole } from './types' - -function generateFetchUrl(args: any[], method: TCMethod) { - const port = env.UNPLUGIN_TURBO_CONSOLE_SERVER_PORT - - if (!port) - console.warn(`[${PLUGIN_NAME}]: UNPLUGIN_TURBO_CONSOLE_SERVER_PORT env not found`) - - return `http://localhost:${port || 3070}/send?m=${JSON.stringify(args)}&t=${method}` -} - -function handleClient(method: TCMethod, ...args: any[]) { - (console as any)[method](...args) - if (globalThis.window || env.NODE_ENV === 'production') - return - - fetch(generateFetchUrl(args, method)).catch(() => {}) -} - -export const client: TConsole = { - log: (...args: any[]) => handleClient('log', ...args), - error: (...args: any[]) => handleClient('error', ...args), - warn: (...args: any[]) => handleClient('warn', ...args), - info: (...args: any[]) => handleClient('info', ...args), - table: (...args: any[]) => handleClient('table', ...args), - dir: (...args: any[]) => handleClient('dir', ...args), -} - -function handleServer(method: TCMethod, ...args: any[]) { - (console as any)[method](...args) - const socket: WebSocket | undefined = (globalThis?.window as any)?.UNPLUGIN_TURBO_CONSOLE_CLIENT_SOCKET - if (socket && socket.readyState === WebSocket.OPEN) { - socket.send(JSON.stringify({ m: JSON.stringify(args), t: method })) - } -} - -export const server: TConsole = { - log: (...args: any[]) => handleServer('log', ...args), - error: (...args: any[]) => handleServer('error', ...args), - warn: (...args: any[]) => handleServer('warn', ...args), - info: (...args: any[]) => handleServer('info', ...args), - table: (...args: any[]) => handleServer('table', ...args), - dir: (...args: any[]) => handleServer('dir', ...args), -} diff --git a/tsup.config.ts b/tsup.config.ts index e0aca29..36f4ac5 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -8,5 +8,6 @@ export default defineConfig({ format: ['esm', 'cjs'], dts: true, splitting: true, + shims: true, onSuccess: 'npm run build:fix', })