Skip to content

Commit 89cf9f3

Browse files
kazuponyyx990803
authored andcommitted
Add 'image' tag of SVG to 'transformToRequire` option as default (vuejs#579)
* add dev dependency module * add 'image' tag to 'transformToRequire' option as default Closes vuejs#569 * update doc * add fixture
1 parent 7504e28 commit 89cf9f3

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

docs/en/options.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ module.exports = {
108108
### transformToRequire
109109

110110
- type: `{ [tag: string]: string | Array<string> }`
111-
- default: `{ img: 'src' }`
111+
- default: `{ img: 'src', image: 'xlink:href' }`
112112

113-
During template compilation, the compiler can transform certain attributes, such as `src` URLs, into `require` calls, so that the target asset can be handled by Webpack. The default config transforms the `src` attribute on `<img>` tags.
113+
During template compilation, the compiler can transform certain attributes, such as `src` URLs, into `require` calls, so that the target asset can be handled by Webpack. The default config transforms the `src` attribute on `<img>` tags and `xlink:href` attribute on `<image>` tags of SVG.
114114

115115
### buble
116116

lib/template-compiler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var hotReloadAPIPath = normalize.dep('vue-hot-reload-api')
77

88
// vue compiler module for using transforming `<tag>:<attribute>` to `require`
99
var defaultTransformToRequire = {
10-
img: 'src'
10+
img: 'src',
11+
image: 'xlink:href'
1112
}
1213
var transformToRequire = defaultTransformToRequire
1314
var defaultCompileOptions = {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"stylus": "^0.54.5",
7373
"stylus-loader": "^2.0.0",
7474
"sugarss": "^0.2.0",
75+
"url-loader": "^0.5.7",
7576
"vue": "^2.1.0",
7677
"vue-template-compiler": "^2.1.0",
7778
"webpack": "^2.1.0-beta.27"

test/fixtures/transform.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<div>
3+
<img src="./logo.png">
4+
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">
5+
<image xlink:href="./logo.png" />
6+
</svg>
7+
</div>
8+
</template>
9+
10+
<style>
11+
html { background-image: url(./logo.png); }
12+
</style>

test/test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,34 @@ describe('vue-loader', function () {
330330
})
331331
})
332332

333+
it('transformToRequire option', function (done) {
334+
test({
335+
entry: './test/fixtures/transform.vue',
336+
module: {
337+
rules: [
338+
{ test: /\.vue$/, loader: loaderPath },
339+
{
340+
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
341+
loader: 'url-loader'
342+
}
343+
]
344+
}
345+
}, function (window, module) {
346+
function includeDataURL (s) {
347+
return !!s.match(/\s*data:([a-z]+\/[a-z]+(;[a-z\-]+\=[a-z\-]+)?)?(;base64)?,[a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*/i)
348+
}
349+
var vnode = mockRender(module)
350+
// img tag
351+
expect(includeDataURL(vnode.children[0].data.attrs.src)).to.equal(true)
352+
// image tag (SVG)
353+
expect(includeDataURL(vnode.children[2].children[0].data.attrs['xlink:href'])).to.equal(true)
354+
var style = window.document.querySelector('style').textContent
355+
// style
356+
expect(includeDataURL(style)).to.equal(true)
357+
done()
358+
})
359+
})
360+
333361
it('postcss options', function (done) {
334362
test({
335363
entry: './test/fixtures/postcss.vue',

0 commit comments

Comments
 (0)