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

const {
fxy060608's avatar
init v3  
fxy060608 已提交
7
  parsePages,
fxy060608's avatar
fxy060608 已提交
8 9 10 11
  normalizePath,
  getFlexDirection
} = require('@dcloudio/uni-cli-shared')

fxy060608's avatar
fxy060608 已提交
12 13 14 15
const {
  addPageUsingComponents
} = require('@dcloudio/uni-cli-shared/lib/pages')

fxy060608's avatar
fxy060608 已提交
16 17 18 19
const {
  parseStyle
} = require('../../util')

fxy060608's avatar
fxy060608 已提交
20
const definePages = require('./define-pages')
21 22 23
const appConfigService = require('./app-config-service')

const wxPageOrientationMapping = {
fxy060608's avatar
fxy060608 已提交
24 25 26
  auto: ['portrait-primary', 'portrait-secondary', 'landscape-primary', 'landscape-secondary'],
  portrait: ['portrait-primary', 'portrait-secondary'],
  landscape: ['landscape-primary', 'landscape-secondary']
27
}
fxy060608's avatar
init v3  
fxy060608 已提交
28

fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
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 = {
      request: 6000,
      connectSocket: 6000,
      uploadFile: 6000,
      downloadFile: 6000
    }
  } else {
    if (typeof appJson.networkTimeout.request === 'undefined') {
      appJson.networkTimeout.request = 6000
    }
    if (typeof appJson.networkTimeout.connectSocket === 'undefined') {
      appJson.networkTimeout.connectSocket = 6000
    }
    if (typeof appJson.networkTimeout.uploadFile === 'undefined') {
      appJson.networkTimeout.uploadFile = 6000
    }
    if (typeof appJson.networkTimeout.downloadFile === 'undefined') {
      appJson.networkTimeout.downloadFile = 6000
    }
  }
}

module.exports = function (pagesJson, userManifestJson) {
  const {
    app
  } = require('../mp')(pagesJson, userManifestJson)

  const manifest = {
    name: 'manifest'
  }

  const appJson = app.content

  const {
    navigationBarTextStyle = 'white',
    navigationBarBackgroundColor = '#000000'
  } = appJson['window'] || {}
fxy060608's avatar
fxy060608 已提交
81

82
  const TABBAR_HEIGHT = 50
fxy060608's avatar
fxy060608 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

  let manifestJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, './manifest.json'), 'utf8'))

  // 状态栏
  manifestJson.plus.statusbar = {
    immersed: 'supportedDevice',
    style: navigationBarTextStyle === 'black' ? 'dark' : 'light',
    background: navigationBarBackgroundColor
  }
  // 用户配置覆盖默认配置
  manifestJson = merge.recursive(
    true,
    manifestJson, {
      id: userManifestJson.appid || '',
      name: userManifestJson.name || '',
      description: userManifestJson.description || '',
      version: {
        name: userManifestJson.versionName,
        code: userManifestJson.versionCode
      }
    }, {
      plus: userManifestJson['app-plus']
    }
  )

  const splashscreenOptions = userManifestJson['app-plus'] && userManifestJson['app-plus']['splashscreen']

  const hasAlwaysShowBeforeRender = splashscreenOptions && splashscreenOptions.hasOwnProperty(
    'alwaysShowBeforeRender')

  // 转换为老版本配置
  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
    }
  }

136
  // 屏幕启动方向
fxy060608's avatar
fxy060608 已提交
137
  if (manifestJson.plus.screenOrientation) { // app平台优先使用 manifest 配置
138 139
    manifestJson.screenOrientation = manifestJson.plus.screenOrientation
    delete manifestJson.plus.screenOrientation
fxy060608's avatar
fxy060608 已提交
140
  } else if (appJson.window && appJson.window.pageOrientation) { // 兼容微信小程序
141 142 143 144
    const pageOrientationValue = wxPageOrientationMapping[appJson.window.pageOrientation]
    if (pageOrientationValue) {
      manifestJson.screenOrientation = pageOrientationValue
    }
145 146
  }

fxy060608's avatar
fxy060608 已提交
147 148 149 150 151 152 153 154 155 156 157
  // 地图坐标系
  if (manifestJson.permissions && manifestJson.permissions.Maps) {
    manifestJson.permissions.Maps.coordType = 'gcj02'
  }

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

  const nvuePages = pagesJson.nvue && pagesJson.nvue.pages

