uni.plugin.js 2.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
'use strict'
fxy060608's avatar
fxy060608 已提交
2

fxy060608's avatar
fxy060608 已提交
3 4 5 6 7 8 9 10 11 12 13
var debug = require('debug')
var uniCliShared = require('@dcloudio/uni-cli-shared')

function _interopDefaultLegacy(e) {
  return e && typeof e === 'object' && 'default' in e ? e : { default: e }
}

var debug__default = /*#__PURE__*/ _interopDefaultLegacy(debug)

var index = [
  uniCliShared.defineUniMainJsPlugin((opts) => {
fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19 20 21 22 23
    let isEnable = false
    return {
      name: 'vite:uni-stat',
      enforce: 'pre',
      config(config, env) {
        if (isSsr(env.command, config)) {
          return
        }
        const inputDir = process.env.UNI_INPUT_DIR
        const platform = process.env.UNI_PLATFORM
fxy060608's avatar
fxy060608 已提交
24 25
        isEnable =
          uniCliShared.getUniStatistics(inputDir, platform).enable === true
fxy060608's avatar
fxy060608 已提交
26
        if (process.env.NODE_ENV === 'production') {
fxy060608's avatar
fxy060608 已提交
27
          const manifestJson = uniCliShared.parseManifestJsonOnce(inputDir)
fxy060608's avatar
fxy060608 已提交
28 29
          if (!manifestJson.appid) {
            console.log()
fxy060608's avatar
fxy060608 已提交
30
            console.warn(uniCliShared.M['stat.warn.appid'])
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36
            console.log()
            isEnable = false
          }
        }
        const titlesJson = Object.create(null)
        if (isEnable) {
fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42 43 44
          uniCliShared
            .parsePagesJsonOnce(inputDir, platform)
            .pages.forEach((page) => {
              const titleText = page.style.navigationBar.titleText || ''
              if (titleText) {
                titlesJson[page.path] = titleText
              }
            })
fxy060608's avatar
fxy060608 已提交
45
        }
fxy060608's avatar
fxy060608 已提交
46
        debug__default['default']('vite:uni:stat')('isEnable', isEnable)
fxy060608's avatar
fxy060608 已提交
47
        process.env.UNI_STAT_TITLE_JSON = JSON.stringify(titlesJson)
fxy060608's avatar
fxy060608 已提交
48 49
        return {
          define: {
fxy060608's avatar
fxy060608 已提交
50
            'process.env.UNI_STAT_TITLE_JSON': process.env.UNI_STAT_TITLE_JSON,
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
          },
        }
      },
      transform(code, id) {
        if (isEnable && opts.filter(id)) {
          return {
            code: code + `;import '@dcloudio/uni-stat';`,
            map: null,
          }
        }
      },
    }
  }),
]
function isSsr(command, config) {
  if (command === 'serve') {
    return !!(config.server && config.server.middlewareMode)
  }
  if (command === 'build') {
    return !!(config.build && config.build.ssr)
  }
  return false
}
fxy060608's avatar
fxy060608 已提交
74 75

module.exports = index