Skip to content

Commit

Permalink
fix: shims for cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyinws committed Jul 16, 2024
1 parent 74f3892 commit 9950b9e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 67 deletions.
4 changes: 2 additions & 2 deletions examples/vite-vue3/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
</template>

<script setup lang="ts">
import { Server } from 'unplugin-turbo-console/helper'
import { server } from 'unplugin-turbo-console/helper'
function test() {
Server.log('xxx')
server.log('xxx')
}
</script>
76 changes: 76 additions & 0 deletions helper.js
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 }
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -93,7 +91,8 @@
},
"files": [
"client.d.ts",
"dist"
"dist",
"helper.js"
],
"scripts": {
"build": "rimraf dist && tsup",
Expand Down
5 changes: 0 additions & 5 deletions scripts/postbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
11 changes: 1 addition & 10 deletions src/core/dir.ts
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')
46 changes: 0 additions & 46 deletions src/helper.ts

This file was deleted.

1 change: 1 addition & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default defineConfig({
format: ['esm', 'cjs'],
dts: true,
splitting: true,
shims: true,
onSuccess: 'npm run build:fix',
})

0 comments on commit 9950b9e

Please sign in to comment.