fxy060608's avatar
fxy060608 已提交
158
  if (nvuePages && nvuePages.length) {
fxy060608's avatar
fxy060608 已提交
159
    const pages = {}
fxy060608's avatar
fxy060608 已提交
160 161 162 163 164 165
    nvuePages.forEach(({
      path,
      style
    }) => {
      pages[path] = {
        'window': parseStyle(style),
fxy060608's avatar
fxy060608 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        'nvue': true
      }
    })

    appJson.nvue = {
      pages
    }

    if (pagesJson.nvue.entryPagePath) {
      appJson.nvue.entryPagePath = pagesJson.nvue.entryPagePath
    }
    // nvue 权限
    manifestJson.permissions.UniNView = {
      'description': 'UniNView原生渲染'
    }
  } else if (process.env.UNI_USING_V8) {
    // nvue 权限
    manifestJson.permissions.UniNView = {
      'description': 'UniNView原生渲染'
    }
  }

  // 启动页面配置
  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

207 208 209 210 211 212 213 214 215 216 217
  const addRenderAlways = function () {
    // "render": "always"
    if (!manifestJson.plus.launchwebview) {
      manifestJson.plus.launchwebview = {
        'render': 'always'
      }
    } else if (!manifestJson.plus.launchwebview.render) {
      manifestJson.plus.launchwebview.render = 'always'
    }
  }

fxy060608's avatar
fxy060608 已提交
218
  if (appJson.tabBar && appJson.tabBar.list && appJson.tabBar.list.length) {
fxy060608's avatar
fxy060608 已提交
219
    // 安全区配置 仅包含 tabBar 的时候才配置
fxy060608's avatar
fxy060608 已提交
220 221 222 223 224 225 226 227
    if (!manifestJson.plus.safearea) {
      manifestJson.plus.safearea = {
        background: appJson.tabBar.backgroundColor || '#FFFFFF',
        bottom: {
          offset: 'auto'
        }
      }
    }
228 229
    if (!process.env.UNI_USING_COMPONENTS) { // 非自定义组件模式下,仍旧添加 render always
      addRenderAlways()
fxy060608's avatar
fxy060608 已提交
230
    }
231 232
  } else {
    addRenderAlways()
fxy060608's avatar
fxy060608 已提交
233 234 235 236
  }

  let flexDir = false

fxy060608's avatar
fxy060608 已提交
237
  if (process.env.UNI_USING_NVUE_COMPILER) {
fxy060608's avatar
fxy060608 已提交
238 239
    appJson.nvueCompiler = 'uni-app'
    flexDir = getFlexDirection(manifestJson.plus)
240 241
  } else {
    appJson.nvueCompiler = 'weex'
fxy060608's avatar
fxy060608 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  }

  if (manifestJson.plus.renderer === 'native') {
    appJson.renderer = 'native'
  } else {
    appJson.renderer = 'auto'
  }

  const nvueCompilerFilePath = path.resolve(process.env.UNI_OUTPUT_DIR, '__uniappnvuecompiler.js')
  const nvueCompilerExists = fs.existsSync(nvueCompilerFilePath)

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

  const rendererFilePath = path.resolve(process.env.UNI_OUTPUT_DIR, '__uniapprenderer.js')
  const rendererExists = fs.existsSync(rendererFilePath)

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

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

  // 强制白屏检测
  if (manifestJson.plus.splashscreen) {
    if (!hasAlwaysShowBeforeRender && manifestJson.plus.splashscreen.autoclose === false) { // 兼容旧版本仅配置了 autoclose 为 false
      manifestJson.plus.splashscreen.alwaysShowBeforeRender = false
    }
    if (manifestJson.plus.splashscreen.alwaysShowBeforeRender) { // 白屏检测
      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
    } else { // 不启用白屏检测
      delete manifestJson.plus.splashscreen.target

      if (manifestJson.plus.splashscreen.autoclose) { // 启用 uni-app 框架关闭 splash
        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 已提交
329 330 331 332 333 334 335 336 337 338
  // 检查原生混淆选项
  const confusion = manifestJson.plus.confusion
  if (confusion && confusion.resources) {
    const resources = {}
    for (const key in confusion.resources) {
      if (!/\.nvue$/.test(key)) {
        throw new Error(`原生混淆仅支持 nvue 页面,错误的页面路径:${key}`)
      } else {
        resources[key.replace(/\.nvue$/, '.js')] = confusion.resources[key]
      }
Q
qiang 已提交
339 340
      if (!Object.keys(appJson.nvue.pages).find(path => {
        const subNVues = appJson.nvue.pages[path].window.subNVues || []
fxy060608's avatar
fxy060608 已提交
341 342 343 344 345 346
        return path.replace(/\.html$/, '.nvue') === key || subNVues.find(({
          path
        }) => path === key.replace(/\.nvue$/, ''))
      }) && !pagesJson.pages.find(({
        style = {}
      }) => {
Q
qiang 已提交
347 348
        style = Object.assign(style, style['app-plus'])
        const subNVues = style.subNVues || []
fxy060608's avatar
fxy060608 已提交
349 350 351
        return subNVues.find(({
          path
        }) => path === key.replace(/\.nvue$/, ''))
Q
qiang 已提交
352
      })) {
Q
qiang 已提交
353 354 355 356 357 358
        throw new Error(`原生混淆页面未在项目内使用,错误的页面路径:${key}`)
      }
    }
    confusion.resources = resources
  }

fxy060608's avatar
fxy060608 已提交
359 360 361 362
  // uni-app
  const uniApp = require('../../../package.json')['uni-app']
  manifestJson.plus['uni-app'] = uniApp
  // 控制页类型
fxy060608's avatar
fxy060608 已提交
363 364
  const control = process.env.UNI_USING_V3 ? 'uni-v3' : (process.env.UNI_USING_V8 ? 'v8' : 'webview')
  manifestJson.plus['uni-app'].control = control
fxy060608's avatar
fxy060608 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  manifestJson.plus['uni-app'].nvueCompiler = appJson.nvueCompiler
  manifestJson.plus['uni-app'].renderer = appJson.renderer
  if (flexDir) {
    manifestJson.plus['uni-app'].nvue = {
      'flex-direction': flexDir
    }
  }

  // 记录编译器版本号
  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
        }
      } catch (e) {}
    }

    let isNVueEntryPage = appJson.nvue && appJson.nvue.entryPagePath
