asset-url.js 3.5 KB
Newer Older
1 2
const url = require('url')

fxy060608's avatar
fxy060608 已提交
3
const transformAssetUrls = {
fxy060608's avatar
fxy060608 已提交
4 5 6 7
  audio: 'src',
  video: ['src', 'poster'],
  img: 'src',
  image: 'src',
8 9 10 11 12 13 14 15 16
  'cover-image': 'src',
  // h5
  'v-uni-audio': 'src',
  'v-uni-video': ['src', 'poster'],
  'v-uni-image': 'src',
  'v-uni-cover-image': 'src',
  // nvue
  'u-image': 'src',
  'u-video': ['src', 'poster']
fxy060608's avatar
fxy060608 已提交
17 18
}

19 20 21 22 23 24 25 26 27 28 29
function urlToRequire (url) {
  const returnValue = `"${url}"`
  // same logic as in transform-require.js
  const firstChar = url.charAt(0)
  if (firstChar === '.' || firstChar === '~' || firstChar === '@') {
    if (firstChar === '~') {
      const secondChar = url.charAt(1)
      url = url.slice(secondChar === '/' ? 2 : 1)
    }
    const uriParts = parseUriParts(url)
    if (!uriParts.hash) { // fixed by xxxxxx (v3 template中需要加/)
30
      return `require("${url}")`
31 32 33 34 35
    } else { // fixed by xxxxxx (v3 template中需要加/)
      // support uri fragment case by excluding it from
      // the require and instead appending it as string;
      // assuming that the path part is sufficient according to
      // the above caseing(t.i. no protocol-auth-host parts expected)
36
      return `require("${uriParts.path}") + "${uriParts.hash}"`
37 38 39 40 41 42 43 44 45
    }
  }
  return returnValue
}
/**
 * vuejs/component-compiler-utils#22 Support uri fragment in transformed require
 * @param urlString an url as a string
 */
function parseUriParts (urlString) {
fxy060608's avatar
fxy060608 已提交
46
  // initialize return value
fxy060608's avatar
fxy060608 已提交
47
  /* eslint-disable node/no-deprecated-api */
48 49 50 51 52
  const returnValue = url.parse('')
  if (urlString) {
    // A TypeError is thrown if urlString is not a string
    // @see https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
    if (typeof urlString === 'string') {
fxy060608's avatar
fxy060608 已提交
53
      // check is an uri
fxy060608's avatar
fxy060608 已提交
54
      /* eslint-disable node/no-deprecated-api */
55 56 57 58 59 60 61
      return url.parse(urlString) // take apart the uri
    }
  }
  return returnValue
}

function rewrite (attr, name, options) {
fxy060608's avatar
fxy060608 已提交
62 63 64 65
  if (attr.name === name) {
    const value = attr.value
    // only transform static URLs
    if (value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
66 67 68 69 70
      if (!options.h5) { // 非 H5 平台
        attr.value = attr.value
          .replace('"@/', '"/')
          .replace('"~@/', '"/')
      }
fxy060608's avatar
fxy060608 已提交
71 72 73
      // v3,h5
      const needRequire = options.service || options.view || options.h5
      if (needRequire) {
74
        attr.value = urlToRequire(attr.value.slice(1, -1))
fxy060608's avatar
fxy060608 已提交
75 76 77 78 79
        if (
          options.h5 &&
          options.publicPath === './' &&
          attr.value.startsWith('require("')
        ) { // require
fxy060608's avatar
fxy060608 已提交
80
          // h5 且 publicPath 为 ./ (仅生产模式可能为./)
81
          attr.value = `(${attr.value}).replace(/^\\./,'')`
82
        }
83
      }
fxy060608's avatar
fxy060608 已提交
84 85 86 87 88
      return true
    }
  }
  return false
}
89

fxy060608's avatar
fxy060608 已提交
90
module.exports = {
91
  postTransformNode: (node, options) => {
fxy060608's avatar
fxy060608 已提交
92 93 94 95 96 97 98 99
    if (!node.attrs) {
      return
    }
    const attributes = transformAssetUrls[node.tag]
    if (!attributes) {
      return
    }
    if (typeof attributes === 'string') {
100 101 102 103 104
      if (node.attrs.some(attr => rewrite(attr, attributes, options))) {
        if (options.service || options.view) {
          node.hasBindings = true
        }
      }
fxy060608's avatar
fxy060608 已提交
105
    } else if (Array.isArray(attributes)) {
106 107 108 109 110 111 112
      attributes.forEach(item => {
        if (node.attrs.some(attr => rewrite(attr, item, options))) {
          if (options.service || options.view) {
            node.hasBindings = true
          }
        }
      })
fxy060608's avatar
fxy060608 已提交
113 114
    }
  }
115
}