index.js 4.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11
const path = require('path')

const isWin = /^win/.test(process.platform)

const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path)

function resolve(...args) {
  return normalizePath(path.resolve.apply(path, [__dirname, ...args]))
}

const srcPath = resolve('../../src/')
fxy060608's avatar
fxy060608 已提交
12
const protocolPath = resolve('../../src/core/helpers/protocol')
fxy060608's avatar
fxy060608 已提交
13 14 15 16 17 18
const coreApiPath = resolve('../../src/core/service/api')
const platformApiPath = resolve('../../src/platforms/' + process.env.UNI_PLATFORM + '/service/api')

const apis = require('../../src/core/helpers/apis')

process.UNI_SERVICE_API_MANIFEST = Object.create(null)
fxy060608's avatar
fxy060608 已提交
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
process.UNI_SERVICE_API_PROTOCOL = Object.create(null)

function parseProtocolExport({
  file,
  exports
}) {
  const filepath = file.replace(srcPath, '')
  exports.forEach(exportName => {
    if (process.UNI_SERVICE_API_PROTOCOL[exportName]) {
      console.warn(`API[${exportName}] 冲突:`)
      console.warn(process.UNI_SERVICE_API_PROTOCOL[exportName])
      console.warn(filepath)
    } else {
      process.UNI_SERVICE_API_PROTOCOL[exportName] = filepath
    }
  })
}

function parseApiExport({
  file,
  methods,
  exports,
  isPlatform
}) {
  const deps = []
  methods && methods.forEach(method => {
    deps.push(['', method])
  })
  const filepath = file.replace(srcPath, '')
  exports.forEach(exportName => {
    if (process.UNI_SERVICE_API_MANIFEST[exportName]) {
      console.warn('\n')
      console.warn(`API[${exportName}] 冲突:`)
      console.warn(process.UNI_SERVICE_API_MANIFEST[exportName][0])
      console.warn(filepath)
      if (isPlatform) { //  优先使用 platform
        process.UNI_SERVICE_API_MANIFEST[exportName] = [filepath, deps]
        console.warn(`优先使用` + filepath)
      }
    } else {
      process.UNI_SERVICE_API_MANIFEST[exportName] = [filepath, deps]
    }
  })
}

function parseExports(node, t, file) {
  if (t.isFunctionDeclaration(node)) {
    return [node.id.name]
  } else if (
    t.isVariableDeclaration(node) &&
    node.declarations.length === 1
  ) {
    return [node.declarations[0].id.name]
  } else if (Array.isArray(node) && node.length) {
    return node.map(specifier => {
      return specifier.exported.name
    })
  } else {
    console.warn('\n')
    console.warn(`${file} 解析 export 失败`, node)
  }
}
fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86 87 88 89 90 91

module.exports = function({
  types: t
}) {
  return {
    visitor: {
      Program: {
        enter(path, state) {
          state.file.opts.file = normalizePath(state.file.opts.filename)
          state.file.opts.isCore = state.file.opts.file.indexOf(coreApiPath) === 0
          state.file.opts.isPlatform = state.file.opts.file.indexOf(platformApiPath) === 0
fxy060608's avatar
fxy060608 已提交
92
          state.file.opts.isProtocol = state.file.opts.file.indexOf(protocolPath) === 0
fxy060608's avatar
fxy060608 已提交
93 94 95 96
        },
        exit(path, state) {
          const {
            exports,
fxy060608's avatar
fxy060608 已提交
97
            isProtocol
fxy060608's avatar
fxy060608 已提交
98 99
          } = state.file.opts
          if (exports && exports.length) {
fxy060608's avatar
fxy060608 已提交
100 101 102 103 104
            if (isProtocol) {
              parseProtocolExport(state.file.opts)
            } else {
              parseApiExport(state.file.opts)
            }
fxy060608's avatar
fxy060608 已提交
105 106 107 108 109 110 111
          }
        }
      },
      ExportNamedDeclaration(path, state) {
        const {
          file,
          isCore,
fxy060608's avatar
fxy060608 已提交
112 113
          isPlatform,
          isProtocol
fxy060608's avatar
fxy060608 已提交
114
        } = state.file.opts
fxy060608's avatar
fxy060608 已提交
115 116 117 118 119
        if (isCore || isPlatform || isProtocol) {
          const exports = parseExports(path.node.declaration || path.node.specifiers, t, file)
          if (Array.isArray(exports)) {
            (state.file.opts.exports || (state.file.opts.exports = [])).push(...exports)
          }
fxy060608's avatar
fxy060608 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        }
      },
      CallExpression(path, state) {
        const {
          file,
          isCore,
          isPlatform
        } = state.file.opts

        if (
          isCore &&
          path.node.callee.name === 'invokeMethod'
        ) {
          (state.file.opts.methods || (state.file.opts.methods = new Set())).add(path.node.arguments[0].value)
        }
      }
    }
  }
}