Q
qiang 已提交
389
    conditionPagePath = process.env.UNI_CLI_LAUNCH_PAGE_PATH || conditionPagePath
390
    if (conditionPagePath && appJson.nvue) {
Q
qiang 已提交
391
      isNVueEntryPage = `${conditionPagePath}.html` in appJson.nvue.pages
fxy060608's avatar
fxy060608 已提交
392
    }
Q
qiang 已提交
393 394
    manifestJson.plus.useragent.value = 'uni-app'
    manifestJson.launch_path = '__uniappview.html'
395 396 397 398 399
    Object.assign(manifestJson.plus.launchwebview, {
      id: '1',
      kernel: 'WKWebview',
      'uni-app': 'auto'
    })
fxy060608's avatar
fxy060608 已提交
400
    if (process.env.UNI_USING_NATIVE) {
401
      appJson.entryPagePath = appJson.nvue.entryPagePath
fxy060608's avatar
fxy060608 已提交
402 403 404
      // networkTimeout
      normalizeNetworkTimeout(appJson)
      appJson.page = Object.create(null)
fxy060608's avatar
fxy060608 已提交
405
      appJson.pages = Object.keys(appJson.nvue.pages).map(pagePath => {
fxy060608's avatar
fxy060608 已提交
406 407 408 409 410 411 412 413 414
        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 已提交
415

Q
qiang 已提交
416 417 418 419
      delete manifestJson.plus.launchwebview.kernel
      manifestJson.launch_path = ''
      Object.assign(manifestJson.plus.launchwebview, {
        uniNView: {
420
          path: appJson.entryPagePath
Q
qiang 已提交
421 422
        }
      })
423 424 425
    } else if (isNVueEntryPage) {
      // 非纯 nvue 项目首页为 nvue 页面
      manifestJson.plus.launchwebview.id = '2'
Q
qiang 已提交
426
      manifestJson.plus.launchwebview.render = 'always'
Q
qiang 已提交
427 428 429
    }
    // 带 tab
    if (pagesJson.tabBar && pagesJson.tabBar.list && pagesJson.tabBar.list.length) {
Q
qiang 已提交
430
      const tabBar = manifestJson.plus.tabBar = Object.assign({}, pagesJson.tabBar)
Q
qiang 已提交
431
      const borderStyles = {
432 433
        black: 'rgba(0,0,0,0.4)',
        white: 'rgba(255,255,255,0.4)'
Q
qiang 已提交
434 435 436
      }
      let borderStyle = tabBar.borderStyle
      if (!borderStyle) {
437
        borderStyle = 'black'
Q
qiang 已提交
438 439 440 441
      }
      if (borderStyle in borderStyles) {
        tabBar.borderStyle = borderStyles[borderStyle]
      }
442 443 444
      if (!tabBar.selectedColor) {
        tabBar.selectedColor = '#0062cc'
      }
445
      tabBar.height = `${parseFloat(tabBar.height) || TABBAR_HEIGHT}px`
Q
qiang 已提交
446 447
      // 非纯 nvue 项目首页为 nvue 页面
      if (!process.env.UNI_USING_NATIVE && isNVueEntryPage) {
Q
qiang 已提交
448 449 450
        manifestJson.plus.launchwebview.id = '2'
      } else {
        // 首页是 tabBar 页面
fxy060608's avatar
fxy060608 已提交
451 452
        const item = tabBar.list.find(page => page.pagePath === (process.env.UNI_USING_NATIVE ? appJson.entryPagePath
          : entryPagePath))
Q
qiang 已提交
453 454 455
        if (item) {
          tabBar.child = ['lauchwebview']
          tabBar.selected = tabBar.list.indexOf(item)
fxy060608's avatar
fxy060608 已提交
456 457 458 459 460
        }
      }
    }
  }

461 462 463 464
  if (!process.env.UNI_USING_COMPONENTS) {
    manifestJson.plus.launchwebview.kernel = 'UIWebview'
  }

fxy060608's avatar
fxy060608 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  manifest.content = manifestJson

  // 分包合并
  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)))
        })
      }
    })
  }

  delete appJson.subPackages

