Blame view

node_modules/mpvue-loader/lib/template-compiler/modules/transform-require.js 1.23 KB
ce4c83ff   wxy   初始提交
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
42
43
44
45
46
47
48
49
50
  // vue compiler module for transforming `<tag>:<attribute>` to `require`
  
  var defaultOptions = {
    img: 'src',
    image: 'xlink:href'
  }
  
  module.exports = userOptions => {
    var options = userOptions
      ? Object.assign({}, defaultOptions, userOptions)
      : defaultOptions
  
    return {
      postTransformNode: node => {
        transform(node, options)
      }
    }
  }
  
  function transform (node, options) {
    for (var tag in options) {
      if (node.tag === tag && node.attrs) {
        var attributes = options[tag]
        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)))
        }
      }
    }
  }
  
  function rewrite (attr, name) {
    if (attr.name === name) {
      var value = attr.value
      var isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"'
      if (!isStatic) {
        return
      }
      var firstChar = value.charAt(1)
      if (firstChar === '.' || firstChar === '~') {
        if (firstChar === '~') {
          var secondChar = value.charAt(2)
          value = '"' + value.slice(secondChar === '/' ? 3 : 2)
        }
        attr.value = `require(${value})`
      }
      return true
    }
  }