@@ -5,19 +5,30 @@ var beautify = require('js-beautify').js_beautify
55var normalize = require ( './normalize' )
66var hotReloadAPIPath = normalize . dep ( 'vue-hot-reload-api' )
77
8- // vue compiler module for using file-loader img src
8+ // vue compiler module for using transforming `<tag>:<attribute>` to `require`
9+ var defaultTransformToRequire = {
10+ img : 'src'
11+ }
12+ var transformToRequire = Object . assign ( { } , defaultTransformToRequire )
913var options = {
1014 modules : [ {
1115 postTransformNode ( el ) {
12- if ( el . tag === 'img' ) {
13- el . attrs && el . attrs . some ( rewrite )
16+ for ( var tag in transformToRequire ) {
17+ if ( el . tag === tag && el . attrs ) {
18+ var attributes = transformToRequire [ tag ]
19+ if ( typeof attributes === 'string' ) {
20+ el . attrs . some ( attr => rewrite ( attr , attributes ) )
21+ } else if ( Array . isArray ( attributes ) ) {
22+ attributes . forEach ( item => el . attrs . some ( attr => rewrite ( attr , item ) ) )
23+ }
24+ }
1425 }
1526 }
1627 } ]
1728}
1829
19- function rewrite ( attr ) {
20- if ( attr . name === 'src' ) {
30+ function rewrite ( attr , name ) {
31+ if ( attr . name === name ) {
2132 var value = attr . value
2233 var isStatic = value . charAt ( 0 ) === '"' && value . charAt ( value . length - 1 ) === '"'
2334 if ( ! isStatic ) {
@@ -38,6 +49,9 @@ module.exports = function (html) {
3849 this . cacheable ( )
3950 var query = loaderUtils . parseQuery ( this . query )
4051 var isServer = this . options . target === 'node'
52+ if ( this . options . vue && this . options . vue . transformToRequire ) {
53+ Object . assign ( transformToRequire , this . options . vue . transformToRequire )
54+ }
4155 var compiled = compiler . compile ( html , options )
4256 var code
4357 if ( compiled . errors . length ) {
0 commit comments