mp-alipay.js 2.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
const {
fxy060608's avatar
fxy060608 已提交
2
  parsePages
3
} = require('@dcloudio/uni-cli-shared')
4 5 6

const {
  updateAppJsonUsingComponents
7
} = require('@dcloudio/uni-cli-shared/lib/cache')
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13 14 15 16

const {
  hasOwn,
  parseStyle,
  parseTabBar
} = require('../util')

const pagesJson2AppJson = {
  'globalStyle': function (name, value, json) {
fxy060608's avatar
fxy060608 已提交
17 18 19 20
    json['window'] = parseStyle(value)
    if (json['window'].usingComponents) {
      json['usingComponents'] = json['window'].usingComponents
      delete json['window']['usingComponents']
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    }
  },
  'tabBar': function (name, value, json) {
    json['tabBar'] = parseTabBar(value)
  }
}

function copyToJson (json, fromJson, options) {
  Object.keys(options).forEach(name => {
    if (hasOwn(fromJson, name)) {
      options[name](name, fromJson[name], json)
    }
  })
}

module.exports = function (pagesJson, manifestJson) {
  const app = {
fxy060608's avatar
fxy060608 已提交
38 39 40 41 42
    pages: [],
    subPackages: []
  }

  const subPackages = {}
fxy060608's avatar
fxy060608 已提交
43 44 45

  parsePages(pagesJson, function (page) {
    app.pages.push(page.path)
fxy060608's avatar
fxy060608 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  }, function (root, page, subPackage) {
    if (!subPackages[root]) {
      subPackages[root] = {
        root,
        pages: []
      }
      Object.keys(subPackage).forEach(name => {
        if (['root', 'pages'].indexOf(name) === -1) {
          subPackages[root][name] = subPackage[name]
        }
      })
    }
    subPackages[root].pages.push(page.path)
  })

  Object.keys(subPackages).forEach(root => {
    app.subPackages.push(subPackages[root])
fxy060608's avatar
fxy060608 已提交
63 64
  })

65
  copyToJson(app, pagesJson, pagesJson2AppJson)
66

67 68 69 70
  const platformJson = manifestJson['mp-alipay'] || {}
  if (hasOwn(platformJson, 'plugins')) {
    app.plugins = platformJson.plugins
  }
71

72 73
  if (app.usingComponents) {
    updateAppJsonUsingComponents(app.usingComponents)
74 75 76
  }

  const project = Object.assign({}, manifestJson['mp-alipay'] || {})
77 78
  delete project.usingComponents
  delete project.plugins
fxy060608's avatar
fxy060608 已提交
79 80 81 82

  return [{
    name: 'app',
    content: app
83 84 85
  }, {
    name: 'mini.project',
    content: project
fxy060608's avatar
fxy060608 已提交
86 87
  }]
}