manifest.js 2.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 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 52
const path = require('path')

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

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

const defaultAsync = {
  loading: 'AsyncLoading',
  error: 'AsyncError',
  delay: 200,
  timeout: 3000
}

const networkTimeout = {
  request: 6000,
  connectSocket: 6000,
  uploadFile: 6000,
  downloadFile: 6000
}

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()
  }

  const h5 = manifestJson.h5 || {}

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

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

fxy060608's avatar
fxy060608 已提交
53 54 55 56 57
  if (process.env.UNI_SUB_PLATFORM === 'mp-360') { // 360 小程序仅支持 hash 模式
    h5.router = Object.assign({}, defaultRouter)
  } else {
    h5.router = Object.assign({}, defaultRouter, h5.router || {})
  }
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

  h5['async'] = Object.assign({}, defaultAsync, h5['async'] || {})

  let base = h5.router.base

  if (base.indexOf('/') !== 0) {
    base = '/' + base
  }
  if (base.substr(-1) !== '/') {
    base = base + '/'
  }

  h5.router.base = base

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

    if (h5.publicPath.substr(-1) !== '/') {
      h5.publicPath = h5.publicPath + '/'
    }
  } else { // 其他模式,启用 base
    h5.publicPath = base
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84
  }

  if (process.env.UNI_SUB_PLATFORM === 'mp-360') {
    h5.router.base = '/'
    h5.publicPath = '/'
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  }

  /* eslint-disable no-mixed-operators */
  h5.template = h5.template && path.resolve(process.env.UNI_INPUT_DIR, h5.template) || path.resolve(__dirname,
    '../../../../public/index.html')

  h5.devServer = h5.devServer || {}

  return h5
}

module.exports = {
  getManifestJson,
  parseManifestJson,
  getNetworkTimeout,
  getH5Options
}