@tailwindcss-obfuscator/core 1.0.10 → 1.1.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.
@@ -13,7 +13,5 @@ export declare class ApplyClassNames {
13
13
  };
14
14
  updateStylesheet(css: string): string;
15
15
  getMap(): Map<string, string>;
16
- /** Clears the obfuscator map only. Keeps allowlist so configResolved (after buildStart) does not clear it before transform runs. */
17
- reset(): void;
18
16
  }
19
17
  //# sourceMappingURL=apply-class-names.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"apply-class-names.d.ts","sourceRoot":"","sources":["../src/apply-class-names.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,QAAQ,EAAc,MAAM,2BAA2B,CAAC;AA6BtE,qBAAa,eAAe;IAC3B,OAAO,CAAC,SAAS,CAA0B;IAC3C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;gBAE5B,MAAM,SAAO;IAIzB,YAAY,CAAC,SAAS,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI;IAI1C,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,oBAAoB;IAY5B,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,QAAQ,CAAA;KAAE;IAwB5E,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAyDrC,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;IAI7B,oIAAoI;IACpI,KAAK,IAAI,IAAI;CAGb"}
1
+ {"version":3,"file":"apply-class-names.d.ts","sourceRoot":"","sources":["../src/apply-class-names.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,QAAQ,EAAc,MAAM,2BAA2B,CAAC;AAyBtE,qBAAa,eAAe;IAC3B,OAAO,CAAC,SAAS,CAA0B;IAC3C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;gBAE5B,MAAM,SAAO;IAIzB,YAAY,CAAC,SAAS,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI;IAI1C,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,oBAAoB;IAY5B,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,QAAQ,CAAA;KAAE;IAwB5E,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IA2DrC,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;CAG7B"}
@@ -44,6 +44,75 @@ function findCnCalls(code) {
44
44
  }
45
45
  return calls;
46
46
  }
