@@ -6,7 +6,12 @@ import { SourceMapConsumer } from 'source-map'
66import type { RawSourceMap } from 'source-map'
77import { 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