manifest.js 4.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
const path = require('path')
fxy060608's avatar
fxy060608 已提交
2 3 4
const {
  hasOwn
} = require('./util')
fxy060608's avatar
fxy060608 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

const {
  getJson,
  parseJson
} = require('./json')

const defaultRouter = {
  mode: 'hash',
  base: '/'
}

const defaultAsync = {
  loading: 'AsyncLoading',
  error: 'AsyncError',
  delay: 200,
20
  timeout: 60000
fxy060608's avatar
fxy060608 已提交
21 22 23
}

const networkTimeout = {
24 25 26 27
  request: 60000,
  connectSocket: 60000,
  uploadFile: 60000,
  downloadFile: 60000
fxy060608's avatar
fxy060608 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
}

function getManifestJson () {
  return getJson('manifest.json')
}

function parseManifestJson (content) {
  return parseJson(content)
}

function getNetworkTimeout (manifestJson) {
  if (!manifestJson) {
    manifestJson = getManifestJson()
  }
  return Object.assign({}, networkTimeout, manifestJson.networkTimeout || {})
}

function getH5Options (manifestJson) {
  if (!manifestJson) {
    manifestJson = getManifestJson()
  }

fxy060608's avatar
fxy060608 已提交
50
  const h5 = manifestJson[process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM] || {}
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55

  h5.appid = (manifestJson.appid || '').replace('__UNI__', '')

  h5.title = h5.title || manifestJson.name || ''

fxy060608's avatar
fxy060608 已提交
56
  h5.router = Object.assign({}, defaultRouter, h5.router || {})
fxy060608's avatar
fxy060608 已提交
57

fxy060608's avatar
fxy060608 已提交
58
  h5.async = Object.assign({}, defaultAsync, h5.async || {})
fxy060608's avatar
fxy060608 已提交
59 60 61

  let base = h5.router.base

62
  if (!base.startsWith('/') && !base.startsWith('./')) {
fxy060608's avatar
fxy060608 已提交
63 64
    base = '/' + base
  }
65
  if (!base.endsWith('/')) {
fxy060608's avatar
fxy060608 已提交
66 67
    base = base + '/'
  }
68 69 70 71
  // 相对路径仅支持 hash 模式
  if (base.startsWith('./')) {
    h5.router.mode = defaultRouter.mode
  }
fxy060608's avatar
fxy060608 已提交
72 73 74 75 76 77

  h5.router.base = base

  if (process.env.NODE_ENV === 'production') { // 生产模式,启用 publicPath
    h5.publicPath = h5.publicPath || base

78
    if (!h5.publicPath.endsWith('/')) {
fxy060608's avatar
fxy060608 已提交
79 80
      h5.publicPath = h5.publicPath + '/'
    }
fxy060608's avatar
fxy060608 已提交
81
  } else { // 其他模式,启用 base
82 83
    if (base.startsWith('./')) {
      // 在开发模式, publicPath 如果为 './' webpack-dev-server 匹配文件时会失败
84 85 86 87
      h5.publicPath = base.substr(1)
    } else {
      h5.publicPath = base
    }
88 89
  }

fxy060608's avatar
fxy060608 已提交
90
  /* eslint-disable no-mixed-operators */
fxy060608's avatar
fxy060608 已提交
91 92
  h5.template = h5.template && path.resolve(process.env.UNI_INPUT_DIR, h5.template) || path.resolve(require('./util')
    .getCLIContext(), 'public/index.html')
fxy060608's avatar
fxy060608 已提交
93 94 95

  h5.devServer = h5.devServer || {}

fxy060608's avatar
fxy060608 已提交
96 97 98 99 100
  // 插件修改 h5Options
  global.uniPlugin.configureH5.forEach(configureH5 => {
    configureH5(h5)
  })

fxy060608's avatar
fxy060608 已提交
101 102 103
  return h5
}

fxy060608's avatar
fxy060608 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
function isEnableUniPushV1 (manifestJson, platform) {
  if (!manifestJson) {
    manifestJson = getManifestJson()
  }
  if (isEnableUniPushV2(manifestJson, platform)) {
    return false
  }
  if (platform === 'app-plus') {
    const platformOptions = manifestJson[platform]
    const sdkConfigs = platformOptions && platformOptions.distribute && platformOptions.distribute.sdkConfigs
    const push = sdkConfigs && sdkConfigs.push
    if (push && hasOwn(push, 'unipush')) {
      return true
    }
  }
  return false
}

fxy060608's avatar
fxy060608 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
function isEnableUniPushV2 (manifestJson, platform) {
  if (!manifestJson) {
    manifestJson = getManifestJson()
  }
  const platformOptions = manifestJson[platform]
  if (platform === 'app-plus') {
    const sdkConfigs = platformOptions && platformOptions.distribute && platformOptions.distribute.sdkConfigs
    const unipush = sdkConfigs && sdkConfigs.push && sdkConfigs.push.unipush
    return (
      /* eslint-disable eqeqeq */
      unipush && unipush.version == '2'
    )
  }
  return platformOptions && platformOptions.unipush && platformOptions.unipush.enable === true
}

function isUniPushOffline (manifestJson) {
  if (!manifestJson) {
    manifestJson = getManifestJson()
  }
  const platformOptions = manifestJson['app-plus']
  const sdkConfigs = platformOptions && platformOptions.distribute && platformOptions.distribute.sdkConfigs
  const unipush = sdkConfigs && sdkConfigs.push && sdkConfigs.push.unipush
fxy060608's avatar
fxy060608 已提交
145
  return unipush && unipush.offline === true
fxy060608's avatar
fxy060608 已提交
146 147
}

fxy060608's avatar
fxy060608 已提交
148 149 150 151
module.exports = {
  getManifestJson,
  parseManifestJson,
  getNetworkTimeout,
fxy060608's avatar
fxy060608 已提交
152 153
  getH5Options,
  isEnableUniPushV1,
fxy060608's avatar
fxy060608 已提交
154 155 156
  isEnableUniPushV2,
  isUniPushOffline
}