uni.compiler.js 4.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6
const path = require('path')

const t = require('@babel/types')

function generateJsCode (properties = '{}') {
  return `
fxy060608's avatar
fxy060608 已提交
7
wx.createComponent({
fxy060608's avatar
fxy060608 已提交
8
    generic:true,
fxy060608's avatar
fxy060608 已提交
9
    props: ${properties},
fxy060608's avatar
fxy060608 已提交
10 11 12 13 14
    render: function(){}
})
`
}

15
function generateCssCode (filename) {
16
  return `
17
@import "./${filename}"
18 19 20
`
}

fxy060608's avatar
fxy060608 已提交
21 22 23 24
function hasOwn (obj, key) {
  return Object.prototype.hasOwnProperty.call(obj, key)
}

fxy060608's avatar
fxy060608 已提交
25
module.exports = {
fxy060608's avatar
fxy060608 已提交
26 27
  directive: 'wx:',
  createScopedSlots (slotName, props, state) {
fxy060608's avatar
fxy060608 已提交
28 29 30
    const componentName = 'scoped-slots-' + slotName
    if (!state.componentGenerics) {
      state.componentGenerics = Object.create(null)
fxy060608's avatar
fxy060608 已提交
31
    }
fxy060608's avatar
fxy060608 已提交
32 33 34

    state.componentGenerics[componentName] = true

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    // 返回多个节点,支持作用域插槽当作普通插槽使用
    return [
      {
        type: 'slot',
        attr: {
          name: slotName
        },
        children: []
      },
      {
        type: componentName,
        attr: props || {},
        children: []
      }
    ]
fxy060608's avatar
fxy060608 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  },
  resolveScopedSlots (slotName, {
    genCode,
    generate,
    ownerName,
    parentName,
    parentNode,
    resourcePath,
    paramExprNode,
    returnExprNodes,
    traverseExpr
  }, state) {
    if (!state.scopedSlots) {
      state.scopedSlots = {}
    }
fxy060608's avatar
fxy060608 已提交
65 66 67 68
    const baseName = `${ownerName}-${parentName}-${slotName}`
    let componentName = baseName
    if (!hasOwn(state.scopedSlots, baseName)) {
      state.scopedSlots[baseName] = 0
fxy060608's avatar
fxy060608 已提交
69
    }
fxy060608's avatar
fxy060608 已提交
70 71
    if (state.scopedSlots[baseName]) {
      componentName = baseName + state.scopedSlots[baseName]
fxy060608's avatar
fxy060608 已提交
72
    }
fxy060608's avatar
fxy060608 已提交
73
    state.scopedSlots[baseName]++
fxy060608's avatar
fxy060608 已提交
74 75 76 77 78 79
    parentNode.attr['generic:scoped-slots-' + slotName] = componentName
    if (!parentNode.attr.generic) {
      parentNode.attr.generic = {}
    }
    parentNode.attr.generic[slotName] = true

80
    // 生成 scopedSlots 文件,包括 json,js,wxml,wxss,还需要更新 owner 的 usingComponents
fxy060608's avatar
fxy060608 已提交
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
    if (!state.files) {
      state.files = {}
    }
    const extname = path.extname(resourcePath)

    // TODO 需要存储 resourcePath 相关 json

    const templateFile = resourcePath.replace(ownerName + extname, componentName + extname)
    const templateContent = generate(traverseExpr(returnExprNodes, state), state)

    state.files[templateFile] = templateContent

    const jsFile = resourcePath.replace(ownerName + extname, componentName + '.js')

    const objectProperties = []

    if (t.isObjectPattern(paramExprNode)) {
      paramExprNode.properties.forEach(property => {
        const key = property.key
        const value = property.value
        const valueObjectProperties = [
          t.objectProperty(t.identifier('type'), t.nullLiteral())
        ]
        if (t.isIdentifier(value)) {
          if (value.name !== key.name) {
            state.errors.add(`解构插槽 Prop 时,不支持将${key.name}重命名为${value.name},重命名后会影响性能`)
          }
        } else if (t.isAssignmentPattern(value)) {
          valueObjectProperties.push(t.objectProperty(t.identifier('default'), value.right))
        }
        objectProperties.push(t.objectProperty(key, t.objectExpression(valueObjectProperties)))
      })
    } else {
      state.errors.add(`目前仅支持解构插槽 ${paramExprNode.name},如 v-slot="{ user }"`)
    }
    const jsContent = generateJsCode(genCode(t.objectExpression(objectProperties), true))
    state.files[jsFile] = jsContent

119 120 121
    try {
      // TODO 使用 getPlatformExts 在单元测试报错,改从 state.options.platform 判断
      const { getPlatformExts } = require('@dcloudio/uni-cli-shared')
Q
qiang 已提交
122
      const styleExtname = getPlatformExts().style
123 124
      const styleFile = resourcePath.replace(ownerName + extname, componentName + styleExtname)
      const styleContent = generateCssCode(ownerName + styleExtname)
125

126 127
      state.files[styleFile] = styleContent
    } catch (error) {}
128

fxy060608's avatar
fxy060608 已提交
129 130 131 132 133 134 135 136
    if (!state.generic) {
      state.generic = []
    }
    // 存储,方便后续生成 json
    state.generic.push(componentName)

    return ''
  }
137
}