@m00rl0ck/simple-builder 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -1
- package/minify-js.js +135 -0
- package/package.json +2 -2
package/index.js
CHANGED
package/minify-js.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
export function minifyJS(code) {
|
|
2
|
+
let out = '';
|
|
3
|
+
let i = 0;
|
|
4
|
+
|
|
5
|
+
let inString = false;
|
|
6
|
+
let stringChar = '';
|
|
7
|
+
let inTemplate = false;
|
|
8
|
+
let inRegex = false;
|
|
9
|
+
let inSingleComment = false;
|
|
10
|
+
let inMultiComment = false;
|
|
11
|
+
|
|
12
|
+
while (i < code.length) {
|
|
13
|
+
const c = code[i];
|
|
14
|
+
const next = code[i + 1];
|
|
15
|
+
|
|
16
|
+
// ======================
|
|
17
|
+
// COMMENTS
|
|
18
|
+
// ======================
|
|
19
|
+
|
|
20
|
+
if (inSingleComment) {
|
|
21
|
+
if (c === '\n') {
|
|
22
|
+
inSingleComment = false;
|
|
23
|
+
}
|
|
24
|
+
i++;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (inMultiComment) {
|
|
29
|
+
if (c === '*' && next === '/') {
|
|
30
|
+
inMultiComment = false;
|
|
31
|
+
i += 2;
|
|
32
|
+
} else {
|
|
33
|
+
i++;
|
|
34
|
+
}
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!inString && !inTemplate && !inRegex) {
|
|
39
|
+
if (c === '/' && next === '/') {
|
|
40
|
+
inSingleComment = true;
|
|
41
|
+
i += 2;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (c === '/' && next === '*') {
|
|
46
|
+
inMultiComment = true;
|
|
47
|
+
i += 2;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ======================
|
|
53
|
+
// STRINGS
|
|
54
|
+
// ======================
|
|
55
|
+
|
|
56
|
+
if (!inTemplate && !inRegex && (c === '"' || c === "'")) {
|
|
57
|
+
if (inString && c === stringChar && code[i - 1] !== '\\') {
|
|
58
|
+
inString = false;
|
|
59
|
+
} else if (!inString) {
|
|
60
|
+
inString = true;
|
|
61
|
+
stringChar = c;
|
|
62
|
+
}
|
|
63
|
+
out += c;
|
|
64
|
+
i++;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ======================
|
|
69
|
+
// TEMPLATE
|
|
70
|
+
// ======================
|
|
71
|
+
|
|
72
|
+
if (!inString && !inRegex && c === '`') {
|
|
73
|
+
inTemplate = !inTemplate;
|
|
74
|
+
out += c;
|
|
75
|
+
i++;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ======================
|
|
80
|
+
// REGEX (simplified)
|
|
81
|
+
// ======================
|
|
82
|
+
|
|
83
|
+
if (!inString && !inTemplate && c === '/' && !inRegex) {
|
|
84
|
+
// naive check: після ( або = або : або , → regex
|
|
85
|
+
const prev = out.trim().slice(-1);
|
|
86
|
+
if (!prev || '({[=,:;!&|?'.includes(prev)) {
|
|
87
|
+
inRegex = true;
|
|
88
|
+
out += c;
|
|
89
|
+
i++;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (inRegex) {
|
|
95
|
+
if (c === '/' && code[i - 1] !== '\\') {
|
|
96
|
+
inRegex = false;
|
|
97
|
+
}
|
|
98
|
+
out += c;
|
|
99
|
+
i++;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ======================
|
|
104
|
+
// WHITESPACE
|
|
105
|
+
// ======================
|
|
106
|
+
|
|
107
|
+
if (!inString && !inTemplate && !inRegex) {
|
|
108
|
+
if (/\s/.test(c)) {
|
|
109
|
+
const prev = out[out.length - 1];
|
|
110
|
+
const nextChar = code[i + 1];
|
|
111
|
+
|
|
112
|
+
// не додаємо зайві пробіли
|
|
113
|
+
if (
|
|
114
|
+
prev &&
|
|
115
|
+
/[a-zA-Z0-9_$]/.test(prev) &&
|
|
116
|
+
/[a-zA-Z0-9_$]/.test(nextChar)
|
|
117
|
+
) {
|
|
118
|
+
out += ' ';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
i++;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ======================
|
|
127
|
+
// DEFAULT
|
|
128
|
+
// ======================
|
|
129
|
+
|
|
130
|
+
out += c;
|
|
131
|
+
i++;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return out.trim();
|
|
135
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@m00rl0ck/simple-builder",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Zero-dependency JS and CSS bundler with dev server and minifier",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"bundler.js",
|
|
25
25
|
"dev-server.js",
|
|
26
26
|
"index.js",
|
|
27
|
-
"minify.js",
|
|
27
|
+
"minify-js.js",
|
|
28
28
|
"minify-css.js"
|
|
29
29
|
]
|
|
30
30
|
}
|