47
+ function findObjectKeysInSpan(code, start, end) {
48
+ const keys = [];
49
+ let i = start;
50
+ let braceDepth = 0;
51
+ let inObject = false;
52
+ while (i < end) {
53
+ const ch = code[i];
54
+ if (ch === '"' || ch === "'" || ch === "`") {
55
+ i = skipQuotedString(code, i);
56
+ } else if (ch === "{") {
57
+ if (!inObject) {
58
+ inObject = true;
59
+ }
60
+ braceDepth += 1;
61
+ i += 1;
62
+ } else if (ch === "}") {
63
+ braceDepth -= 1;
64
+ if (braceDepth === 0) {
65
+ inObject = false;
66
+ }
67
+ i += 1;
68
+ } else if (inObject && braceDepth === 1) {
69
+ if (/[a-zA-Z_]/.test(ch)) {
70
+ let key = "";
71
+ const keyStart = i;
72
+ while (i < end && /[a-zA-Z0-9_-]/.test(code[i])) {
73
+ key += code[i];
74
+ i += 1;
75
+ }
76
+ const keyEnd = i;
77
+ while (i < end && /\s/.test(code[i])) {
78
+ i += 1;
79
+ }
80
+ if (i < end && code[i] === ":") {
81
+ keys.push({ end: keyEnd, key, start: keyStart });
82
+ i += 1;
83
+ }
84
+ } else if (ch === '"' || ch === "'") {
85
+ const strStart = i;
86
+ i = skipQuotedString(code, i);
87
+ const strEnd = i;
88
+ const quote = code[strStart];
89
+ let key = "";
90
+ let j = strStart + 1;
91
+ while (j < strEnd - 1) {
92
+ if (code[j] === "\\" && j + 1 < strEnd - 1) {
93
+ key += code[j + 1];
94
+ j += 2;
95
+ } else {
96
+ key += code[j];
97
+ j += 1;
98
+ }
99
+ }
100
+ while (i < end && /\s/.test(code[i])) {
101
+ i += 1;
102
+ }
103
+ if (i < end && code[i] === ":") {
104
+ keys.push({ end: strEnd, key, start: strStart });
105
+ i += 1;
106
+ }
107
+ } else {
108
+ i += 1;
109
+ }
110
+ } else {
111
+ i += 1;
112
+ }
113
+ }
114
+ return keys;
115
+ }
47
116
  function findClassStrings(code) {
48
117
  const matches = [];
49
118
  const stringRegex = /(['"`])(.*?)\1/g;
@@ -62,6 +131,22 @@ function findClassStrings(code) {
62
131
  }
63
132
  match = stringRegex.exec(call.content);
64
133
  }
134
+ const parenContentStart = call.content.indexOf("(") + 1;
135
+ const parenContentEnd = call.content.lastIndexOf(")");
136
+ if (parenContentStart > 0 && parenContentEnd > parenContentStart) {
137
+ const callBodyStart = call.start + parenContentStart;
138
+ const keys = findObjectKeysInSpan(code, callBodyStart, callBodyStart + (parenContentEnd - parenContentStart));
139
+ for (const { key, start, end } of keys) {
140
+ if (key.length >= 1) {
141
+ matches.push({
142
+ classString: key,
143
+ end,
144
+ fullMatch: code.slice(start, end),
145
+ start
146
+ });
147
+ }
148
+ }
149
+ }
65
150
  }
66
151
  const jsxClassNameRegex = /className\s*=\s*(?:\{\s*["'`]([^"'`]+)["'`]\s*\}|["'`]([^"'`]+)["'`])/g;
67
152
  const compiledClassNameRegex = /className\s*:\s*(["'`])([^"'`]+)\1/g;
@@ -119,9 +204,13 @@ function bigintToLetters(num, alphabet) {
119
204
  } while (n > 0n);
120
205
  return result;
121
206
  }
207
+ var globalClassMap = new Map;
208
+ function __resetGlobalClassMap() {
209
+ globalClassMap.clear();
210
+ }
122
211
 
123
212
  class Obfuscator {
124
- map = new Map;
213
+ map = globalClassMap;
125
214
  prefix;
126
215
  alphabet = "abcdefghijklmnopqrstuvwxyz";
127
216
  constructor(prefix = "az") {
@@ -140,9 +229,6 @@ class Obfuscator {
140
229
  getMap() {
141
230
  return new Map(this.map);
142
231
  }
143
- reset() {
144
- this.map.clear();
145
- }
146
232
  }
147
233
  // src/apply-class-names.ts
148
234
  var RE_WHITESPACE = /\s+/;
@@ -213,8 +299,8 @@ class ApplyClassNames {
213
299
  const importantRegex = new RegExp(`\\.${escapedForRegex}(?=[\\s\\{\\[\\]\\(\\):,;]|$)`, "g");
214
300
  transformed = transformed.replace(importantRegex, `.${obfuscated}`);
215
301
  }
216
- if (original.includes("[") || original.includes("]") || original.includes(":") || original.includes("/") || original.includes(".") || original.includes(",")) {
217
- const cssEscaped = original.replaceAll(/\[/g, "\\[").replaceAll(/\]/g, "\\]").replaceAll(/:/g, "\\:").replaceAll(/\//g, "\\/").replaceAll("(", "\\(").replaceAll(")", "\\)").replaceAll(".", "\\.").replaceAll(",", "\\,");
302
+ if (original.includes("[") || original.includes("]") || original.includes(":") || original.includes("/") || original.includes(".") || original.includes(",") || original.includes("&")) {
303
+ const cssEscaped = original.replaceAll(/\[/g, "\\[").replaceAll(/\]/g, "\\]").replaceAll(/:/g, "\\:").replaceAll(/\//g, "\\/").replaceAll("(", "\\(").replaceAll(")", "\\)").replaceAll(".", "\\.").replaceAll(",", "\\,").replaceAll("&", "\\&");
218
304
  const cssSelector = `.${cssEscaped}`;
219
305
  if (transformed.includes(cssSelector)) {
220
306
  transformed = transformed.replaceAll(cssSelector, `.${obfuscated}`);
@@ -226,9 +312,6 @@ class ApplyClassNames {
226
312
  getMap() {
227
313
  return this.obfuscator.getMap();
228
314
  }
229
- reset() {
230
- this.obfuscator.reset();
231
- }
232
315
  }
233
316
  export {
234
317
  ApplyClassNames
@@ -4,9 +4,5 @@ export interface ClassStringMatch {
4
4
  classString: string;
5
5
  start: number;
6
6
  }
7
- /**
8
- * Find all class string occurrences in source (className="...", cn("..."), etc.).
9
- * Only finds; does not replace or check allowlist.
10
- */
11
7
  export declare function findClassStrings(code: string): ClassStringMatch[];
12
8
  //# sourceMappingURL=find-class-strings.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"find-class-strings.d.ts","sourceRoot":"","sources":["../src/find-class-strings.ts"],"names":[],"mappings":"AAsDA,MAAM,WAAW,gBAAgB;IAChC,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;CACd;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,EAAE,CA0DjE"}
1
+ {"version":3,"file":"find-class-strings.d.ts","sourceRoot":"","sources":["../src/find-class-strings.ts"],"names":[],"mappings":"AAsDA,MAAM,WAAW,gBAAgB;IAChC,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;CACd;AAqFD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAgFjE"}
@@ -44,6 +44,75 @@ function findCnCalls(code) {
44
44
  }
45
45
  return calls;
46
46
  }
47
+ function findObjectKeysInSpan(code, start, end) {
48
+ const keys = [];
49
+ let i = start;
50
+ let braceDepth = 0;
51
+ let inObject = false;
52
+ while (i < end) {
53
+ const ch = code[i];
54
+ if (ch === '"' || ch === "'" || ch === "`") {
55
+ i = skipQuotedString(code, i);
56
+ } else if (ch === "{") {
57
+ if (!inObject) {
58
+ inObject = true;
59
+ }
60
+ braceDepth += 1;
61
+ i += 1;
62
+ } else if (ch === "}") {
63
+ braceDepth -= 1;
64
+ if (braceDepth === 0) {
65
+ inObject = false;
66
+ }
67
+ i += 1;
68
+ } else if (inObject && braceDepth === 1) {
69
+ if (/[a-zA-Z_]/.test(ch)) {
70
+ let key = "";
71
+ const keyStart = i;
72
+ while (i < end && /[a-zA-Z0-9_-]/.test(code[i])) {
73
+ key += code[i];
74
+ i += 1;
75
+ }
76
+ const keyEnd = i;
77
+ while (i < end && /\s/.test(code[i])) {
78
+ i += 1;
79
+ }
80
+ if (i < end && code[i] === ":") {
81
+ keys.push({ end: keyEnd, key, start: keyStart });
82
+ i += 1;
83
+ }
84
+ } else if (ch === '"' || ch === "'") {
85
+ const strStart = i;
86
+ i = skipQuotedString(code, i);
87
+ const strEnd = i;
88
+ const quote = code[strStart];
89
+ let key = "";
90
+ let j = strStart + 1;
91
+ while (j < strEnd - 1) {
92
+ if (code[j] === "\\" && j + 1 < strEnd - 1) {
93
+ key += code[j + 1];
94
+ j += 2;
95
+ } else {
96
+ key += code[j];
97
+ j += 1;
98
+ }
99
+ }
100
+ while (i < end && /\s/.test(code[i])) {
101
+ i += 1;
102
+ }
103
+ if (i < end && code[i] === ":") {
104
+ keys.push({ end: strEnd, key, start: strStart });
105
+ i += 1;
106
+ }
107
+ } else {
108
+ i += 1;
109
+ }
110
+ } else {
111
+ i += 1;
112
+ }
113
+ }
114
+ return keys;
115
+ }
47
116
  function findClassStrings(code) {
48
117
  const matches = [];
49
118
  const stringRegex = /(['"`])(.*?)\1/g;
@@ -62,6 +131,22 @@ function findClassStrings(code) {
62
131
  }
63
132
  match = stringRegex.exec(call.content);
64
133
  }
134
+ const parenContentStart = call.content.indexOf("(") + 1;
135
+ const parenContentEnd = call.content.lastIndexOf(")");
136
+ if (parenContentStart > 0 && parenContentEnd > parenContentStart) {
137
+ const callBodyStart = call.start + parenContentStart;
138
+ const keys = findObjectKeysInSpan(code, callBodyStart, callBodyStart + (parenContentEnd - parenContentStart));
139
+ for (const { key, start, end } of keys) {
140
+ if (key.length >= 1) {
141
+ matches.push({
142
+ classString: key,
143
+ end,
144
+ fullMatch: code.slice(start, end),
145
+ start
146
+ });
147
+ }
148
+ }
149
+ }
65
150
  }
66
151
  const jsxClassNameRegex = /className\s*=\s*(?:\{\s*["'`]([^"'`]+)["'`]\s*\}|["'`]([^"'`]+)["'`])/g;
67
152
  const compiledClassNameRegex = /className\s*:\s*(["'`])([^"'`]+)\1/g;
@@ -1,4 +1,5 @@
1
1
  type ClassMap = Map<string, string>;
2
+ declare function __resetGlobalClassMap(): void;
2
3
  declare class Obfuscator {
3
4
  private readonly map;
4
5
  private readonly prefix;
@@ -6,8 +7,8 @@ declare class Obfuscator {
6
7
  constructor(prefix?: string);
7
8
  getObfuscatedClass(originalClass: string): string;
8
9
  getMap(): ClassMap;
9
- reset(): void;
10
10
  }
11
11
  export type { ClassMap };
12
12
  export { Obfuscator };
13
+ export { __resetGlobalClassMap };
13
14
  //# sourceMappingURL=generate-class-names.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"generate-class-names.d.ts","sourceRoot":"","sources":["../src/generate-class-names.ts"],"names":[],"mappings":"AAAA,KAAK,QAAQ,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAoCpC,cAAM,UAAU;IACf,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAuB;IAC3C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgC;gBAE7C,MAAM,SAAO;IAIzB,kBAAkB,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM;IAejD,MAAM,IAAI,QAAQ;IAIlB,KAAK,IAAI,IAAI;CAGb;AAED,YAAY,EAAE,QAAQ,EAAE,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,CAAC"}
1
+ {"version":3,"file":"generate-class-names.d.ts","sourceRoot":"","sources":["../src/generate-class-names.ts"],"names":[],"mappings":"AAAA,KAAK,QAAQ,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAkCpC,iBAAS,qBAAqB,IAAI,IAAI,CAErC;AAED,cAAM,UAAU;IACf,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA4B;IAChD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgC;gBAE7C,MAAM,SAAO;IAIzB,kBAAkB,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM;IAejD,MAAM,IAAI,QAAQ;CAGlB;AAED,YAAY,EAAE,QAAQ,EAAE,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,CAAC;AACtB,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
@@ -23,9 +23,13 @@ function bigintToLetters(num, alphabet) {
23
23
  } while (n > 0n);
24
24
  return result;
25
25
  }
26
+ var globalClassMap = new Map;
27
+ function __resetGlobalClassMap() {
28
+ globalClassMap.clear();
29
+ }
26
30
 
27
31
  class Obfuscator {
28
- map = new Map;
32
+ map = globalClassMap;
29
33
  prefix;
30
34
  alphabet = "abcdefghijklmnopqrstuvwxyz";
31
35
  constructor(prefix = "az") {
@@ -44,10 +48,8 @@ class Obfuscator {
44
48
  getMap() {
45
49
  return new Map(this.map);
46
50
  }
47
- reset() {
48
- this.map.clear();
49
- }
50
51
  }
51
52
  export {
53
+ __resetGlobalClassMap,
52
54
  Obfuscator
53
55
  };
package/package.json CHANGED
@@ -20,5 +20,5 @@
20
20
  },
21
21
  "type": "module",
22
22
  "types": "./dist/apply-class-names.d.ts",
23
- "version": "1.0.10"
23
+ "version": "1.1.0"
24
24
  }