@mednours/fronton 0.4.0 → 0.5.0
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/backon/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AASzC,MAAM,WAAW,kBAAkB;IACjC,wFAAwF;IACxF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;;;;;;;;;;OAYG;IACH,EAAE,CAAC,EAAE,OAAO,CAAC;CACd;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/backon/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AASzC,MAAM,WAAW,kBAAkB;IACjC,wFAAwF;IACxF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;;;;;;;;;;OAYG;IACH,EAAE,CAAC,EAAE,OAAO,CAAC;CACd;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAqB1G"}
|
package/dist/index.js
CHANGED
|
@@ -95,10 +95,92 @@ function scanExpressionEnd(code, start) {
|
|
|
95
95
|
}
|
|
96
96
|
var QUOTE_CHARS = /* @__PURE__ */ new Set(['"', "'", "`"]);
|
|
97
97
|
var STRING_LITERAL_PATTERN = /(["'`])(?<lit>(?:\\.|(?!\1).)*)\1/gd;
|
|
98
|
+
function scanTemplateLiteral(code, openIdx) {
|
|
99
|
+
const quasis = [];
|
|
100
|
+
const expressions = [];
|
|
101
|
+
let i = openIdx + 1;
|
|
102
|
+
let quasiStart = i;
|
|
103
|
+
while (i < code.length) {
|
|
104
|
+
const ch = code[i];
|
|
105
|
+
if (ch === "\\") {
|
|
106
|
+
i += 2;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (ch === "`") {
|
|
110
|
+
quasis.push([quasiStart, i]);
|
|
111
|
+
return { end: i, quasis, expressions };
|
|
112
|
+
}
|
|
113
|
+
if (ch === "$" && code[i + 1] === "{") {
|
|
114
|
+
quasis.push([quasiStart, i]);
|
|
115
|
+
let exprSliceStart = i + 2;
|
|
116
|
+
let j = exprSliceStart;
|
|
117
|
+
let braceDepth = 1;
|
|
118
|
+
let inStr = null;
|
|
119
|
+
while (j < code.length && braceDepth > 0) {
|
|
120
|
+
const c = code[j];
|
|
121
|
+
if (inStr) {
|
|
122
|
+
if (c === "\\") {
|
|
123
|
+
j += 2;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (c === inStr) inStr = null;
|
|
127
|
+
j++;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (c === '"' || c === "'") {
|
|
131
|
+
inStr = c;
|
|
132
|
+
j++;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (c === "`") {
|
|
136
|
+
if (j > exprSliceStart) expressions.push([exprSliceStart, j]);
|
|
137
|
+
const nested = scanTemplateLiteral(code, j);
|
|
138
|
+
quasis.push(...nested.quasis);
|
|
139
|
+
expressions.push(...nested.expressions);
|
|
140
|
+
j = nested.end + 1;
|
|
141
|
+
exprSliceStart = j;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (c === "{") braceDepth++;
|
|
145
|
+
else if (c === "}") {
|
|
146
|
+
braceDepth--;
|
|
147
|
+
if (braceDepth === 0) break;
|
|
148
|
+
}
|
|
149
|
+
j++;
|
|
150
|
+
}
|
|
151
|
+
if (j > exprSliceStart) expressions.push([exprSliceStart, j]);
|
|
152
|
+
i = j + 1;
|
|
153
|
+
quasiStart = i;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
i++;
|
|
157
|
+
}
|
|
158
|
+
quasis.push([quasiStart, code.length]);
|
|
159
|
+
return { end: code.length, quasis, expressions };
|
|
160
|
+
}
|
|
161
|
+
function rewriteQuotedStringLiterals(code, rangeStart, rangeEnd, s, map) {
|
|
162
|
+
let count = 0;
|
|
163
|
+
const slice = code.slice(rangeStart, rangeEnd);
|
|
164
|
+
for (const litMatch of slice.matchAll(STRING_LITERAL_PATTERN)) {
|
|
165
|
+
const range = litMatch.indices.groups.lit;
|
|
166
|
+
if (!range) continue;
|
|
167
|
+
const [relStart, relEnd] = range;
|
|
168
|
+
const start = rangeStart + relStart;
|
|
169
|
+
const end = rangeStart + relEnd;
|
|
170
|
+
const { text, changed } = rewriteClassList(code.slice(start, end), map);
|
|
171
|
+
if (changed) {
|
|
172
|
+
s.overwrite(start, end, text);
|
|
173
|
+
count++;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return count;
|
|
177
|
+
}
|
|
98
178
|
function rewriteTextOutput(code, map) {
|
|
99
179
|
const s = new MagicString(code);
|
|
100
180
|
let count = 0;
|
|
181
|
+
let lastProcessedEnd = 0;
|
|
101
182
|
for (const match of code.matchAll(PROP_START)) {
|
|
183
|
+
if (match.index < lastProcessedEnd) continue;
|
|
102
184
|
let i = match.index + match[0].length;
|
|
103
185
|
while (i < code.length && /\s/.test(code[i])) i++;
|
|
104
186
|
if (code[i] === "{") {
|
|
@@ -106,6 +188,21 @@ function rewriteTextOutput(code, map) {
|
|
|
106
188
|
while (i < code.length && /\s/.test(code[i])) i++;
|
|
107
189
|
}
|
|
108
190
|
const ch = code[i];
|
|
191
|
+
if (ch === "`") {
|
|
192
|
+
const tpl = scanTemplateLiteral(code, i);
|
|
193
|
+
for (const [qStart, qEnd] of tpl.quasis) {
|
|
194
|
+
const { text, changed } = rewriteClassList(code.slice(qStart, qEnd), map);
|
|
195
|
+
if (changed) {
|
|
196
|
+
s.overwrite(qStart, qEnd, text);
|
|
197
|
+
count++;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
for (const [eStart, eEnd] of tpl.expressions) {
|
|
201
|
+
count += rewriteQuotedStringLiterals(code, eStart, eEnd, s, map);
|
|
202
|
+
}
|
|
203
|
+
lastProcessedEnd = tpl.end + 1;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
109
206
|
if (ch !== void 0 && QUOTE_CHARS.has(ch)) {
|
|
110
207
|
const quote = ch;
|
|
111
208
|
let j = i + 1;
|
|
@@ -121,24 +218,13 @@ function rewriteTextOutput(code, map) {
|
|
|
121
218
|
s.overwrite(start, end, text);
|
|
122
219
|
count++;
|
|
123
220
|
}
|
|
221
|
+
lastProcessedEnd = end + 1;
|
|
124
222
|
continue;
|
|
125
223
|
}
|
|
126
224
|
if (ch === void 0) continue;
|
|
127
225
|
const exprEnd = scanExpressionEnd(code, i);
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const range = litMatch.indices.groups.lit;
|
|
131
|
-
if (!range) continue;
|
|
132
|
-
const [relStart, relEnd] = range;
|
|
133
|
-
const start = i + relStart;
|
|
134
|
-
const end = i + relEnd;
|
|
135
|
-
const list = code.slice(start, end);
|
|
136
|
-
const { text, changed } = rewriteClassList(list, map);
|
|
137
|
-
if (changed) {
|
|
138
|
-
s.overwrite(start, end, text);
|
|
139
|
-
count++;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
226
|
+
count += rewriteQuotedStringLiterals(code, i, exprEnd, s, map);
|
|
227
|
+
lastProcessedEnd = exprEnd;
|
|
142
228
|
}
|
|
143
229
|
return { code: s.toString(), count };
|
|
144
230
|
}
|
|
@@ -208,8 +294,10 @@ function frontonCompression(options = {}) {
|
|
|
208
294
|
// src/backon/index.ts
|
|
209
295
|
function backonProxy(target, options = {}) {
|
|
210
296
|
const { prefix = "/api", stripPrefix = true, ws = true } = options;
|
|
297
|
+
const normalizedPrefix = prefix === "/" ? "/" : prefix.replace(/\/+$/, "");
|
|
298
|
+
const escapedPrefix = normalizedPrefix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
211
299
|
return {
|
|
212
|
-
[
|
|
300
|
+
[normalizedPrefix]: {
|
|
213
301
|
target,
|
|
214
302
|
changeOrigin: true,
|
|
215
303
|
ws,
|
|
@@ -218,7 +306,7 @@ function backonProxy(target, options = {}) {
|
|
|
218
306
|
// an empty path ("/api" -> ""), which is not a valid request
|
|
219
307
|
// target — fall back to "/" rather than relying on whatever the
|
|
220
308
|
// underlying HTTP client happens to coerce an empty string to.
|
|
221
|
-
rewrite: (path) => path.replace(new RegExp(`^${
|
|
309
|
+
rewrite: (path) => path.replace(new RegExp(`^${escapedPrefix}`), "") || "/"
|
|
222
310
|
} : {}
|
|
223
311
|
}
|
|
224
312
|
};
|
|
@@ -2,20 +2,17 @@
|
|
|
2
2
|
* Rewrites known-safe class-bearing text in already-built JS/HTML output.
|
|
3
3
|
* For each `className`/`class` property found:
|
|
4
4
|
*
|
|
5
|
-
* - if its value is a single plain string/template literal (
|
|
6
|
-
*
|
|
5
|
+
* - if its value is a single plain string/template literal (including
|
|
6
|
+
* `${...}` interpolations), rewrite class tokens in static quasis and
|
|
7
|
+
* any string literals inside `${...}` expressions;
|
|
7
8
|
* - otherwise (a function call, ternary, logical expression — however a
|
|
8
9
|
* merge helper compiled down), scan the whole value expression for
|
|
9
10
|
* string literals and rewrite class tokens inside each one, without
|
|
10
11
|
* needing to know what produced the expression.
|
|
11
12
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* left exactly as it was. Only tokens present in `map` (i.e. provably
|
|
16
|
-
* real classes Tailwind actually generated, per rewrite-css.ts's
|
|
17
|
-
* collectClassesFromCss) are ever touched, so a miss here just means
|
|
18
|
-
* "not obfuscated", never a broken build.
|
|
13
|
+
* Only tokens present in `map` (i.e. provably real classes Tailwind actually
|
|
14
|
+
* generated, per rewrite-css.ts's collectClassesFromCss) are ever touched,
|
|
15
|
+
* so a miss here just means "not obfuscated", never a broken build.
|
|
19
16
|
*/
|
|
20
17
|
export declare function rewriteTextOutput(code: string, map: Map<string, string>): {
|
|
21
18
|
code: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rewrite-text.d.ts","sourceRoot":"","sources":["../../src/obfuscate/rewrite-text.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"rewrite-text.d.ts","sourceRoot":"","sources":["../../src/obfuscate/rewrite-text.ts"],"names":[],"mappings":"AA8LA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAsEzG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mednours/fronton",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Opinionated runtime pieces for fronton-scaffolded Vite projects — Tailwind class obfuscation, gzip/brotli compression, and backon API wiring.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,7 +18,9 @@
|
|
|
18
18
|
"publishConfig": {
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
|
-
"files": [
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
22
24
|
"exports": {
|
|
23
25
|
".": {
|
|
24
26
|
"types": "./dist/index.d.ts",
|