fxy060608's avatar
fxy060608 已提交
480
  // TODO 处理纯原生
fxy060608's avatar
fxy060608 已提交
481 482 483 484 485
  if (process.env.UNI_USING_NATIVE) {
    manifest.name = 'manifest.json'
    manifest.content = JSON.stringify(manifest.content)
    return [manifest, parseConfig(appJson)]
  }
fxy060608's avatar
fxy060608 已提交
486 487 488

  if (process.env.UNI_USING_V3) { // v3
    appJson.entryPagePath = appJson.pages[0]
489 490
    // timeout
    normalizeNetworkTimeout(appJson)
fxy060608's avatar
init v3  
fxy060608 已提交
491 492
    appJson.page = Object.create(null)

fxy060608's avatar
fxy060608 已提交
493
    const addPage = function (pagePath, windowOptions, nvue) {
fxy060608's avatar
fxy060608 已提交
494 495
      // 缓存页面级usingComponents
      addPageUsingComponents(pagePath, windowOptions.usingComponents)
fxy060608's avatar
init v3  
fxy060608 已提交
496 497
      delete windowOptions.usingComponents
      appJson.page[pagePath] = {
fxy060608's avatar
fxy060608 已提交
498 499
        window: windowOptions,
        nvue
fxy060608's avatar
init v3  
fxy060608 已提交
500 501
      }
    }
502
    parsePages(pagesJson, function (page) {
fxy060608's avatar
fxy060608 已提交
503
      addPage(page.path, parseStyle(page.style), !!page.nvue)
504
    }, function (root, page) {
fxy060608's avatar
fxy060608 已提交
505
      addPage(normalizePath(path.join(root, page.path)), parseStyle(page.style, root), !!page.nvue)
fxy060608's avatar
init v3  
fxy060608 已提交
506
    })
fxy060608's avatar
fxy060608 已提交
507 508 509 510 511
    // nvue 权限
    manifestJson.permissions.UniNView = {
      'description': 'UniNView原生渲染'
    }
    // TODO 需要考虑 condition
Q
qiang 已提交
512 513
    manifestJson.plus.launchwebview.id = '1' // 首页 id 固定 为 1
    // 删除首页 style 中的 uni-app 配置(不注入 app-view.js)
fxy060608's avatar
fxy060608 已提交
514
    delete manifestJson.plus.launchwebview['uni-app']
fxy060608's avatar
fxy060608 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527

    if (appJson.page[appJson.entryPagePath].nvue) { // 首页是 nvue
      manifestJson.launch_path = '' // 首页地址为空
      manifestJson.plus.launchwebview.uniNView = {
        path: appJson.entryPagePath
      }
      if (manifestJson.plus.tabBar) {
        manifestJson.plus.tabBar.child = ['lauchwebview']
      }
    } else {
      manifestJson.plus.launch_path = '__uniappview.html' // 首页地址固定
    }

fxy060608's avatar
init v3  
fxy060608 已提交
528 529
    manifest.name = 'manifest.json'
    manifest.content = JSON.stringify(manifest.content)
fxy060608's avatar
fxy060608 已提交
530 531
    delete appJson.nvue
    return [manifest, definePages(appJson), appConfigService(appJson)]
fxy060608's avatar
init v3  
fxy060608 已提交
532
  }
fxy060608's avatar
fxy060608 已提交
533
  return [app, manifest]
fxy060608's avatar
fxy060608 已提交
534
}