index.ts 3.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import debug from 'debug'
2 3
import { once } from '@dcloudio/uni-shared'

fxy060608's avatar
fxy060608 已提交
4 5 6 7 8
import {
  M,
  defineUniMainJsPlugin,
  getUniStatistics,
  parseManifestJsonOnce,
fxy060608's avatar
fxy060608 已提交
9
  parsePagesJson,
fxy060608's avatar
fxy060608 已提交
10
  isSsr,
fxy060608's avatar
fxy060608 已提交
11
  resolveBuiltIn,
fxy060608's avatar
fxy060608 已提交
12
} from '@dcloudio/uni-cli-shared'
13 14 15 16 17
const uniStatLog = once((text: string) => {
  console.log()
  console.warn(text)
  console.log()
})
fxy060608's avatar
fxy060608 已提交
18

fxy060608's avatar
fxy060608 已提交
19
export default () => [
fxy060608's avatar
fxy060608 已提交
20
  defineUniMainJsPlugin((opts) => {
fxy060608's avatar
fxy060608 已提交
21
    let statVersion: '1' | '2' = '1'
fxy060608's avatar
fxy060608 已提交
22
    let isEnable = false
fxy060608's avatar
fxy060608 已提交
23 24 25 26 27 28 29 30
    const stats: Record<string, string> = {
      '@dcloudio/uni-stat': resolveBuiltIn(
        '@dcloudio/uni-stat/dist/uni-stat.es.js'
      ),
      '@dcloudio/uni-cloud-stat': resolveBuiltIn(
        '@dcloudio/uni-stat/dist/uni-cloud-stat.es.js'
      ),
    }
fxy060608's avatar
fxy060608 已提交
31

fxy060608's avatar
fxy060608 已提交
32
    return {
fxy060608's avatar
fxy060608 已提交
33
      name: 'uni:stat',
fxy060608's avatar
fxy060608 已提交
34 35 36 37 38
      enforce: 'pre',
      config(config, env) {
        const inputDir = process.env.UNI_INPUT_DIR!
        const platform = process.env.UNI_PLATFORM!
        const titlesJson = Object.create(null)
39 40 41 42 43 44 45 46 47 48 49 50
        parsePagesJson(inputDir, platform).pages.forEach((page: any) => {
          const style = page.style || {}
          const titleText =
            // MP
            style.navigationBarTitleText ||
            // H5 || App
            style.navigationBar?.titleText ||
            ''
          if (titleText) {
            titlesJson[page.path] = titleText
          }
        })
fxy060608's avatar
fxy060608 已提交
51 52
        // ssr 时不开启
        if (!isSsr(env.command, config)) {
fxy060608's avatar
fxy060608 已提交
53
          const statConfig = getUniStatistics(inputDir, platform)
54
          const uniCloudConfig = statConfig.uniCloud || {}
fxy060608's avatar
fxy060608 已提交
55 56
          statVersion = statConfig.version === '2' ? '2' : '1'
          isEnable = statConfig.enable === true
57 58

          process.env.UNI_STAT_UNI_CLOUD = JSON.stringify(uniCloudConfig)
fxy060608's avatar
fxy060608 已提交
59
          process.env.UNI_STAT_DEBUG = statConfig.debug ? 'true' : 'false'
fxy060608's avatar
fxy060608 已提交
60 61 62
          if (process.env.NODE_ENV === 'production') {
            const manifestJson = parseManifestJsonOnce(inputDir)
            if (!manifestJson.appid) {
63
              uniStatLog(M['stat.warn.appid'])
fxy060608's avatar
fxy060608 已提交
64
              isEnable = false
M
mehaotian 已提交
65
            } else {
fxy060608's avatar
fxy060608 已提交
66
              if (!statConfig.version) {
67
                uniStatLog(M['stat.warn.version'])
fxy060608's avatar
fxy060608 已提交
68
              }
69 70 71 72 73 74 75
              if (isEnable) {
                uniStatLog(`已开启 uni统计${statConfig.version}.0 版本`)
              }
            }
          } else {
            if (isEnable) {
              uniStatLog(M['stat.warn.tip'].replace('{version}', '1.0'))
fxy060608's avatar
fxy060608 已提交
76
            }
fxy060608's avatar
fxy060608 已提交
77
          }
78

fxy060608's avatar
fxy060608 已提交
79
          debug('uni:stat')('isEnable', isEnable)
fxy060608's avatar
fxy060608 已提交
80
        }
81

fxy060608's avatar
fxy060608 已提交
82 83 84 85
        process.env.UNI_STAT_TITLE_JSON = JSON.stringify(titlesJson)
        return {
          define: {
            'process.env.UNI_STAT_TITLE_JSON': process.env.UNI_STAT_TITLE_JSON,
86
            'process.env.UNI_STAT_UNI_CLOUD':
fxy060608's avatar
fxy060608 已提交
87
              process.env.UNI_STAT_UNI_CLOUD || JSON.stringify({}),
88
            'process.env.UNI_STAT_DEBUG': process.env.UNI_STAT_DEBUG || 'false',
fxy060608's avatar
fxy060608 已提交
89 90 91
          },
        }
      },
fxy060608's avatar
fxy060608 已提交
92 93 94
      resolveId(id) {
        return stats[id] || null
      },
fxy060608's avatar
fxy060608 已提交
95
      transform(code, id) {
96
        if (isEnable && opts.filter(id)) {
fxy060608's avatar
fxy060608 已提交
97
          return {
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102
            code:
              code +
              `;import '@dcloudio/uni${
                statVersion === '2' ? '-cloud' : ''
              }-stat';`,
fxy060608's avatar
fxy060608 已提交
103 104 105 106 107 108 109
            map: null,
          }
        }
      },
    }
  }),
]