manifestJson.ts 2.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import type { 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
  initI18nOptions,
fxy060608's avatar
fxy060608 已提交
8
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
9 10 11 12

const defaultRouter = {
  mode: 'hash',
  base: '/',
13
  assets: 'assets',
fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19 20 21 22 23
}

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

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

fxy060608's avatar
fxy060608 已提交
45
        const networkTimeout = normalizeNetworkTimeout(manifest.networkTimeout)
fxy060608's avatar
fxy060608 已提交
46 47 48 49

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

        const qqMapKey =
Q
qiang 已提交
50 51 52 53 54 55
          sdkConfigs.maps && sdkConfigs.maps.qqmap && sdkConfigs.maps.qqmap.key

        const googleMapKey =
          sdkConfigs.maps &&
          sdkConfigs.maps.google &&
          sdkConfigs.maps.google.key
fxy060608's avatar
fxy060608 已提交
56

Q
qiang 已提交
57 58
        let locale: string | null | undefined = manifest.locale
        locale = locale && locale.toUpperCase() !== 'AUTO' ? locale : ''
Q
qiang 已提交
59

fxy060608's avatar
fxy060608 已提交
60 61 62 63 64 65 66 67
        const i18nOptions = initI18nOptions(
          process.env.UNI_PLATFORM,
          process.env.UNI_INPUT_DIR,
          false,
          false
        )
        const fallbackLocale = (i18nOptions && i18nOptions.locale) || ''

fxy060608's avatar
fxy060608 已提交
68
        const flexDirection =
fxy060608's avatar
fxy060608 已提交
69 70 71
          (manifest['app'] &&
            manifest['app'].nvue &&
            manifest['app'].nvue['flex-direction']) ||
fxy060608's avatar
fxy060608 已提交
72 73 74 75
          'column'

        return {
          code: `export const appid = '${appid || ''}'    
fxy060608's avatar
fxy060608 已提交
76 77 78 79 80 81 82 83
  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)}
Q
qiang 已提交
84 85
  export const qqMapKey = ${JSON.stringify(qqMapKey)}
  export const googleMapKey = ${JSON.stringify(googleMapKey)}
fxy060608's avatar
fxy060608 已提交
86
  export const sdkConfigs = ${JSON.stringify(sdkConfigs)}
Q
qiang 已提交
87
  export const locale = '${locale}'
fxy060608's avatar
fxy060608 已提交
88
  export const fallbackLocale = '${fallbackLocale}'
fxy060608's avatar
fxy060608 已提交
89
  `,
fxy060608's avatar
fxy060608 已提交
90 91
          map: { mappings: '' },
        }
fxy060608's avatar
fxy060608 已提交
92 93 94
      },
    }
  })
fxy060608's avatar
fxy060608 已提交
95
}