-
Notifications
You must be signed in to change notification settings - Fork 21
/
build.cjs
122 lines (96 loc) · 3.24 KB
/
build.cjs
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
const fs = require("fs");
const path = require("path");
const babel = require("@babel/core");
// region JS
function generateJS() {
// list of JS files to concat
const inputJSFiles = [
path.join(__dirname, 'src/js/_namespace_.js'),
path.join(__dirname, 'src/js/enums/enums.js'),
path.join(__dirname, 'src/js/main.js')
];
// output JS file
const outputJSFilename = "dist/uasset-reader.js";
let outputJSFileContent = concatJSFiles(inputJSFiles);
outputJSFileContent = removeLines(outputJSFileContent);
outputJSFileContent = removeDebug(outputJSFileContent);
outputJSFileContent = addHeaderAndLicense(outputJSFileContent);
saveFile(outputJSFilename, outputJSFileContent);
}
function concatJSFiles(files) {
let outputFileContent = ["(function () {"];
outputFileContent.push('"use strict";' + "\n");
for(let idxFiles = 0, maxFiles = files.length; idxFiles < maxFiles; ++idxFiles) {
outputFileContent.push(fs.readFileSync(files[idxFiles]).toString("utf8"));
}
outputFileContent.push("})();");
return outputFileContent.join("\n");
}
function removeLines(content) {
const lines = content.split(/\r\n|\r|\n/g);
let idxLines = 0;
let maxLines = lines.length;
let newLines = [];
for (; idxLines < maxLines; ++idxLines) {
if (lines[idxLines].match("BUILD REMOVE LINE")) {
continue;
}
newLines.push(lines[idxLines]);
}
return newLines.join("\n");
}
function removeDebug(content) {
function getNode(node) {
if (node.isMemberExpression()) {
const object = node.get("object");
if (object.isIdentifier() && node.has("property")) {
return node.get("property");
}
}
return node;
}
function isConsole(nodePath) {
const callee = nodePath.get("callee");
if (!callee.isMemberExpression()) {
return;
}
return getNode(callee.get("object")).isIdentifier({name: "console"}) && callee.has("property");
}
function isAlert(nodePath) {
return getNode(nodePath.get("callee")).isIdentifier({name: "alert"});
}
return babel.transformSync(content, {
retainLines: true,
plugins:[{
visitor: {
DebuggerStatement(nodePath) {
nodePath.remove();
},
CallExpression(nodePath) {
if (isConsole(nodePath) || isAlert(nodePath)) {
nodePath.remove();
}
}
},
}]
}).code;
}
generateJS();
// endregion
function addHeaderAndLicense(content) {
const license = fs.readFileSync("./LICENSE", "utf8");
const pkg = require("./package.json");
let header = pkg.name + " (v" + pkg.version + ")\n" + pkg.homepage + "\n\n" + license;
const lines = header.split("\n");
for (let idx = 0, max = lines.length; idx < max; ++idx) {
if (idx + 1 !== max) {
lines[idx] = " * " + lines[idx];
} else {
lines[idx] = " */" + lines[idx];
}
}
return "/**\n" + lines.join("\n") + "\n" + content;
}
function saveFile(filename, content) {
fs.writeFileSync(filename, content);
}