Skip to content

Commit 8e6af6f

Browse files
17359898647yuyinws
authored andcommitted
feat: ๐Ÿš€ ๅขžๅŠ ๅ‰ๅŽ็ผ€้€‰้กน
1 parent 53507a0 commit 8e6af6f

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

โ€Žsrc/index.tsโ€Ž

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { SourceMapConsumer } from 'source-map'
66
import type { RawSourceMap } from 'source-map'
77
import { getConsoleStyle, transformFileTypes } from './utils'
88

9-
function VitePluginTurboConsole(): PluginOption {
9+
interface TurboConsoleOptions {
10+
prefix?: string
11+
suffix?: string
12+
}
13+
14+
function VitePluginTurboConsole(option?: TurboConsoleOptions): PluginOption {
1015
return {
1116
name: 'vite-plugin-turbo-console',
1217
enforce: 'post',
@@ -24,10 +29,10 @@ function VitePluginTurboConsole(): PluginOption {
2429
CallExpression: (node: any) => {
2530
const { callee, arguments: args, loc } = node
2631
if (callee.type === 'MemberExpression'
27-
&& callee.object?.type === 'Identifier'
28-
&& callee.object?.name === 'console'
29-
&& callee.property?.type === 'Identifier'
30-
&& callee.property?.name === 'log'
32+
&& callee.object?.type === 'Identifier'
33+
&& callee.object?.name === 'console'
34+
&& callee.property?.type === 'Identifier'
35+
&& callee.property?.name === 'log'
3136
) {
3237
const fileName = basename(id)
3338
const fileType = extname(id)
@@ -46,11 +51,20 @@ function VitePluginTurboConsole(): PluginOption {
4651
.replaceAll('`', '')
4752
.replaceAll('\n', '')
4853
.replaceAll('\"', '')
49-
5054
const argumentStart = args[0].start
51-
magicString.appendLeft(
52-
argumentStart,
53-
`"%c${fileName}:${originalLine} ~ ${argsName}","${getConsoleStyle(fileType)}",`)
55+
const argumentEnd = args[args.length - 1].end
56+
const { prefix, suffix } = option || {}
57+
const _prefix = prefix ? `${prefix} \\n` : ''
58+
const _suffix = suffix ? `\\n ${suffix}` : ''
59+
magicString
60+
.appendLeft(
61+
argumentStart,
62+
`"${_prefix} %c${fileName}:${originalLine} ~ ${argsName}","${getConsoleStyle(fileType)}",`,
63+
)
64+
.appendRight(
65+
argumentEnd,
66+
`,"${_suffix}"`,
67+
)
5468
})
5569

5670
asyncOps.push(asyncOp)

0 commit comments

Comments
ย (0)