Skip to content

Commit 3b1558a

Browse files
fnlctrlyyx990803
authored andcommitted
Add ability to set transformToRequire option under vue in webpack… (#316)
* Add ability to set `transformToRequire` option under `vue` in webpack config, This provides the missing feature from html-loader to specify which tag-attribute combination to be transformed to `require()`. `transformToRequire` Defaults to {img: 'src'}. Example usage: (in webpack.config.js) ```javascript vue: { transformToRequire: { foo: 'bar' } } `` will transform ``<foo bar="..."/>`` to ``<foo bar="require(...)"/>`` * fix extra semi colon * support array config for vue.transformToRequire * remove trailing spaces
1 parent c261e0a commit 3b1558a

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
@@ -4,19 +4,30 @@ var beautify = require('js-beautify').js_beautify
44
var normalize = require('./normalize')
55
var hotReloadAPIPath = normalize.dep('vue-hot-reload-api')
66

7-
// vue compiler module for using file-loader img src
7+
// vue compiler module for using transforming `<tag>:<attribute>` to `require`
8+
var defaultTransformToRequire = {
9+
img: 'src'
10+
}
11+
var transformToRequire = Object.assign({}, defaultTransformToRequire)
812
var options = {
913
modules: [{
1014
postTransformNode (el) {
11-
if (el.tag === 'img') {
12-
el.attrs && el.attrs.some(rewrite)
15+
for (var tag in transformToRequire) {
16+
if (el.tag === tag && el.attrs) {
17+
var attributes = transformToRequire[tag]
18+
if (typeof attributes === 'string') {
19+
el.attrs.some(attr => rewrite(attr, attributes))
20+
} else if (Array.isArray(attributes)) {
21+
attributes.forEach(item => el.attrs.some(attr => rewrite(attr, item)))
22+
}
23+
}
1324
}
1425
}
1526
}]
1627
}
1728

18-
function rewrite (attr) {
19-
if (attr.name === 'src') {
29+
function rewrite (attr, name) {
30+
if (attr.name === name) {
2031
var value = attr.value
2132
var isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"'
2233
if (!isStatic) {
@@ -37,6 +48,9 @@ module.exports = function (html) {
3748
this.cacheable()
3849
var query = loaderUtils.parseQuery(this.query)
3950
var isServer = this.options.target === 'node'
51+
if (this.options.vue && this.options.vue.transformToRequire) {
52+
Object.assign(transformToRequire, this.options.vue.transformToRequire)
53+
}
4054
var compiled = compiler.compile(html, options)
4155
var code
4256
if (compiled.errors.length) {

0 commit comments

Comments
 (0)