uni_modules.js 3.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
'use strict'
var __importDefault = (this && this.__importDefault) || function (mod) {
  return (mod && mod.__esModule) ? mod : { default: mod }
}
Object.defineProperty(exports, '__esModule', { value: true })
exports.parseDefines = exports.parseExports = exports.genUniModulesExports = void 0
const path_1 = __importDefault(require('path'))
const fs_extra_1 = __importDefault(require('fs-extra'))
const merge_1 = require('merge')
function genUniModulesExports () {
  const uniModulesDir = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'uni_modules')
  if (!fs_extra_1.default.existsSync(uniModulesDir)) {
    return ''
  }
  const importCodes = []
  const assignCodes = []
  fs_extra_1.default.readdirSync(uniModulesDir).forEach((uniModuleDir) => {
    var _a, _b
    const pkgPath = path_1.default.resolve(uniModulesDir, uniModuleDir, 'package.json')
    if (!fs_extra_1.default.existsSync(pkgPath)) {
      return
    }
    const exports = (_b = (_a = JSON.parse(fs_extra_1.default.readFileSync(pkgPath, 'utf8'))) === null || _a === void 0 ? void 0 : _a.uni_modules) === null || _b === void 0 ? void 0 : _b.exports
    if (exports) {
      const [exportsImportCodes, exportsAssignCodes] = parseExports(process.env.UNI_PLATFORM === 'h5' ? 'web' : process.env.UNI_PLATFORM, `@/uni_modules/${uniModuleDir}`, exports)
      importCodes.push(...exportsImportCodes)
      assignCodes.push(...exportsAssignCodes)
    }
  })
  if (!importCodes.length) {
    return ''
  }
  return `${importCodes.join('\n')}
${assignCodes.join('\n')}`
}
exports.genUniModulesExports = genUniModulesExports
function parseExports (platform, source, exports = {}) {
  const rootDefines = {}
  Object.keys(exports).forEach((name) => {
    if (name.startsWith('uni')) {
      rootDefines[name] = exports[name]
    }
  })
  const platformDefines = exports[platform]
  // 该平台不支持
  if (platformDefines === false) {
    return [[], []]
  }
  return parseDefines(source, (0, merge_1.recursive)(true, rootDefines, platformDefines))
}
exports.parseExports = parseExports
function parseDefines (source, defines = {}) {
  const importCodes = []
  const assignCodes = []
  Object.keys(defines).forEach((name) => {
    const [defineImportCodes, defineAssignCodes] = parseDefine(source, name, defines[name])
    importCodes.push(...defineImportCodes)
    assignCodes.push(...defineAssignCodes)
  })
  return [importCodes, assignCodes]
}
exports.parseDefines = parseDefines
/**
 *  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
 */
function parseDefine (source, globalObject, define) {
  const importCodes = []
  const assignCodes = []
  if (typeof define === 'string') {
    importCodes.push(`import ${define} from '${source}'`)
    assignCodes.push(`${globalObject}.${define} = ${define}`)
  } else if (Array.isArray(define)) {
    importCodes.push(`import { ${define.join(', ')} } from '${source}'`)
    define.forEach((d) => {
      assignCodes.push(`${globalObject}.${d} = ${d}`)
    })
  } else {
    const keys = Object.keys(define)
    const specifiers = []
    keys.forEach((d) => {
      if (d !== define[d]) {
        specifiers.push(`${define[d]} as ${d}`)
      } else {
        specifiers.push(d)
      }
      assignCodes.push(`${globalObject}.${d} = ${d}`)
    })
    importCodes.push(`import { ${specifiers.join(', ')} } from '${source}'`)
  }
  return [importCodes, assignCodes]
}