asset-url.js 1.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
const transformAssetUrls = {
  'audio': 'src',
  'video': ['src', 'poster'],
  'img': 'src',
  'image': 'src',
  'cover-image': 'src',
  'v-uni-audio': 'src',
  'v-uni-video': ['src', 'poster'],
  'v-uni-image': 'src',
  'v-uni-cover-image': 'src'
}

function rewrite (attr, name) {
  if (attr.name === name) {
    const value = attr.value
    // only transform static URLs
    if (value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
      attr.value = attr.value
        .replace('"@/', '"/')
        .replace('"~@/', '"/')
      return true
    }
  }
  return false
}
module.exports = {
  postTransformNode: (node) => {
    if (!node.attrs) {
      return
    }
    const attributes = transformAssetUrls[node.tag]
    if (!attributes) {
      return
    }
    if (typeof attributes === 'string') {
      node.attrs.some(attr => rewrite(attr, attributes))
    } else if (Array.isArray(attributes)) {
      attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item)))
    }
  }
}