Skip to content

Commit 0766a00

Browse files
committed
Merge branch 'next' of github.com:vuejs/vue-loader into next
2 parents 6485908 + 3b1558a commit 0766a00

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

lib/template-compiler.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,30 @@ var beautify = require('js-beautify').js_beautify
55
var normalize = require('./normalize')
66
var 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)
913
var 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

Comments
 (0)