222
schangxiang@126.com
2025-04-30 9bec4dcae002f36aa23231da11cb03a156b40110
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// @ts-nocheck
 
/**
 * 增加debugger组件
 * @param option
 * @returns
 */
export default function VitePluginDevelopmentFilter(option: {
  tag: string
  prodTag: string
}): any {
  return {
    name: 'vite-plugin-development-filter',
    transform(code, id) {
      const { tag, prodTag } = option
      const regexWithCapture = new RegExp(
        `<${tag}>([\\s\\S]*?)<\\/${tag}>`,
        'g'
      )
      const regexWithCaptureProd = new RegExp(
        `<${prodTag}>([\\s\\S]*?)<\\/${prodTag}>`,
        'g'
      )
      if (regexWithCapture.test(code)) {
        if (process.env.NODE_ENV === 'production') {
          const newCode = code.replaceAll(regexWithCapture, '')
          return newCode
            .replaceAll(`<${prodTag}>`, '')
            .replaceAll(`</${prodTag}>`, '')
        }
      }
      if (regexWithCaptureProd.test(code)) {
        if (process.env.NODE_ENV === 'development') {
          const newCode = code.replaceAll(regexWithCaptureProd, '')
          return newCode
        }
      }
      return code
    },
  }
}