'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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/')
const protocolPath = resolve('../../src/core/helpers/protocol')
const coreApiPath = resolve('../../src/core/service/api')
const platformApiPath = resolve('../../src/platforms/' + process.env.UNI_PLATFORM + '/service/api')
 
const apis = require('../apis')
 
process.UNI_SERVICE_API_MANIFEST = Object.create(null)
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]
    }
  })
}
 
const CONTEXTS = ['VideoContext', 'MapContext', 'EditorContext', 'CanvasContext']
 
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 {
    if (t.isClassDeclaration(node) && CONTEXTS.includes(node.id.name)) {
      // ignore
      return
    }
    console.warn('\n')
    console.warn(`${file} 解析 export 失败`, node)
  }
}
 
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
          state.file.opts.isProtocol = state.file.opts.file.indexOf(protocolPath) === 0
        },
        exit(path, state) {
          const {
            exports,
            isProtocol
          } = state.file.opts
          if (exports && exports.length) {
            if (isProtocol) {
              parseProtocolExport(state.file.opts)
            } else {
              parseApiExport(state.file.opts)
            }
          }
        }
      },
      ExportNamedDeclaration(path, state) {
        const {
          file,
          isCore,
          isPlatform,
          isProtocol
        } = state.file.opts
        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)
          }
        }
      },
      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)
        }
      }
    }
  }
}