Skip to content

Commit

Permalink
feat: add apply mode
Browse files Browse the repository at this point in the history
  • Loading branch information
baozouai committed Feb 23, 2024
1 parent e0fabb3 commit 3779e74
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README-zh_CN.md
Expand Up @@ -54,6 +54,8 @@ npm i vite-plugin-enhance-log -D

```ts
interface Options {
/** 应用在开发或build陌生, 默认都生效 */
apply?: 'serve' | 'build' | ((this: void, config: UserConfig, env: ConfigEnv) => boolean)
/** 高亮文件名(firefox不支持) */
colorFileName?: boolean
/**
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -56,6 +56,8 @@ npm i vite-plugin-enhance-log -D

```ts
interface Options {
/** apply plugin in which mode, default all */
apply?: 'serve' | 'build' | ((this: void, config: UserConfig, env: ConfigEnv) => boolean)
/** colorful filename,but The firefox can't recognize color labels, and garbled characters appear */
colorFileName?: boolean
/**
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
@@ -1,7 +1,7 @@
import traverse from '@babel/traverse'
import { parse } from '@babel/parser'
import generate from '@babel/generator'
import type { PluginOption } from 'vite'
import type { ConfigEnv, PluginOption, UserConfig } from 'vite'
import { createFilter } from 'vite'
import type { StringLiteral } from '@babel/types'
import { SourceMapConsumer } from 'source-map'
Expand Down Expand Up @@ -37,6 +37,8 @@ export type EnableFileName = boolean | {
custom?: (filename: string) => string
}
export interface Options {
/** apply plugin in which mode, default all */
apply?: 'serve' | 'build' | ((this: void, config: UserConfig, env: ConfigEnv) => boolean)
/** colorful filename,but The firefox can't recognize color labels, and garbled characters appear */
colorFileName?: boolean
/**
Expand Down Expand Up @@ -105,6 +107,7 @@ const DEFAULT_PRE_TIP = '🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀'

export default function enhanceLogPlugin(options: Options = {}): PluginOption {
const {
apply,
colorFileName,
logMethodReg = /console\.log/,
preTip = DEFAULT_PRE_TIP,
Expand All @@ -127,6 +130,7 @@ export default function enhanceLogPlugin(options: Options = {}): PluginOption {
configResolved(config) {
root = config.root
},
apply,
enforce: 'post',
async transform(code, id) {
if (!filter(id))
Expand Down

0 comments on commit 3779e74

Please sign in to comment.