-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
170 lines (142 loc) · 5.49 KB
/
build.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
161
162
163
164
165
166
167
168
169
170
const fs = require("fs")
const path = require("path")
var UglifyJS = require("uglify-js");
const yaml = require('js-yaml');
var options = {
mangle: true
};
console.log("******************************************")
console.log("** Compile and minify dynamic snippets **")
console.log("******************************************")
console.log(" ")
const src = "./src/"
const dst = "./build/"
console.log(`> src=${src}`)
console.log(`> dst=${dst}`)
console.log(" ")
function removeComments(code) {
return code.replace(/\/\*(.|\n)*?\*\//gmi, function($0,$1) {
return "";
});
}
// Uglify all files
let all = "";
let allcss = "";
let allcss_reduced = "";
// empaqueta per categories
const categories = {
"general": {code:[], css:[]}
};
fs.readdirSync(src).forEach( (file) => {
if(path.extname(file)!=".js") {
return
}
// look for category in presets
const presetFile = path.join("presets", "dynamic_"+file.replace(".js",".yaml"));
let currentCat = "general";
if(fs.existsSync(presetFile)) {
const txt = fs.readFileSync(presetFile, "utf-8");
try {
let parsed = yaml.load(txt);
if(parsed.misc.category) {
currentCat = parsed.misc.category.toLowerCase().trim();
}
} catch(ex){
console.error(ex);
}
}
let catObj = categories[currentCat];
if(!catObj) {
catObj = {code:[], css:[]};
categories[currentCat] = catObj;
}
// Search for library includes
const code1 = fs.readFileSync(path.join(src, file), "utf-8")
const regex = /\/\*@include:(.*)\*\//gmi;
const needLibs = []
let m;
while ((m = regex.exec(code1)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
needLibs.push(m[1].trim());
}
const result = UglifyJS.minify(path.join(src, file), options)
if(result.error) {
console.error(result.error)
process.exit(1)
} else if(result.warnings) {
console.log(result.warnings)
}
let code = result.code;
// Add libraries if needed
if(needLibs.length) {
allLibs = "";
needLibs.forEach( (libName) => {
allLibs += removeComments(fs.readFileSync(libName, "utf-8")) + "\n"
});
code = allLibs + "\n" + code;
}
all += code + "\n"
catObj.code.push(code);
if(fs.existsSync(path.join(src, file.replace(".js",".css")))) {
let local_css = fs.readFileSync(path.join(src, file.replace(".js",".css")), "utf8");
var local_all_css = local_css;
//remove %SKIPALLCSS
if(local_all_css.indexOf("/* %SKIPALLCSS */")) {
local_all_css = local_all_css.split("/* %SKIPALLCSS */")[0];
}
local_all_css = removeComments(local_all_css)
local_css = removeComments(local_css);
local_css = local_css.replace(/'/g,'"').replace(/\\/g,"\\\\").replace(/\n/g,' ').replace(/\t/g,' ').replace(/ /g, ' ').replace(/5 Free/g,'5 Pro');
local_all_css = local_all_css.replace(/'/g,'"').replace(/\\/g,"\\\\").replace(/\n/g,' ').replace(/\t/g,' ').replace(/ /g, ' ').replace(/5 Free/g,'5 Pro');
catObj.css.push(local_css);
allcss_reduced += " "+local_all_css;
allcss += " " + local_css;
// add css
code = `
!function(){if(document.getElementById("sd_css_${file}")){return;}; var l = '${local_css}'; var s = document.createElement('style'); s.type = 'text/css'; s.innerHTML = l; s.id="sd_css_${file}"; document.getElementsByTagName('head')[0].appendChild(s);}();
`
+ code
}
code = "window.IB = window.IB || {}; window.IB.sd = window.IB.sd || {}; "+ code
let target = path.join(dst, file.replace(".js", ".min.js"));
fs.writeFileSync(target, code, {encoding:'utf8'});
console.log("> written " + target);
});
if(allcss.length) {
// add css
all = `
window.IB = window.IB || {}; window.IB.sd = window.IB.sd || {};
!function(){if(document.getElementById("sd_css_all")){return;}; var l = '${allcss}'; var s = document.createElement('style'); s.type = 'text/css'; s.innerHTML = l; s.id="sd_css_all"; document.getElementsByTagName('head')[0].appendChild(s);}();
`
+ all
}
// Monolithic file with everything
target = path.join(dst, "all.min.js");
fs.writeFileSync(target, all, {encoding:'utf8'});
console.log("> written " + target);
// Monolithic file only with css
if(allcss.length) {
// add css
target = path.join(dst, "all.css");
fs.writeFileSync(target, allcss_reduced, {encoding:'utf8'});
console.log("> written " + target);
}
// Bundles by categories
Object.keys(categories).forEach( (catname) => {
var listCode = categories[catname].code;
var listCss= categories[catname].css;
let code = "window.IB = window.IB || {}; window.IB.sd = window.IB.sd || {};";
if(listCss.length) {
code += `
!function(){if(document.getElementById("sd_css_${catname}")){return;}; var l = '${listCss.join(" ")}'; var s = document.createElement('style'); s.type = 'text/css'; s.innerHTML = l; s.id="sd_css_${catname}"; document.getElementsByTagName('head')[0].appendChild(s);}();
`;
}
code += listCode.join(" \n")
let target = path.join(dst, catname+"_cat.min.js");
fs.writeFileSync(target, code, {encoding:'utf8'});
console.log("> written " + target);
});