index.js 19.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5
const fs = require('fs')
const fsExtra = require('fs-extra')
const path = require('path')
const merge = require('merge')

fxy060608's avatar
fxy060608 已提交
6 7 8 9
const {
  normalizePath,
  getFlexDirection
} = require('@dcloudio/uni-cli-shared')
D
DCloud_LXH 已提交
10 11 12 13 14
const {
  getTheme,
  hasTheme,
  parseTheme
} = require('@dcloudio/uni-cli-shared/lib/theme')
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21 22 23 24
const {
  compileI18nJsonStr
} = require('@dcloudio/uni-i18n')
const {
  initI18nOptions
} = require('@dcloudio/uni-cli-shared/lib/i18n')
const {
  hasOwn,
  parseStyle
} = require('../../util')
fxy060608's avatar
fxy060608 已提交
25

fxy060608's avatar
fxy060608 已提交
26
const wxPageOrientationMapping = {
fxy060608's avatar
fxy060608 已提交
27 28 29 30 31 32
  auto: [
    'portrait-primary',
    'portrait-secondary',
    'landscape-primary',
    'landscape-secondary'
  ],
fxy060608's avatar
fxy060608 已提交
33 34 35
  portrait: ['portrait-primary', 'portrait-secondary'],
  landscape: ['landscape-primary', 'landscape-secondary']
}
fxy060608's avatar
init v3  
fxy060608 已提交
36

fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
function parseConfig (appJson) {
  return {
    name: 'app-config.js',
    content: `__registerConfig(${JSON.stringify(appJson)});`
  }
}

const _toString = Object.prototype.toString

function isPlainObject (obj) {
  return _toString.call(obj) === '[object Object]'
}

function normalizeNetworkTimeout (appJson) {
  if (!isPlainObject(appJson.networkTimeout)) {
    appJson.networkTimeout = {
53 54 55 56
      request: 60000,
      connectSocket: 60000,
      uploadFile: 60000,
      downloadFile: 60000
fxy060608's avatar
fxy060608 已提交
57 58 59
    }
  } else {
    if (typeof appJson.networkTimeout.request === 'undefined') {
60
      appJson.networkTimeout.request = 60000
fxy060608's avatar
fxy060608 已提交
61 62
    }
    if (typeof appJson.networkTimeout.connectSocket === 'undefined') {
63
      appJson.networkTimeout.connectSocket = 60000
fxy060608's avatar
fxy060608 已提交
64 65
    }
    if (typeof appJson.networkTimeout.uploadFile === 'undefined') {
66
      appJson.networkTimeout.uploadFile = 60000
fxy060608's avatar
fxy060608 已提交
67 68
    }
    if (typeof appJson.networkTimeout.downloadFile === 'undefined') {
69
      appJson.networkTimeout.downloadFile = 60000
fxy060608's avatar
fxy060608 已提交
70 71 72 73
    }
  }
}

fxy060608's avatar
fxy060608 已提交
74 75
function updateFileFlag (appJson) {
  // 已经不再根据文件识别,理论可废弃此处的逻辑
fxy060608's avatar
fxy060608 已提交
76
  if (process.env.UNI_USING_V3 || process.env.UNI_USING_V3_NATIVE) {
fxy060608's avatar
fxy060608 已提交
77 78
    return
  }
fxy060608's avatar
fxy060608 已提交
79 80 81 82
  const nvueCompilerFilePath = path.resolve(
    process.env.UNI_OUTPUT_DIR,
    '__uniappnvuecompiler.js'
  )
fxy060608's avatar
fxy060608 已提交
83 84 85 86 87 88 89 90 91 92 93 94
  const nvueCompilerExists = fs.existsSync(nvueCompilerFilePath)

  if (appJson.nvueCompiler === 'uni-app') {
    if (!nvueCompilerExists) {
      fsExtra.outputFile(nvueCompilerFilePath, '')
    }
  } else {
    if (nvueCompilerExists) {
      fs.unlinkSync(nvueCompilerFilePath)
    }
  }

fxy060608's avatar
fxy060608 已提交
95 96 97 98
  const rendererFilePath = path.resolve(
    process.env.UNI_OUTPUT_DIR,
    '__uniapprenderer.js'
  )
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111
  const rendererExists = fs.existsSync(rendererFilePath)

  if (appJson.renderer === 'native') {
    if (!rendererExists) {
      fsExtra.outputFile(rendererFilePath, '')
    }
  } else {
    if (rendererExists) {
      fs.unlinkSync(rendererFilePath)
    }
  }
}

