-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (156 loc) · 4 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict'
const htmlMinify = require('html-minifier').minify
module.exports = function (comps) {
comps
.tag('foreach', {
paired: true,
recursive: true,
created: function () {
var $attr = this.$attributes;
this._iterableName = $attr.$obj || $attr.$arr || $attr.$items
this._iterableIsObject = !!$attr.$obj
this._itemName = $attr.$as
this._indexName = $attr.$index || '$index'
this._keyName = $attr.$key || '$key'
},
outer: function () {
if (this._iterableIsObject) {
return [
"${Object.keys(%s).map(function(%s,%s) {var %s=%s[%s];"
.replace('%s', this._iterableName)
.replace('%s', this._keyName)
.replace('%s', this._indexName)
.replace('%s', this._itemName)
.replace('%s', this._iterableName)
.replace('%s', this._keyName),
"}).join('')}"
]
} else {
return [
"${(%s.length ? %s : []).map(function (%s,%s) {"
.replace('%s', this._iterableName)
.replace('%s', this._iterableName)
.replace('%s', this._itemName || '$value')
.replace('%s', this._indexName),
"}).join('')}"
]
}
},
inner: function () {
var ctx = this
return 'return `' + minify(this.$el.childNodes.map(function (n) {
return ctx.$walk(n, ctx.$scope)
}).join('')) + '`'
}
}).tag('if', {
paired: true,
recursive: true,
created: function () {
this._condition = this.$attributes.$is
},
outer: function () {
return [
'${(function(){if(%s){return '.replace('%s', this._condition),
'}})()||""}'
]
},
inner: function () {
var ctx = this
return '`' + minify(this.$el.childNodes.map(function (n) {
return ctx.$walk(n, ctx.$scope)
}).join('')) + '`'
}
}).tag('else', {
paired: false,
created: function () {
this._condition = this.$attributes.$is || this.$attributes.$if
},
outer: function () {
return ['', '']
},
inner: function () {
if (this._condition) {
return '`}else if(%s){return `'.replace('%s', this._condition);
} else {
return '`}else{return `'
}
}
}).tag('scope', {
created: function () {
},
outer: function () {
var attrs = this.$attributes
var attKeys = Object.keys(attrs)
return [
'${(function (%s) {return '.replace('%s', attKeys.join(',')),
'})(%s)||""}'.replace('%s', attKeys.map(function (k) {
return attrs[k] || 'undefined'
}).join(','))]
},
inner: function () {
var ctx = this
return '`' + minify(this.$el.childNodes.map(function (n) {
return ctx.$walk(n, ctx.$scope)
}).join('')) + '`'
}
}).tag('/', {
// comment
paired: false,
created: function () {},
outer: function () {
return ['', '']
},
inner: function () {
return ''
}
}).tag('function', {
created: function () {},
outer: function () {
var attrs = this.$attributes
var attKeys = Object.keys(attrs)
return [
'${(function (%s) {'.replace('%s', attKeys.join(',')),
'})(%s)||""}'.replace('%s', attKeys.map(function (k) {
return attrs[k] || 'undefined'
}).join(','))]
},
inner: function () {
var ctx = this
return this.$el.childNodes.map(function (n) {
return ctx.$walk(n, ctx.$scope)
}).join('').trim();
}
}).aspect('component', {
beforeCreated: saveWith,
render: wrapByWith
}).aspect('include', {
beforeCreated: saveWith,
render: wrapByWith
})
}
function saveWith() {
this._with = this.$attributes.with
delete this.$attributes.with
}
function wrapByWith(innerHTML) {
var useWith = this._with
if (useWith) {
return '${function($value){with($value){return `' + innerHTML + '`}}(' + useWith + ')}'
} else {
return innerHTML
}
}
function minify(str) {
if (str.indexOf('<') != -1 || str.indexOf('\n') != -1) {
try {
str = htmlMinify(str, {
collapseWhitespace: true,
removeComments: true,
minifyJS: true,
includeAutoGeneratedTags: false,
minifyCSS: true
})
} catch(e) {}
}
return str;
}