足力健前端,vue版本

index.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const path = require('path')
  2. const isWin = /^win/.test(process.platform)
  3. const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path)
  4. function resolve(...args) {
  5. return normalizePath(path.resolve.apply(path, [__dirname, ...args]))
  6. }
  7. const srcPath = resolve('../../src/')
  8. const protocolPath = resolve('../../src/core/helpers/protocol')
  9. const coreApiPath = resolve('../../src/core/service/api')
  10. const platformApiPath = resolve('../../src/platforms/' + process.env.UNI_PLATFORM + '/service/api')
  11. const apis = require('../apis')
  12. process.UNI_SERVICE_API_MANIFEST = Object.create(null)
  13. process.UNI_SERVICE_API_PROTOCOL = Object.create(null)
  14. function parseProtocolExport({
  15. file,
  16. exports
  17. }) {
  18. const filepath = file.replace(srcPath, '')
  19. exports.forEach(exportName => {
  20. if (process.UNI_SERVICE_API_PROTOCOL[exportName]) {
  21. console.warn(`API[${exportName}] 冲突:`)
  22. console.warn(process.UNI_SERVICE_API_PROTOCOL[exportName])
  23. console.warn(filepath)
  24. } else {
  25. process.UNI_SERVICE_API_PROTOCOL[exportName] = filepath
  26. }
  27. })
  28. }
  29. function parseApiExport({
  30. file,
  31. methods,
  32. exports,
  33. isPlatform
  34. }) {
  35. const deps = []
  36. methods && methods.forEach(method => {
  37. deps.push(['', method])
  38. })
  39. const filepath = file.replace(srcPath, '')
  40. exports.forEach(exportName => {
  41. if (process.UNI_SERVICE_API_MANIFEST[exportName]) {
  42. console.warn('\n')
  43. console.warn(`API[${exportName}] 冲突:`)
  44. console.warn(process.UNI_SERVICE_API_MANIFEST[exportName][0])
  45. console.warn(filepath)
  46. if (isPlatform) { // 优先使用 platform
  47. process.UNI_SERVICE_API_MANIFEST[exportName] = [filepath, deps]
  48. console.warn(`优先使用` + filepath)
  49. }
  50. } else {
  51. process.UNI_SERVICE_API_MANIFEST[exportName] = [filepath, deps]
  52. }
  53. })
  54. }
  55. const CONTEXTS = ['VideoContext', 'MapContext', 'EditorContext', 'CanvasContext']
  56. function parseExports(node, t, file) {
  57. if (t.isFunctionDeclaration(node)) {
  58. return [node.id.name]
  59. } else if (
  60. t.isVariableDeclaration(node) &&
  61. node.declarations.length === 1
  62. ) {
  63. return [node.declarations[0].id.name]
  64. } else if (Array.isArray(node) && node.length) {
  65. return node.map(specifier => {
  66. return specifier.exported.name
  67. })
  68. } else {
  69. if (t.isClassDeclaration(node) && CONTEXTS.includes(node.id.name)) {
  70. // ignore
  71. return
  72. }
  73. console.warn('\n')
  74. console.warn(`${file} 解析 export 失败`, node)
  75. }
  76. }
  77. module.exports = function({
  78. types: t
  79. }) {
  80. return {
  81. visitor: {
  82. Program: {
  83. enter(path, state) {
  84. state.file.opts.file = normalizePath(state.file.opts.filename)
  85. state.file.opts.isCore = state.file.opts.file.indexOf(coreApiPath) === 0
  86. state.file.opts.isPlatform = state.file.opts.file.indexOf(platformApiPath) === 0
  87. state.file.opts.isProtocol = state.file.opts.file.indexOf(protocolPath) === 0
  88. },
  89. exit(path, state) {
  90. const {
  91. exports,
  92. isProtocol
  93. } = state.file.opts
  94. if (exports && exports.length) {
  95. if (isProtocol) {
  96. parseProtocolExport(state.file.opts)
  97. } else {
  98. parseApiExport(state.file.opts)
  99. }
  100. }
  101. }
  102. },
  103. ExportNamedDeclaration(path, state) {
  104. const {
  105. file,
  106. isCore,
  107. isPlatform,
  108. isProtocol
  109. } = state.file.opts
  110. if (isCore || isPlatform || isProtocol) {
  111. const exports = parseExports(path.node.declaration || path.node.specifiers, t, file)
  112. if (Array.isArray(exports)) {
  113. (state.file.opts.exports || (state.file.opts.exports = [])).push(...exports)
  114. }
  115. }
  116. },
  117. CallExpression(path, state) {
  118. const {
  119. file,
  120. isCore,
  121. isPlatform
  122. } = state.file.opts
  123. if (
  124. isCore &&
  125. path.node.callee.name === 'invokeMethod'
  126. ) {
  127. (state.file.opts.methods || (state.file.opts.methods = new Set())).add(path.node.arguments[0].value)
  128. }
  129. }
  130. }
  131. }
  132. }