/
index.js
74 lines (59 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
/*!
* ss2vue
* https://github.com/SnakeskinTpl/ss2vue
*
* Released under the MIT license
* https://github.com/SnakeskinTpl/ss2vue/blob/master/LICENSE
*/
const
snakeskin = require('snakeskin'),
compiler = require('vue-template-compiler'),
transpiler = require('vue-template-es2015-compiler');
function toFunction(code) {
return `function () {${code}}`;
}
const options = {
modules: [{
postTransformNode(el) {
if (el.tag === 'img') {
el.attrs && el.attrs.some(rewrite);
}
}
}]
};
function rewrite(attr) {
if (attr.name === 'src') {
const
firstChar = attr.value[1];
if (firstChar === '.' || firstChar === '~') {
if (firstChar === '~') {
attr.value = `"${attr.value.slice(2)}`;
}
attr.value = `require(${attr.value})`;
}
return true;
}
}
function setParams(p) {
p = Object.assign({}, p, {useStrict: false});
p.adapterOptions = Object.assign({}, p.adapterOptions, p.adapterOptions.requireImgUrls ? options : undefined);
return p;
}
function template(id, fn, txt, p) {
const
compiled = compiler.compile(txt, p);
txt = `{
render: ${toFunction(compiled.render)},
staticRenderFns: [${compiled.staticRenderFns.map(toFunction).join(',')}]
};`;
let
res = `${id} = ${fn}return ${txt}};`;
if (p.transpiler) {
res = transpiler(res, p.transpilerOptions);
}
return res;
}
exports.adapter = function (txt, opt_params, opt_info) {
return snakeskin.adapter(txt, {setParams, template}, opt_params, opt_info);
};