-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpl.js
54 lines (47 loc) · 1.23 KB
/
tpl.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
var dot = require('dot');
var $id='';
var $child = [];
var $childcode=[];
function __construct($src,name){
var $match=$src.split(/<\/script>/ig);
if($match.length >0){
$match.pop()
for (var i= 0; i< $match. length; i++){
$child.push($match[i]);
}
}
if($child.length ==0 )
$child.push($src);
$id=name;
}
function compaim(){
$child.forEach(function($value){
$childcode.push ( {
'id': getId($value),
'code':dot.template($value.replace(/<script.*id=[\"'](.*)[\"'].*>/ig, "").replace ("</script>" ,"" ),'').toString()
});
});
return render();
}
function render() {
var $child = [];
$childcode.forEach(function(el) {
var tmp = ' exports.' + el.id + ' =' + el.code + ';';
$child.push(tmp);
});
var codetmp = [];
codetmp.push('define("' + $id + '",function(require,exports,module){');
codetmp.push($child.join('\r\n'));
codetmp.push(' });');
return codetmp.join('\r\n');
}
function getId($str){
var $match=$str.match(/<script.*id=[\"'](.*)[\"'].*>/i );
if($match){
return $match[ 1];
}
}
exports.dot=function(srcCode,name){
__construct(srcCode,name);
return compaim();
}