-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (32 loc) · 929 Bytes
/
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
'use strict';
var templateReg = /(template\t*\:[\s]*)\`([\s\S]*?)\`/g;
var templateUrlReg = /(templateUrl\t*\:\s*)[\"\'\`]{1}([\s\S]*?)[\"\'\`]{1}/g;
var stringRegex = /(['"])((?:[^\\]\\\1|.)*?)\1/g;
var lastReg = /\`([\s\S]*?)\`/g;
var fs = require('fs');
function getConent(string) {
var url = string.replace(stringRegex, function (match, quote, url) {
if (url.charAt(0) !== ".") {
url = "../.." + url;
}
return url;
});
url = string;
try{
var content = fs.readFileSync(url);
return content;
} catch(e) {
console.log(e);
}
return "";
}
module.exports = function(source) {
var newSource = source.replace(templateReg, function(a,b,c) {
return `${b} function() \{ return ${c} \}`;
}).replace(templateUrlReg, function(a,b,c) {
return "template: function() \{ return " + getConent(c) + " \}";
}).replace(lastReg, function(a,b) {
return b;
});
return newSource;
}