manifest.js 2.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
const path = require('path')

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

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

const defaultAsync = {
  loading: 'AsyncLoading',
  error: 'AsyncError',
  delay: 200,
17
  timeout: 60000
fxy060608's avatar
fxy060608 已提交
18 19 20
}

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

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 已提交
47
  const h5 = manifestJson[process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM] || {}
fxy060608's avatar
fxy060608 已提交
48 49 50 51 52

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

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

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

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

  let base = h5.router.base

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

  h5.router.base = base

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

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

fxy060608's avatar
fxy060608 已提交
87 88 89 90 91 92
  /* 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 || {}

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

fxy060608's avatar
fxy060608 已提交
98 99 100 101 102 103 104 105
  return h5
}

module.exports = {
  getManifestJson,
  parseManifestJson,
  getNetworkTimeout,
  getH5Options
fxy060608's avatar
fxy060608 已提交
106
}