D
DCloud_LXH 已提交
112 113 114
function _initTheme (appJson, userManifestJson) {
  const manifestJson = userManifestJson[process.env.UNI_PLATFORM] || {}
  appJson.darkmode = manifestJson.darkmode || false
D
DCloud_LXH 已提交
115
  const themeLocation = manifestJson.themeLocation || 'theme.json'
D
DCloud_LXH 已提交
116 117 118 119 120 121
  if (themeLocation && hasTheme(themeLocation)) {
    appJson.themeConfig = getTheme()
  }
  return appJson
}

122
module.exports = function (pagesJson, userManifestJson, isAppView) {
fxy060608's avatar
fxy060608 已提交
123 124 125
  const {
    app
  } = require('../mp')(pagesJson, userManifestJson)
fxy060608's avatar
fxy060608 已提交
126 127 128 129 130 131 132

  const manifest = {
    name: 'manifest'
  }

  const appJson = app.content

D
DCloud_LXH 已提交
133 134
  _initTheme(appJson, userManifestJson)

fxy060608's avatar
fxy060608 已提交
135 136 137
  const {
    navigationBarTextStyle = 'white',
    navigationBarBackgroundColor = '#000000'
D
DCloud_LXH 已提交
138
  } = parseTheme(appJson.window) || {}
fxy060608's avatar
fxy060608 已提交
139

140
  const TABBAR_HEIGHT = 50
fxy060608's avatar
fxy060608 已提交
141

fxy060608's avatar
fxy060608 已提交
142 143 144
  let manifestJson = JSON.parse(
    fs.readFileSync(path.resolve(__dirname, './manifest.json'), 'utf8')
  )
fxy060608's avatar
fxy060608 已提交
145 146 147 148 149 150 151 152 153 154

  // 状态栏
  manifestJson.plus.statusbar = {
    immersed: 'supportedDevice',
    style: navigationBarTextStyle === 'black' ? 'dark' : 'light',
    background: navigationBarBackgroundColor
  }
  // 用户配置覆盖默认配置
  manifestJson = merge.recursive(
    true,
fxy060608's avatar
fxy060608 已提交
155
    manifestJson, {
fxy060608's avatar
fxy060608 已提交
156 157 158 159 160 161
      id: userManifestJson.appid || '',
      name: userManifestJson.name || '',
      description: userManifestJson.description || '',
      version: {
        name: userManifestJson.versionName,
        code: userManifestJson.versionCode
Q
qiang 已提交
162
      },
163 164
      locale: userManifestJson.locale,
      uniStatistics: userManifestJson.uniStatistics
fxy060608's avatar
fxy060608 已提交
165
    }, {
fxy060608's avatar
fxy060608 已提交
166 167 168 169
      plus: userManifestJson['app-plus']
    }
  )

fxy060608's avatar
fxy060608 已提交
170
  initUniStatistics(manifestJson)
171

fxy060608's avatar
fxy060608 已提交
172 173
  const splashscreenOptions =
    userManifestJson['app-plus'] && userManifestJson['app-plus'].splashscreen
fxy060608's avatar
fxy060608 已提交
174

fxy060608's avatar
fxy060608 已提交
175 176 177
  const hasAlwaysShowBeforeRender =
    splashscreenOptions &&
    hasOwn(splashscreenOptions, 'alwaysShowBeforeRender')
fxy060608's avatar
fxy060608 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

  // 转换为老版本配置
  if (manifestJson.plus.modules) {
    manifestJson.permissions = manifestJson.plus.modules
    delete manifestJson.plus.modules
  }

  const distribute = manifestJson.plus.distribute

  if (distribute) {
    if (distribute.android) {
      manifestJson.plus.distribute.google = distribute.android
      delete manifestJson.plus.distribute.android
    }
    if (distribute.ios) {
      manifestJson.plus.distribute.apple = distribute.ios
      delete manifestJson.plus.distribute.ios
    }
    if (distribute.sdkConfigs) {
      manifestJson.plus.distribute.plugins = distribute.sdkConfigs
      delete manifestJson.plus.distribute.sdkConfigs
    }
200 201 202 203 204 205 206 207 208
    if (manifestJson.plus.darkmode) {
      if (!(distribute.google || (distribute.google = {})).defaultNightMode) {
        distribute.google.defaultNightMode = 'auto'
      }

      if (!(distribute.apple || (distribute.apple = {})).UIUserInterfaceStyle) {
        distribute.apple.UIUserInterfaceStyle = 'Automatic'
      }
    }
fxy060608's avatar
fxy060608 已提交
209 210
  }

211
  // 屏幕启动方向
fxy060608's avatar
fxy060608 已提交
212 213
  if (manifestJson.plus.screenOrientation) {
    // app平台优先使用 manifest 配置
214 215
    manifestJson.screenOrientation = manifestJson.plus.screenOrientation
    delete manifestJson.plus.screenOrientation
fxy060608's avatar
fxy060608 已提交
216 217 218 219
  } else if (appJson.window && appJson.window.pageOrientation) {
    // 兼容微信小程序
    const pageOrientationValue =
      wxPageOrientationMapping[appJson.window.pageOrientation]
220 221 222
    if (pageOrientationValue) {
      manifestJson.screenOrientation = pageOrientationValue
    }
223 224
  }

Q
qiang 已提交
225 226 227
  // 全屏配置
  manifestJson.fullscreen = manifestJson.plus.fullscreen

fxy060608's avatar
fxy060608 已提交
228 229 230 231 232 233 234 235
  // 地图坐标系
  if (manifestJson.permissions && manifestJson.permissions.Maps) {
    manifestJson.permissions.Maps.coordType = 'gcj02'
  }

  if (!manifestJson.permissions) {
    manifestJson.permissions = {}
  }
236

fxy060608's avatar
fxy060608 已提交
237 238 239
  const nvuePages = process.env.UNI_USING_V3_NATIVE
    ? pagesJson.pages
    : pagesJson.nvue && pagesJson.nvue.pages
fxy060608's avatar
fxy060608 已提交
240

fxy060608's avatar
fxy060608 已提交
241
  if (nvuePages && nvuePages.length) {
fxy060608's avatar
fxy060608 已提交
242
    const pages = {}
fxy060608's avatar
fxy060608 已提交
243 244 245 246
    nvuePages.forEach(({
      path,
      style
    }) => {
fxy060608's avatar
fxy060608 已提交
247
      pages[path] = {
fxy060608's avatar
fxy060608 已提交
248 249
        window: parseStyle(style),
        nvue: true
fxy060608's avatar
fxy060608 已提交
250 251 252 253 254 255
      }
    })

    appJson.nvue = {
      pages
    }
256 257 258 259 260 261

    if (process.env.UNI_USING_V3_NATIVE) {
      appJson.nvue.entryPagePath = nvuePages[0]
    } else if (pagesJson.nvue.entryPagePath) {
      appJson.nvue.entryPagePath = pagesJson.nvue.entryPagePath
    }
fxy060608's avatar
fxy060608 已提交
262 263 264

    // nvue 权限
    manifestJson.permissions.UniNView = {
fxy060608's avatar
fxy060608 已提交
265
      description: 'UniNView原生渲染'
fxy060608's avatar
fxy060608 已提交
266 267 268 269
    }
  } else if (process.env.UNI_USING_V8) {
    // nvue 权限
    manifestJson.permissions.UniNView = {
fxy060608's avatar
fxy060608 已提交
270
      description: 'UniNView原生渲染'
fxy060608's avatar
fxy060608 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    }
  }

  // 启动页面配置
  if (process.env.NODE_ENV === 'development') {
    const condition = pagesJson.condition
    if (condition && Array.isArray(condition.list) && condition.list.length) {
      const list = condition.list
      let current = parseInt(condition.current) || 0
      if (current < 0) {
        current = 0
      }
      if (current >= list.length) {
        current = 0
      }
      manifestJson.plus.arguments = JSON.stringify(list[current])
    }
  }

  // 允许内联播放视频
  manifestJson.plus.allowsInlineMediaPlayback = true

293 294 295 296
  const addRenderAlways = function () {
    // "render": "always"
    if (!manifestJson.plus.launchwebview) {
      manifestJson.plus.launchwebview = {
fxy060608's avatar
fxy060608 已提交
297
        render: 'always'
298 299 300 301 302 303
      }
    } else if (!manifestJson.plus.launchwebview.render) {
      manifestJson.plus.launchwebview.render = 'always'
    }
  }

fxy060608's avatar
fxy060608 已提交
304
  if (appJson.tabBar && appJson.tabBar.list && appJson.tabBar.list.length) {
fxy060608's avatar
fxy060608 已提交
305
    // 安全区配置 仅包含 tabBar 的时候才配置
fxy060608's avatar
fxy060608 已提交
306 307
    if (!manifestJson.plus.safearea) {
      manifestJson.plus.safearea = {
D
DCloud_LXH 已提交
308
        background: parseTheme(appJson.tabBar).backgroundColor || '#FFFFFF',
fxy060608's avatar
fxy060608 已提交
309 310 311 312 313
        bottom: {
          offset: 'auto'
        }
      }
    }
fxy060608's avatar
fxy060608 已提交
314 315
    if (!process.env.UNI_USING_COMPONENTS) {
      // 非自定义组件模式下,仍旧添加 render always
316
      addRenderAlways()
fxy060608's avatar
fxy060608 已提交
317
    }
318 319
  } else {
    addRenderAlways()
fxy060608's avatar
fxy060608 已提交
320 321 322 323
  }

  let flexDir = false

fxy060608's avatar
fxy060608 已提交
324
  if (process.env.UNI_USING_NVUE_COMPILER) {
fxy060608's avatar
fxy060608 已提交
325 326
    appJson.nvueCompiler = 'uni-app'
    flexDir = getFlexDirection(manifestJson.plus)
327 328
  } else {
    appJson.nvueCompiler = 'weex'
fxy060608's avatar
fxy060608 已提交
329 330
  }

fxy060608's avatar
fxy060608 已提交
331 332 333
  appJson.nvueStyleCompiler = process.env.UNI_USING_NVUE_STYLE_COMPILER
    ? 'uni-app'
    : 'weex'
334

335
  if (manifestJson.plus.renderer === 'native') {
fxy060608's avatar
fxy060608 已提交
336 337 338 339 340
    appJson.renderer = 'native'
  } else {
    appJson.renderer = 'auto'
  }

fxy060608's avatar
fxy060608 已提交
341
  updateFileFlag(appJson)
fxy060608's avatar
fxy060608 已提交
342 343 344 345 346 347 348 349

  appJson.splashscreen = {
    alwaysShowBeforeRender: false, // 是否启用白屏检测 关闭 splash
    autoclose: false // 是否 uni-app 框架关闭 splash
  }

  // 强制白屏检测
  if (manifestJson.plus.splashscreen) {
fxy060608's avatar
fxy060608 已提交
350 351 352 353 354
    if (
      !hasAlwaysShowBeforeRender &&
      manifestJson.plus.splashscreen.autoclose === false
    ) {
      // 兼容旧版本仅配置了 autoclose 为 false
fxy060608's avatar
fxy060608 已提交
355 356
      manifestJson.plus.splashscreen.alwaysShowBeforeRender = false
    }
fxy060608's avatar
fxy060608 已提交
357 358
    if (manifestJson.plus.splashscreen.alwaysShowBeforeRender) {
      // 白屏检测
fxy060608's avatar
fxy060608 已提交
359 360 361 362 363 364 365
      if (!manifestJson.plus.splashscreen.target) {
        manifestJson.plus.splashscreen.target = 'id:1'
      }
      manifestJson.plus.splashscreen.autoclose = true
      manifestJson.plus.splashscreen.delay = 0

      appJson.splashscreen.alwaysShowBeforeRender = true
fxy060608's avatar
fxy060608 已提交
366 367
    } else {
      // 不启用白屏检测
fxy060608's avatar
fxy060608 已提交
368 369
      delete manifestJson.plus.splashscreen.target

fxy060608's avatar
fxy060608 已提交
370 371
      if (manifestJson.plus.splashscreen.autoclose) {
        // 启用 uni-app 框架关闭 splash
fxy060608's avatar
fxy060608 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
        manifestJson.plus.splashscreen.autoclose = false // 原 5+ autoclose 改为 false
        appJson.splashscreen.autoclose = true
      }
    }
    delete manifestJson.plus.splashscreen.alwaysShowBeforeRender
  }

  appJson.appname = manifestJson.name

  if (!manifestJson.plus.distribute) {
    manifestJson.plus.distribute = {
      plugins: {}
    }
  }

  if (!manifestJson.plus.distribute.plugins) {
    manifestJson.plus.distribute.plugins = {}
  }

  // 录音支持 mp3
  manifestJson.plus.distribute.plugins.audio = {
    mp3: {
      description: 'Android平台录音支持MP3格式文件'
    }
  }

  // 有效值为 close,none
  if (!['close', 'none'].includes(manifestJson.plus.popGesture + '')) {
    manifestJson.plus.popGesture = 'close'
  }

Q
qiang 已提交
403 404 405
  // 检查原生混淆选项
  const confusion = manifestJson.plus.confusion
  if (confusion && confusion.resources) {
fxy060608's avatar
fxy060608 已提交
406
    const resources = {}
fxy060608's avatar
fxy060608 已提交
407
    const nvuePages = (appJson.nvue && appJson.nvue.pages) || {}
Q
qiang 已提交
408
    for (const key in confusion.resources) {
fxy060608's avatar
fxy060608 已提交
409 410
      if (path.extname(key) === '.js') {
        // 支持 js 混淆,过滤掉
411
        // 静态 js 文件
fxy060608's avatar
fxy060608 已提交
412 413 414 415 416
        if (
          key.indexOf('hybrid/html') === 0 ||
          key.indexOf('static/') === 0 ||
          key.indexOf('/static/') !== -1
        ) {
417 418
          resources[key] = confusion.resources[key]
        }
fxy060608's avatar
fxy060608 已提交
419 420
        continue
      }
Q
qiang 已提交
421 422 423 424 425
      if (!/\.nvue$/.test(key)) {
        throw new Error(`原生混淆仅支持 nvue 页面,错误的页面路径:${key}`)
      } else {
        resources[key.replace(/\.nvue$/, '.js')] = confusion.resources[key]
      }
fxy060608's avatar
fxy060608 已提交
426 427 428 429 430 431 432
      if (
        !Object.keys(nvuePages).find(path => {
          const subNVues = nvuePages[path].window.subNVues || []
          // TODO
          return (
            path.replace(/\.html$/, '.nvue') === key ||
            path.replace(/\.html$/, '.nvue') + '.nvue' === key ||
fxy060608's avatar
fxy060608 已提交
433 434 435
            subNVues.find(({
              path
            }) => path === key.replace(/\.nvue$/, ''))
fxy060608's avatar
fxy060608 已提交
436 437
          )
        }) &&
fxy060608's avatar
fxy060608 已提交
438 439 440
        !pagesJson.pages.find(({
          style = {}
        }) => {
fxy060608's avatar
fxy060608 已提交
441 442 443
          style = Object.assign(style, style['app-plus'])
          const subNVues = style.subNVues || []
          return subNVues.find(
fxy060608's avatar
fxy060608 已提交
444 445 446
            ({
              path
            }) => path === key.replace(/\.nvue$/, '')
fxy060608's avatar
fxy060608 已提交
447 448 449
          )
        })
      ) {
Q
qiang 已提交
450 451 452 453 454 455
        throw new Error(`原生混淆页面未在项目内使用,错误的页面路径:${key}`)
      }
    }
    confusion.resources = resources
  }

fxy060608's avatar
fxy060608 已提交
456 457 458 459
  // uni-app
  const uniApp = require('../../../package.json')['uni-app']
  manifestJson.plus['uni-app'] = uniApp
  // 控制页类型
fxy060608's avatar
fxy060608 已提交
460 461 462 463 464 465
  const control =
    process.env.UNI_USING_V3 || process.env.UNI_USING_V3_NATIVE
      ? 'uni-v3'
      : process.env.UNI_USING_V8
        ? 'v8'
        : 'webview'
fxy060608's avatar
fxy060608 已提交
466

fxy060608's avatar
fxy060608 已提交
467
  manifestJson.plus['uni-app'].control = control
fxy060608's avatar
fxy060608 已提交
468
  manifestJson.plus['uni-app'].nvueCompiler = appJson.nvueCompiler
469 470 471 472 473
  // v3 + native 时强制 auto
  manifestJson.plus['uni-app'].renderer = process.env.UNI_USING_V3_NATIVE
    ? 'auto'
    : appJson.renderer

fxy060608's avatar
fxy060608 已提交
474 475 476 477 478 479
  if (flexDir) {
    manifestJson.plus['uni-app'].nvue = {
      'flex-direction': flexDir
    }
  }

480
  // 检查 webview 版本 || 下载 X5 后启动
fxy060608's avatar
fxy060608 已提交
481
  const plusWebview = manifestJson.plus.webView
482 483
  if (plusWebview) {
    manifestJson.plus['uni-app'].webView = plusWebview
fxy060608's avatar
fxy060608 已提交
484
    delete manifestJson.plus.webView
485 486
  }

fxy060608's avatar
fxy060608 已提交
487 488 489 490 491 492 493 494 495 496 497 498
  // 记录编译器版本号
  appJson.compilerVersion = uniApp.compilerVersion

  if (process.env.UNI_USING_V8) {
    let entryPagePath = appJson.pages[0]
    let conditionPagePath = false
    if (manifestJson.plus.arguments) {
      try {
        const args = JSON.parse(manifestJson.plus.arguments)
        if (args && (args.path || args.pathName)) {
          entryPagePath = conditionPagePath = args.path || args.pathName
        }
D
DCloud_LXH 已提交
499
      } catch (e) { }
fxy060608's avatar
fxy060608 已提交
500 501 502
    }

    let isNVueEntryPage = appJson.nvue && appJson.nvue.entryPagePath
fxy060608's avatar
fxy060608 已提交
503 504
    conditionPagePath =
      process.env.UNI_CLI_LAUNCH_PAGE_PATH || conditionPagePath
505
    if (conditionPagePath && appJson.nvue) {
Q
qiang 已提交
506
      isNVueEntryPage = `${conditionPagePath}.html` in appJson.nvue.pages
fxy060608's avatar
fxy060608 已提交
507
    }
Q
qiang 已提交
508 509
    manifestJson.plus.useragent.value = 'uni-app'
    manifestJson.launch_path = '__uniappview.html'
510 511 512 513 514
    Object.assign(manifestJson.plus.launchwebview, {
      id: '1',
      kernel: 'WKWebview',
      'uni-app': 'auto'
    })
fxy060608's avatar
fxy060608 已提交
515
    if (process.env.UNI_USING_NATIVE) {
516
      appJson.entryPagePath = appJson.nvue.entryPagePath
fxy060608's avatar
fxy060608 已提交
517 518 519
      // networkTimeout
      normalizeNetworkTimeout(appJson)
      appJson.page = Object.create(null)
fxy060608's avatar
fxy060608 已提交
520
      appJson.pages = Object.keys(appJson.nvue.pages).map(pagePath => {
fxy060608's avatar
fxy060608 已提交
521 522 523 524 525 526 527 528 529
        const newPagePath = pagePath.replace('.html', '')
        appJson.page[newPagePath] = {
          window: appJson.nvue.pages[pagePath].window,
          nvue: true
        }
        return newPagePath
      })

      delete appJson.nvue
fxy060608's avatar
fxy060608 已提交
530

Q
qiang 已提交
531 532 533 534
      delete manifestJson.plus.launchwebview.kernel
      manifestJson.launch_path = ''
      Object.assign(manifestJson.plus.launchwebview, {
        uniNView: {
535
          path: appJson.entryPagePath
Q
qiang 已提交
536 537
        }
      })
538 539 540
    } else if (isNVueEntryPage) {
      // 非纯 nvue 项目首页为 nvue 页面
      manifestJson.plus.launchwebview.id = '2'
Q
qiang 已提交
541
      manifestJson.plus.launchwebview.render = 'always'
Q
qiang 已提交
542 543
    }
    // 带 tab
fxy060608's avatar
fxy060608 已提交
544 545 546 547 548
    if (
      pagesJson.tabBar &&
      pagesJson.tabBar.list &&
      pagesJson.tabBar.list.length
    ) {
fxy060608's avatar
fxy060608 已提交
549
      const tabBar = (manifestJson.plus.tabBar = Object.assign({},
D
DCloud_LXH 已提交
550
        parseTheme(pagesJson.tabBar)
fxy060608's avatar
fxy060608 已提交
551
      ))
Q
qiang 已提交
552
      const borderStyles = {
553 554
        black: 'rgba(0,0,0,0.4)',
        white: 'rgba(255,255,255,0.4)'
Q
qiang 已提交
555 556 557
      }
      let borderStyle = tabBar.borderStyle
      if (!borderStyle) {
558
        borderStyle = 'black'
Q
qiang 已提交
559 560 561 562
      }
      if (borderStyle in borderStyles) {
        tabBar.borderStyle = borderStyles[borderStyle]
      }
563 564 565
      if (!tabBar.selectedColor) {
        tabBar.selectedColor = '#0062cc'
      }
566
      tabBar.height = `${parseFloat(tabBar.height) || TABBAR_HEIGHT}px`
Q
qiang 已提交
567 568
      // 非纯 nvue 项目首页为 nvue 页面
      if (!process.env.UNI_USING_NATIVE && isNVueEntryPage) {
Q
qiang 已提交
569 570 571
        manifestJson.plus.launchwebview.id = '2'
      } else {
        // 首页是 tabBar 页面
fxy060608's avatar
fxy060608 已提交
572 573 574
        const item = tabBar.list.find(
          page =>
            page.pagePath ===
D
DCloud_LXH 已提交
575 576 577
            (process.env.UNI_USING_NATIVE
              ? appJson.entryPagePath
              : entryPagePath)
fxy060608's avatar
fxy060608 已提交
578
        )
Q
qiang 已提交
579 580 581
        if (item) {
          tabBar.child = ['lauchwebview']
          tabBar.selected = tabBar.list.indexOf(item)
fxy060608's avatar
fxy060608 已提交
582 583
        }
      }
fxy060608's avatar
fxy060608 已提交
584 585 586 587 588 589 590 591

      const i18nOptions = initI18nOptions(
        process.env.UNI_PLATFORM,
        process.env.UNI_INPUT_DIR,
        true,
        true
      )
      if (i18nOptions) {
fxy060608's avatar
fxy060608 已提交
592 593
        manifestJson = JSON.parse(
          compileI18nJsonStr(JSON.stringify(manifestJson), i18nOptions)
fxy060608's avatar
fxy060608 已提交
594 595 596
        )
        manifestJson.fallbackLocale = i18nOptions.locale
      }
fxy060608's avatar
fxy060608 已提交
597 598 599
    }
  }

600 601 602 603
  if (!process.env.UNI_USING_COMPONENTS) {
    manifestJson.plus.launchwebview.kernel = 'UIWebview'
  }

fxy060608's avatar
fxy060608 已提交
604 605
  manifest.content = manifestJson

fxy060608's avatar
fxy060608 已提交
606
  const subPackages = []
fxy060608's avatar
fxy060608 已提交
607 608 609 610 611 612 613
  // 分包合并
  if (appJson.subPackages && appJson.subPackages.length) {
    appJson.subPackages.forEach(subPackage => {
      if (subPackage.pages && subPackage.pages.length) {
        subPackage.pages.forEach(page => {
          appJson.pages.push(normalizePath(path.join(subPackage.root, page)))
        })
fxy060608's avatar
fxy060608 已提交
614 615 616
        subPackages.push({
          root: subPackage.root
        })
fxy060608's avatar
fxy060608 已提交
617 618 619 620 621 622
      }
    })
  }

  delete appJson.subPackages

fxy060608's avatar
fxy060608 已提交
623
  // TODO 处理纯原生
fxy060608's avatar
fxy060608 已提交
624 625 626 627 628
  if (process.env.UNI_USING_NATIVE) {
    manifest.name = 'manifest.json'
    manifest.content = JSON.stringify(manifest.content)
    return [manifest, parseConfig(appJson)]
  }
fxy060608's avatar
fxy060608 已提交
629

fxy060608's avatar
fxy060608 已提交
630
  if (process.env.UNI_USING_V3 || process.env.UNI_USING_V3_NATIVE) {
631
    if (process.env.UNI_USING_V3 && process.env.UNI_OPT_SUBPACKAGES) {
fxy060608's avatar
fxy060608 已提交
632 633
      appJson.subPackages = subPackages
    }
fxy060608's avatar
fxy060608 已提交
634 635
    return require('./index.v3')(
      appJson,
fxy060608's avatar
fxy060608 已提交
636
      manifestJson, {
fxy060608's avatar
fxy060608 已提交
637 638 639 640 641 642
        manifest,
        pagesJson,
        normalizeNetworkTimeout
      },
      isAppView
    )
fxy060608's avatar
init v3  
fxy060608 已提交
643
  }
fxy060608's avatar
fxy060608 已提交
644
  return [app, manifest]
fxy060608's avatar
fxy060608 已提交
645 646 647 648 649 650 651 652 653 654 655 656
}

