manifestJson.ts 2.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { Plugin } from 'vite'
fxy060608's avatar
fxy060608 已提交
2

fxy060608's avatar
fxy060608 已提交
3 4 5
import {
  defineUniManifestJsonPlugin,
  normalizeNetworkTimeout,
fxy060608's avatar
fxy060608 已提交
6
  parseJson,
fxy060608's avatar
fxy060608 已提交
7
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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

const defaultAsync = {
  loading: 'AsyncLoading',
  error: 'AsyncError',
  delay: 200,
  timeout: 60000,
  suspensible: true,
}

const defaultQQMapKey = 'XVXBZ-NDMC4-JOGUS-XGIEE-QVHDZ-AMFV2'

fxy060608's avatar
fxy060608 已提交
24
export function uniManifestJsonPlugin(): Plugin {
fxy060608's avatar
fxy060608 已提交
25 26 27 28 29 30 31 32
  return defineUniManifestJsonPlugin((opts) => {
    return {
      name: 'vite:uni-h5-manifest-json',
      enforce: 'pre',
      transform(code, id) {
        if (!opts.filter(id)) {
          return
        }
fxy060608's avatar
fxy060608 已提交
33
        const manifest = parseJson(code)
fxy060608's avatar
fxy060608 已提交
34 35 36
        const { debug, h5 } = manifest
        const appid = (manifest.appid || '').replace('__UNI__', '')
        const router = { ...defaultRouter, ...((h5 && h5.router) || {}) }
fxy060608's avatar
fxy060608 已提交
37 38 39
        if (!router.base) {
          router.base = '/'
        }
fxy060608's avatar
fxy060608 已提交
40
        const async = { ...defaultAsync, ...((h5 && h5.async) || {}) }
fxy060608's avatar
fxy060608 已提交
41

fxy060608's avatar
fxy060608 已提交
42
        const networkTimeout = normalizeNetworkTimeout(manifest.networkTimeout)
fxy060608's avatar
fxy060608 已提交
43 44 45 46 47 48 49 50 51 52

        const sdkConfigs = (h5 && h5.sdkConfigs) || {}

        const qqMapKey =
          (sdkConfigs.maps &&
            sdkConfigs.maps.qqmap &&
            sdkConfigs.maps.qqmap.key) ||
          defaultQQMapKey

        const flexDirection =
fxy060608's avatar
fxy060608 已提交
53 54 55
          (manifest['app'] &&
            manifest['app'].nvue &&
            manifest['app'].nvue['flex-direction']) ||
fxy060608's avatar
fxy060608 已提交
56 57 58 59
          'column'

        return {
          code: `export const appid = '${appid || ''}'    
fxy060608's avatar
fxy060608 已提交
60 61 62 63 64 65 66 67 68 69 70
  export const debug = ${!!debug}
  export const nvue = ${JSON.stringify({
    'flex-direction': flexDirection,
  })}
  export const networkTimeout = ${JSON.stringify(networkTimeout)}
  // h5
  export const router = ${JSON.stringify(router)}
  export const async = ${JSON.stringify(async)}
  export const qqMapKey = '${qqMapKey}'
  export const sdkConfigs = ${JSON.stringify(sdkConfigs)}
  `,
fxy060608's avatar
fxy060608 已提交
71 72
          map: { mappings: '' },
        }
fxy060608's avatar
fxy060608 已提交
73 74 75
      },
    }
  })
fxy060608's avatar
fxy060608 已提交
76
}