-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
75 lines (74 loc) · 2.74 KB
/
main.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
var topolr = require("topolr-util");
var parser = require('vue-template-compiler');
var codep = require("./parser/codeparser");
var cssp = require("./parser/cssparser");
var temp = require("./parser/templateparser");
var idp = require("./parser/idparser");
var config = require("./parser/config/config");
var codetemp = topolr.file(require("path").resolve(__dirname, "./template/umd")).readSync();
var base = function (content, option) {
this._info = parser.parseComponent(content);
this.option = topolr.extend(true, {}, config, option);
};
base.getPath = function (path, suffix) {
var _a = path.replace(/\\/g, "/").split("/");
var _b = _a.pop().split(".");
_b.pop();
_a.push(_b.join(".") + "." + suffix);
return _a.join("/");
};
base.prototype.getParseInfo = function (filepath) {
var ths = this;
return cssp.call(this, this._info.styles, filepath).then(function (result) {
var str = "", r = [];
r.push(codep.call(ths, ths._info.script, filepath));
r.push(temp.call(ths, ths._info.template, filepath));
if (result.id) {
r.push(idp.call(ths, result.id));
}
str += "[" + r.join(",") + "]";
var codetemp = "";
if (ths.option.codeType === "cmd") {
codetemp = topolr.file(require("path").resolve(__dirname, "./template/cmd")).readSync();
} else {
codetemp = topolr.file(require("path").resolve(__dirname, "./template/umd")).readSync();
var name = filepath.replace(/\\/g, "/").split("/").pop().split(".")[0];
codetemp = codetemp.replace(/\[\[name\]\]/g, name);
}
if (ths.option.outputStyleFile) {
codetemp = codetemp.replace(/\[\[css\]\]/g, "");
} else {
codetemp = codetemp.replace(/\[\[css\]\]/g, result.content);
}
str = codetemp.replace(/\[\[code\]\]/g, str);
return {
script: str,
style: result.content,
styleRaw: result.raw,
styleId: result.id
};
});
};
base.prototype.getCodeStr = function (filepath) {
return this.getParseInfo(filepath).then(function (info) {
return info.script;
});
};
base.prototype.outputFile = function (path) {
var ths = this, info = null;
return this.getParseInfo(path).then(function (_info) {
info = _info;
if (ths.option.outputScriptFile) {
return topolr.file(base.getPath(path, "js")).write(info.script);
}
}).then(function () {
if (ths.option.outputStyleFile) {
return topolr.file(base.getPath(path, "css")).write(info.styleRaw);
}
}).then(function () {
return info.script;
});
};
module.exports = function (content, option) {
return new base(content, option);
};