function initUniStatistics (manifestJson) {
  // 根节点配置了统计
  if (manifestJson.uniStatistics) {
    manifestJson.plus.uniStatistics = merge.recursive(
      true,
      manifestJson.uniStatistics,
      manifestJson.plus.uniStatistics
    )
    delete manifestJson.uniStatistics
  }
657
  if (!process.env.UNI_CLOUD_PROVIDER) {
fxy060608's avatar
fxy060608 已提交
658 659 660 661
    return
  }
  let spaces = []
  try {
662
    spaces = JSON.parse(process.env.UNI_CLOUD_PROVIDER)
D
DCloud_LXH 已提交
663
  } catch (e) { }
fxy060608's avatar
fxy060608 已提交
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
  if (!Array.isArray(spaces) || !spaces.length) {
    return
  }
  const space = spaces[0]
  if (!space) {
    return
  }
  const uniStatistics = manifestJson.plus && manifestJson.plus.uniStatistics
  if (!uniStatistics) {
    return
  }
  if (uniStatistics.version === 2 || uniStatistics.version === '2') {
    if (uniStatistics.uniCloud && uniStatistics.uniCloud.spaceId) {
      return
    }
    uniStatistics.uniCloud = {
      provider: space.provider,
681
      spaceId: space.spaceId,
fxy060608's avatar
fxy060608 已提交
682
      clientSecret: space.clientSecret,
683
      endpoint: space.endpoint
fxy060608's avatar
fxy060608 已提交
684 685
    }
  }
686
}