uni_modules.ts 3.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import path from 'path'
import fs from 'fs-extra'
import { recursive } from 'merge'

fxy060608's avatar
fxy060608 已提交
5
type Define = string | string[] | Record<string, string> | false
fxy060608's avatar
fxy060608 已提交
6 7 8 9 10 11 12 13
type Defines = {
  [name: string]: Define
}

interface Exports {
  [name: string]: Define | Defines | false
}

fxy060608's avatar
fxy060608 已提交
14
export function parseUniExtApis(vite = true) {
fxy060608's avatar
fxy060608 已提交
15 16
  const uniModulesDir = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules')
  if (!fs.existsSync(uniModulesDir)) {
fxy060608's avatar
fxy060608 已提交
17
    return {}
fxy060608's avatar
fxy060608 已提交
18
  }
fxy060608's avatar
fxy060608 已提交
19 20

  const injects: Injects = {}
fxy060608's avatar
fxy060608 已提交
21
  fs.readdirSync(uniModulesDir).forEach((uniModuleDir) => {
fxy060608's avatar
fxy060608 已提交
22 23 24 25
    // 必须以 uni- 开头
    if (!uniModuleDir.startsWith('uni-')) {
      return
    }
fxy060608's avatar
fxy060608 已提交
26 27 28 29
    const pkgPath = path.resolve(uniModulesDir, uniModuleDir, 'package.json')
    if (!fs.existsSync(pkgPath)) {
      return
    }
fxy060608's avatar
fxy060608 已提交
30 31 32
    const exports = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))?.uni_modules?.[
      'uni-ext-api'
    ] as Exports | undefined
fxy060608's avatar
fxy060608 已提交
33
    if (exports) {
fxy060608's avatar
fxy060608 已提交
34 35 36
      Object.assign(
        injects,
        parseInjects(
fxy060608's avatar
fxy060608 已提交
37
          vite,
fxy060608's avatar
fxy060608 已提交
38
          process.env.UNI_PLATFORM === 'h5' ? 'web' : process.env.UNI_PLATFORM,
fxy060608's avatar
fxy060608 已提交
39 40 41 42
          `@/uni_modules/${uniModuleDir}` +
            (vite || !process.env.UNI_UTS_PLATFORM
              ? ''
              : `/utssdk/${process.env.UNI_UTS_PLATFORM}/index`),
fxy060608's avatar
fxy060608 已提交
43 44
          exports
        )
fxy060608's avatar
fxy060608 已提交
45 46 47
      )
    }
  })
fxy060608's avatar
fxy060608 已提交
48
  return injects
fxy060608's avatar
fxy060608 已提交
49 50
}

fxy060608's avatar
fxy060608 已提交
51 52 53
type Inject = string | string[]
type Injects = {
  [name: string]: string | string[] | false
fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
}
/**
 *  uni:'getBatteryInfo'
 * import getBatteryInfo from '..'
 *
 * uni:['getBatteryInfo']
 * import { getBatteryInfo } from '..'
 *
 * uni:['openLocation','chooseLocation']
 * import { openLocation, chooseLocation } from '..'
 *
 * uni:{
 *  onUserCaptureScreen: "onCaptureScreen"
 *  offUserCaptureScreen: "offCaptureScreen"
 * }
 *
 * uni.getBatteryInfo = getBatteryInfo
 * @param source
 * @param globalObject
 * @param define
 * @returns
 */
fxy060608's avatar
fxy060608 已提交
76
export function parseInjects(
fxy060608's avatar
fxy060608 已提交
77
  vite = true,
fxy060608's avatar
fxy060608 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  platform: UniApp.PLATFORM,
  source: string,
  exports: Exports = {}
) {
  let rootDefines: Defines = {}
  Object.keys(exports).forEach((name) => {
    if (name.startsWith('uni')) {
      rootDefines[name] = exports[name] as Inject
    }
  })
  const platformDefines = exports[platform] as false | Defines
  // 该平台不支持
  if (platformDefines === false) {
    return {}
  }
  if (platformDefines) {
    rootDefines = recursive(true, rootDefines, platformDefines)
  }
  const injects: Injects = {}
  for (const key in rootDefines) {
fxy060608's avatar
fxy060608 已提交
98
    Object.assign(injects, parseInject(vite, source, 'uni', rootDefines[key]))
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103
  }
  return injects
}

export function parseInject(
fxy060608's avatar
fxy060608 已提交
104
  vite = true,
fxy060608's avatar
fxy060608 已提交
105 106 107
  source: string,
  globalObject: string,
  define: Define
fxy060608's avatar
fxy060608 已提交
108 109 110 111 112
) {
  const injects: Injects = {}
  if (define === false) {
  } else if (typeof define === 'string') {
    // {'uni.getBatteryInfo' : '@dcloudio/uni-getbatteryinfo'}
fxy060608's avatar
fxy060608 已提交
113
    injects[globalObject + '.' + define] = vite ? source : [source, 'default']
fxy060608's avatar
fxy060608 已提交
114
  } else if (Array.isArray(define)) {
fxy060608's avatar
fxy060608 已提交
115
    // {'uni.getBatteryInfo' : ['@dcloudio/uni-getbatteryinfo','getBatteryInfo]}
fxy060608's avatar
fxy060608 已提交
116
    define.forEach((d) => {
fxy060608's avatar
fxy060608 已提交
117
      injects[globalObject + '.' + d] = [source, d]
fxy060608's avatar
fxy060608 已提交
118
    })
fxy060608's avatar
fxy060608 已提交
119
  } else {
fxy060608's avatar
fxy060608 已提交
120 121
    const keys = Object.keys(define)
    keys.forEach((d) => {
fxy060608's avatar
fxy060608 已提交
122
      injects[globalObject + '.' + d] = [source, define[d]]
fxy060608's avatar
fxy060608 已提交
123 124
    })
  }
fxy060608's avatar
fxy060608 已提交
125
  return injects
fxy060608's avatar
fxy060608 已提交
126
}