1+ // vue compiler module for transforming `img:srcset` to a number of `require`s
2+
3+ module . exports = function ( ) {
4+ return {
5+ postTransformNode : node => {
6+ transform ( node )
7+ }
8+ }
9+ }
10+
11+ function transform ( node ) {
12+
13+ if ( node . tag == 'img' && node . attrs ) {
14+ node . attrs . forEach ( attr => {
15+ if ( attr . name == 'srcset' ) {
16+
17+ // same logic as in transform-require.js
18+ var value = attr . value
19+ var isStatic = value . charAt ( 0 ) === '"' && value . charAt ( value . length - 1 ) === '"'
20+ if ( ! isStatic ) {
21+ return
22+ }
23+
24+ // http://w3c.github.io/html/semantics-embedded-content.html#ref-for-image-candidate-string-5
25+
26+ let spaceCharacters = / [ \t \n \f \r ] + /
27+
28+ let imageCandidates = value . substr ( 1 , value . length - 2 ) . split ( ',' ) . map ( s => {
29+ let [ url , descriptor ] = s . trim ( ) . split ( spaceCharacters , 2 ) ;
30+ return { require : urlToRequire ( url ) , descriptor : descriptor } ;
31+ } )
32+
33+ let code = '' ;
34+ imageCandidates . forEach ( ( o , i , a ) => {
35+ code += o . require + ' + " ' + o . descriptor + ( i < a . length - 1 ? ',' : '' ) + '"' ;
36+ } )
37+
38+ attr . value = code ;
39+
40+ }
41+ } )
42+ }
43+
44+ }
45+
46+ function urlToRequire ( url ) {
47+ // same logic as in transform-require.js
48+ var firstChar = url . charAt ( 0 )
49+ if ( firstChar === '.' || firstChar === '~' ) {
50+ if ( firstChar === '~' ) {
51+ var secondChar = url . charAt ( 1 )
52+ url = '"' + url . slice ( secondChar === '/' ? 2 : 1 )
53+ }
54+ return 'require("' + url + '")'
55+ }
56+ }
0 commit comments