get-real-path.js 834 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import getRealRoute from 'uni-helpers/get-real-route'

3
const SCHEME_RE = /^([a-z-]+:)?\/\//i
4
const DATA_RE = /^data:.*,.*/
fxy060608's avatar
fxy060608 已提交
5 6 7 8 9 10 11 12 13 14

function addBase (filePath) {
  if (__uniConfig.router.base) {
    return __uniConfig.router.base + filePath
  }
  return filePath
}

export default function getRealPath (filePath) {
  if (filePath.indexOf('/') === 0) {
15 16 17 18 19
    if (filePath.indexOf('//') === 0) {
      filePath = 'https:' + filePath
    } else {
      return addBase(filePath.substr(1))
    }
fxy060608's avatar
fxy060608 已提交
20 21
  }
  // 网络资源或base64
22
  if (SCHEME_RE.test(filePath) || DATA_RE.test(filePath) || filePath.indexOf('blob:') === 0) {
fxy060608's avatar
fxy060608 已提交
23 24 25 26 27 28 29 30 31
    return filePath
  }

  const pages = getCurrentPages()
  if (pages.length) {
    return addBase(getRealRoute(pages[pages.length - 1].$page.route, filePath).substr(1))
  }

  return filePath
32
}