mp.js 3.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
const EVENTS = {
  click: 'tap'
}
4 5 6 7 8 9 10
const tags = {
  // 小程序平台通用组件
  base: [
    'slot',
    'block',
    'component',
    'template',
fxy060608's avatar
fxy060608 已提交
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
    'ad',
    'audio',
    'button',
    'camera',
    'canvas',
    'checkbox',
    'checkbox-group',
    'cover-image',
    'cover-view',
    'form',
    'functional-page-navigator',
    'icon',
    'image',
    'input',
    'label',
    'live-player',
    'live-pusher',
    'map',
    'movable-area',
    'movable-view',
    'navigator',
    'official-account',
    'open-data',
    'picker',
    'picker-view',
    'picker-view-column',
    'progress',
    'radio',
    'radio-group',
    'rich-text',
    'scroll-view',
    'slider',
    'swiper',
    'swiper-item',
    'switch',
    'text',
    'textarea',
    'video',
    'view',
    'web-view',
Q
qiang 已提交
52
    'editor'
53 54 55 56
  ],
  // 支付宝小程序平台独有组件
  'mp-alipay': [
    'lifestyle',
57 58 59 60 61 62 63 64 65
    'life-follow',
    'contact-button',
    'spread',
    'error-view',
    'poster',
    'cashier',
    'ix-grid',
    'ix-native-grid',
    'ix-native-list'
66 67
  ]
}
fxy060608's avatar
fxy060608 已提交
68

fxy060608's avatar
fxy060608 已提交
69
const baseCompiler = {
fxy060608's avatar
fxy060608 已提交
70 71 72 73
  ref: 'data-ref',
  refInFor: 'data-ref-in-for',
  specialEvents: {},
  /**
fxy060608's avatar
fxy060608 已提交
74 75 76 77
   * TODO 暂时先简单判断是不是自定义组件,
   * 如果要依赖真实导入的组件识别,需要 template-loader 与 script-loader 结合,
   * 目前 template 在前,script 在后,要做的话,就需要把 wxml 的生成机制放到 plugin 中才可以拿到真实的组件列表
   */
fxy060608's avatar
fxy060608 已提交
78
  isComponent (tagName) {
79
    return !tags.base.concat(tags[this.name] || []).includes(tagName)
fxy060608's avatar
fxy060608 已提交
80
  },
fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
  createFilterTag (filterTag, {
    content,
    attrs
  }) {
    content = content.trim()
    if (content) {
      return `<${filterTag} module="${attrs.module}">
${content}
</${filterTag}>`
    } else if (attrs.src) {
      return `<${filterTag} src="${attrs.src}" module="${attrs.module}"></${filterTag}>`
    }
  },
fxy060608's avatar
fxy060608 已提交
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
  getEventType (eventType) {
    return EVENTS[eventType] || eventType
  },
  formatEventType (eventName, isCatch, isCapture, isCustom) {
    let eventType = 'bind'
    if (isCatch) {
      eventType = 'catch'
    }
    if (isCapture) {
      return `capture-${eventType}:${eventName}`
    }
    if (isCustom) {
      return `${eventType}:${eventName}`
    }
    return `${eventType}${eventName}` // 原生组件不支持 bind:input 等写法,统一使用 bindinput
  },
  createScopedSlots (slotName, props, state) {
    state.errors.add('暂不支持 scoped slot [' + slotName + ']')
    return {
      type: 'slot',
      attr: {
        name: slotName
      },
      children: []
    }
  },
  resolveScopedSlots (slotName, componentName, paramExprNode, returnExprNodes, {
    traverseExpr,
    normalizeChildren
  }, state) {
    state.errors.add('暂不支持 scoped slot [' + slotName + ']')
    return {
      type: 'view',
      attr: {
        slot: slotName
      },
      children: []
    }
  }
fxy060608's avatar
fxy060608 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145
}

module.exports = function getCompilerOptions (platform) {
  let id = '@dcloudio/uni-' + platform
  if (global.uniPlugin) {
    id = global.uniPlugin.id
  }
  return Object.assign({
    name: platform
  },
  baseCompiler,
  require(id + '/lib/uni.compiler.js')
  )
inkwalk's avatar
inkwalk 已提交
146
}