@tailwindcss-mangle/shared 4.0.0 → 4.0.1-alpha.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.
package/dist/index.cjs ADDED
@@ -0,0 +1,314 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ ClassGenerator: () => ClassGenerator,
24
+ acceptChars: () => acceptChars,
25
+ defaultMangleClassFilter: () => defaultMangleClassFilter,
26
+ defu: () => defu,
27
+ defuOverrideArray: () => defuOverrideArray,
28
+ escapeStringRegexp: () => escapeStringRegexp,
29
+ groupBy: () => groupBy,
30
+ isMap: () => isMap,
31
+ isRegexp: () => isRegexp,
32
+ isValidSelector: () => isValidSelector,
33
+ makeRegex: () => makeRegex,
34
+ preserveClassNames: () => preserveClassNames,
35
+ regExpTest: () => regExpTest,
36
+ splitCode: () => splitCode,
37
+ stripEscapeSequence: () => stripEscapeSequence,
38
+ validateFilterRE: () => validateFilterRE
39
+ });
40
+ module.exports = __toCommonJS(src_exports);
41
+
42
+ // ../../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs
43
+ function isPlainObject(value) {
44
+ if (value === null || typeof value !== "object") {
45
+ return false;
46
+ }
47
+ const prototype = Object.getPrototypeOf(value);
48
+ if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
49
+ return false;
50
+ }
51
+ if (Symbol.iterator in value) {
52
+ return false;
53
+ }
54
+ if (Symbol.toStringTag in value) {
55
+ return Object.prototype.toString.call(value) === "[object Module]";
56
+ }
57
+ return true;
58
+ }
59
+ function _defu(baseObject, defaults, namespace = ".", merger) {
60
+ if (!isPlainObject(defaults)) {
61
+ return _defu(baseObject, {}, namespace, merger);
62
+ }
63
+ const object = Object.assign({}, defaults);
64
+ for (const key in baseObject) {
65
+ if (key === "__proto__" || key === "constructor") {
66
+ continue;
67
+ }
68
+ const value = baseObject[key];
69
+ if (value === null || value === void 0) {
70
+ continue;
71
+ }
72
+ if (merger && merger(object, key, value, namespace)) {
73
+ continue;
74
+ }
75
+ if (Array.isArray(value) && Array.isArray(object[key])) {
76
+ object[key] = [...value, ...object[key]];
77
+ } else if (isPlainObject(value) && isPlainObject(object[key])) {
78
+ object[key] = _defu(
79
+ value,
80
+ object[key],
81
+ (namespace ? `${namespace}.` : "") + key.toString(),
82
+ merger
83
+ );
84
+ } else {
85
+ object[key] = value;
86
+ }
87
+ }
88
+ return object;
89
+ }
90
+ function createDefu(merger) {
91
+ return (...arguments_) => (
92
+ // eslint-disable-next-line unicorn/no-array-reduce
93
+ arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
94
+ );
95
+ }
96
+ var defu = createDefu();
97
+ var defuFn = createDefu((object, key, currentValue) => {
98
+ if (object[key] !== void 0 && typeof currentValue === "function") {
99
+ object[key] = currentValue(object[key]);
100
+ return true;
101
+ }
102
+ });
103
+ var defuArrayFn = createDefu((object, key, currentValue) => {
104
+ if (Array.isArray(object[key]) && typeof currentValue === "function") {
105
+ object[key] = currentValue(object[key]);
106
+ return true;
107
+ }
108
+ });
109
+
110
+ // src/utils.ts
111
+ var defuOverrideArray = createDefu((obj, key, value) => {
112
+ if (Array.isArray(obj[key]) && Array.isArray(value)) {
113
+ obj[key] = value;
114
+ return true;
115
+ }
116
+ });
117
+ var preserveClassNames = [
118
+ // https://tailwindcss.com/docs/transition-timing-function start
119
+ // https://github.com/sonofmagic/tailwindcss-mangle/issues/21
120
+ "ease-out",
121
+ "ease-linear",
122
+ "ease-in",
123
+ "ease-in-out"
124
+ // https://tailwindcss.com/docs/transition-timing-function end
125
+ ];
126
+ var preserveClassNamesMap = preserveClassNames.reduce((acc, cur) => {
127
+ acc[cur] = true;
128
+ return acc;
129
+ }, {});
130
+ function defaultMangleClassFilter(className) {
131
+ if (preserveClassNamesMap[className]) {
132
+ return false;
133
+ }
134
+ return /[:-]/.test(className);
135
+ }
136
+ function groupBy(arr, cb) {
137
+ if (!Array.isArray(arr)) {
138
+ throw new TypeError("expected an array for first argument");
139
+ }
140
+ if (typeof cb !== "function") {
141
+ throw new TypeError("expected a function for second argument");
142
+ }
143
+ const result = {};
144
+ for (const item of arr) {
145
+ const bucketCategory = cb(item);
146
+ const bucket = result[bucketCategory];
147
+ if (Array.isArray(bucket)) {
148
+ result[bucketCategory].push(item);
149
+ } else {
150
+ result[bucketCategory] = [item];
151
+ }
152
+ }
153
+ return result;
154
+ }
155
+ var acceptChars = [..."abcdefghijklmnopqrstuvwxyz"];
156
+ function stripEscapeSequence(words) {
157
+ return words.replaceAll("\\", "");
158
+ }
159
+ function isRegexp(value) {
160
+ return Object.prototype.toString.call(value) === "[object RegExp]";
161
+ }
162
+ function isMap(value) {
163
+ return Object.prototype.toString.call(value) === "[object Map]";
164
+ }
165
+ function regExpTest(arr = [], str) {
166
+ if (Array.isArray(arr)) {
167
+ for (const item of arr) {
168
+ if (typeof item === "string") {
169
+ if (item === str) {
170
+ return true;
171
+ }
172
+ } else if (isRegexp(item)) {
173
+ item.lastIndex = 0;
174
+ if (item.test(str)) {
175
+ return true;
176
+ }
177
+ }
178
+ }
179
+ return false;
180
+ }
181
+ throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
182
+ }
183
+
184
+ // src/classGenerator.ts
185
+ var ClassGenerator = class {
186
+ newClassMap;
187
+ newClassSize;
188
+ context;
189
+ opts;
190
+ classPrefix;
191
+ constructor(opts = {}) {
192
+ this.newClassMap = {};
193
+ this.newClassSize = 0;
194
+ this.context = {};
195
+ this.opts = opts;
196
+ this.classPrefix = opts.classPrefix ?? "tw-";
197
+ }
198
+ defaultClassGenerate() {
199
+ const chars = [];
200
+ let rest = (this.newClassSize - this.newClassSize % acceptChars.length) / acceptChars.length;
201
+ if (rest > 0) {
202
+ while (true) {
203
+ rest -= 1;
204
+ const m = rest % acceptChars.length;
205
+ const c = acceptChars[m];
206
+ chars.push(c);
207
+ rest -= m;
208
+ if (rest === 0) {
209
+ break;
210
+ }
211
+ rest /= acceptChars.length;
212
+ }
213
+ }
214
+ const prefixIndex = this.newClassSize % acceptChars.length;
215
+ const newClassName = `${this.classPrefix}${acceptChars[prefixIndex]}${chars.join("")}`;
216
+ return newClassName;
217
+ }
218
+ ignoreClassName(className) {
219
+ return regExpTest(this.opts.ignoreClass, className);
220
+ }
221
+ includeFilePath(filePath) {
222
+ const { include } = this.opts;
223
+ return Array.isArray(include) ? regExpTest(include, filePath) : true;
224
+ }
225
+ excludeFilePath(filePath) {
226
+ const { exclude } = this.opts;
227
+ return Array.isArray(exclude) ? regExpTest(exclude, filePath) : false;
228
+ }
229
+ isFileIncluded(filePath) {
230
+ return this.includeFilePath(filePath) && !this.excludeFilePath(filePath);
231
+ }
232
+ transformCssClass(className) {
233
+ const key = stripEscapeSequence(className);
234
+ const cn = this.newClassMap[key];
235
+ if (cn) {
236
+ return cn.name;
237
+ }
238
+ return className;
239
+ }
240
+ generateClassName(original) {
241
+ const opts = this.opts;
242
+ original = stripEscapeSequence(original);
243
+ const cn = this.newClassMap[original];
244
+ if (cn) {
245
+ return cn;
246
+ }
247
+ let newClassName;
248
+ if (opts.customGenerate && typeof opts.customGenerate === "function") {
249
+ newClassName = opts.customGenerate(original, opts, this.context);
250
+ }
251
+ if (!newClassName) {
252
+ newClassName = this.defaultClassGenerate();
253
+ }
254
+ if (opts.reserveClassName && regExpTest(opts.reserveClassName, newClassName)) {
255
+ if (opts.log) {
256
+ console.log(`The class name has been reserved. ${newClassName}`);
257
+ }
258
+ this.newClassSize++;
259
+ return this.generateClassName(original);
260
+ }
261
+ if (opts.log) {
262
+ console.log(`Minify class name from ${original} to ${newClassName}`);
263
+ }
264
+ const newClass = {
265
+ name: newClassName,
266
+ usedBy: /* @__PURE__ */ new Set()
267
+ };
268
+ this.newClassMap[original] = newClass;
269
+ this.newClassSize++;
270
+ return newClass;
271
+ }
272
+ };
273
+
274
+ // src/regex.ts
275
+ function escapeStringRegexp(str) {
276
+ if (typeof str !== "string") {
277
+ throw new TypeError("Expected a string");
278
+ }
279
+ return str.replaceAll(/[$()*+.?[\\\]^{|}]/g, "\\$&").replaceAll("-", "\\x2d");
280
+ }
281
+ function makeRegex(str, options = {
282
+ exact: true
283
+ }) {
284
+ return new RegExp(`(?<=^|[\\s"])${escapeStringRegexp(str)}${options.exact ? '(?=$|[\\s"])' : ""}`, "g");
285
+ }
286
+
287
+ // src/split.ts
288
+ var validateFilterRE = /[\w\u00A0-\uFFFF%-?]/;
289
+ function isValidSelector(selector = "") {
290
+ return validateFilterRE.test(selector);
291
+ }
292
+ function splitCode(code, options = { splitQuote: true }) {
293
+ const regex = options.splitQuote ? /[\s"]+/ : /\s+/;
294
+ return code.split(regex).filter((x) => isValidSelector(x));
295
+ }
296
+ // Annotate the CommonJS export names for ESM import in node:
297
+ 0 && (module.exports = {
298
+ ClassGenerator,
299
+ acceptChars,
300
+ defaultMangleClassFilter,
301
+ defu,
302
+ defuOverrideArray,
303
+ escapeStringRegexp,
304
+ groupBy,
305
+ isMap,
306
+ isRegexp,
307
+ isValidSelector,
308
+ makeRegex,
309
+ preserveClassNames,
310
+ regExpTest,
311
+ splitCode,
312
+ stripEscapeSequence,
313
+ validateFilterRE
314
+ });
@@ -0,0 +1,75 @@
1
+ import * as defu from 'defu';
2
+ export { defu } from 'defu';
3
+
4
+ interface IClassGeneratorContextItem {
5
+ name: string;
6
+ usedBy: Set<string>;
7
+ }
8
+ interface IClassGeneratorOptions {
9
+ reserveClassName?: (string | RegExp)[];
10
+ customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined;
11
+ log?: boolean;
12
+ exclude?: (string | RegExp)[];
13
+ include?: (string | RegExp)[];
14
+ ignoreClass?: (string | RegExp)[];
15
+ classPrefix?: string;
16
+ }
17
+ interface IClassGenerator {
18
+ newClassMap: Record<string, IClassGeneratorContextItem>;
19
+ newClassSize: number;
20
+ context: Record<string, any>;
21
+ }
22
+
23
+ declare class ClassGenerator implements IClassGenerator {
24
+ newClassMap: Record<string, IClassGeneratorContextItem>;
25
+ newClassSize: number;
26
+ context: Record<string, any>;
27
+ opts: IClassGeneratorOptions;
28
+ classPrefix: string;
29
+ constructor(opts?: IClassGeneratorOptions);
30
+ defaultClassGenerate(): string;
31
+ ignoreClassName(className: string): boolean;
32
+ includeFilePath(filePath: string): boolean;
33
+ excludeFilePath(filePath: string): boolean;
34
+ isFileIncluded(filePath: string): boolean;
35
+ transformCssClass(className: string): string;
36
+ generateClassName(original: string): IClassGeneratorContextItem;
37
+ }
38
+
39
+ declare function escapeStringRegexp(str: string): string;
40
+ interface MakeRegexOptions {
41
+ /**
42
+ * 这是为了进行精确提取用的
43
+ * 比如同时出现了 bg-500 bg-500/50,
44
+ * true 只会提取 bg-500
45
+ * 而 false 会提取 2 个 bg-500
46
+ */
47
+ exact?: boolean;
48
+ }
49
+ declare function makeRegex(str: string, options?: MakeRegexOptions): RegExp;
50
+
51
+ declare const validateFilterRE: RegExp;
52
+ declare function isValidSelector(selector?: string): selector is string;
53
+ declare function splitCode(code: string, options?: {
54
+ splitQuote?: boolean;
55
+ }): string[];
56
+
57
+ declare const defuOverrideArray: <Source extends {
58
+ [x: string]: any;
59
+ [x: number]: any;
60
+ [x: symbol]: any;
61
+ }, Defaults extends Array<{
62
+ [x: string]: any;
63
+ [x: number]: any;
64
+ [x: symbol]: any;
65
+ } | (number | boolean | any[] | Record<never, any> | null | undefined)>>(source: Source, ...defaults: Defaults) => defu.Defu<Source, Defaults>;
66
+ declare const preserveClassNames: string[];
67
+ declare function defaultMangleClassFilter(className: string): boolean;
68
+ declare function groupBy<T>(arr: T[], cb: (arg: T) => string): Record<string, T[]>;
69
+ declare const acceptChars: string[];
70
+ declare function stripEscapeSequence(words: string): string;
71
+ declare function isRegexp(value: unknown): boolean;
72
+ declare function isMap(value: unknown): boolean;
73
+ declare function regExpTest(arr: (string | RegExp)[] | undefined, str: string): boolean;
74
+
75
+ export { ClassGenerator, type IClassGenerator, type IClassGeneratorContextItem, type IClassGeneratorOptions, type MakeRegexOptions, acceptChars, defaultMangleClassFilter, defuOverrideArray, escapeStringRegexp, groupBy, isMap, isRegexp, isValidSelector, makeRegex, preserveClassNames, regExpTest, splitCode, stripEscapeSequence, validateFilterRE };
@@ -0,0 +1,75 @@
1
+ import * as defu from 'defu';
2
+ export { defu } from 'defu';
3
+
4
+ interface IClassGeneratorContextItem {
5
+ name: string;
6
+ usedBy: Set<string>;
7
+ }
8
+ interface IClassGeneratorOptions {
9
+ reserveClassName?: (string | RegExp)[];
10
+ customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined;
11
+ log?: boolean;
12
+ exclude?: (string | RegExp)[];
13
+ include?: (string | RegExp)[];
14
+ ignoreClass?: (string | RegExp)[];
15
+ classPrefix?: string;
16
+ }
17
+ interface IClassGenerator {
18
+ newClassMap: Record<string, IClassGeneratorContextItem>;
19
+ newClassSize: number;
20
+ context: Record<string, any>;
21
+ }
22
+
23
+ declare class ClassGenerator implements IClassGenerator {
24
+ newClassMap: Record<string, IClassGeneratorContextItem>;
25
+ newClassSize: number;
26
+ context: Record<string, any>;
27
+ opts: IClassGeneratorOptions;
28
+ classPrefix: string;
29
+ constructor(opts?: IClassGeneratorOptions);
30
+ defaultClassGenerate(): string;
31
+ ignoreClassName(className: string): boolean;
32
+ includeFilePath(filePath: string): boolean;
33
+ excludeFilePath(filePath: string): boolean;
34
+ isFileIncluded(filePath: string): boolean;
35
+ transformCssClass(className: string): string;
36
+ generateClassName(original: string): IClassGeneratorContextItem;
37
+ }
38
+
39
+ declare function escapeStringRegexp(str: string): string;
40
+ interface MakeRegexOptions {
41
+ /**
42
+ * 这是为了进行精确提取用的
43
+ * 比如同时出现了 bg-500 bg-500/50,
44
+ * true 只会提取 bg-500
45
+ * 而 false 会提取 2 个 bg-500
46
+ */
47
+ exact?: boolean;
48
+ }
49
+ declare function makeRegex(str: string, options?: MakeRegexOptions): RegExp;
50
+
51
+ declare const validateFilterRE: RegExp;
52
+ declare function isValidSelector(selector?: string): selector is string;
53
+ declare function splitCode(code: string, options?: {
54
+ splitQuote?: boolean;
55
+ }): string[];
56
+
57
+ declare const defuOverrideArray: <Source extends {
58
+ [x: string]: any;
59
+ [x: number]: any;
60
+ [x: symbol]: any;
61
+ }, Defaults extends Array<{
62
+ [x: string]: any;
63
+ [x: number]: any;
64
+ [x: symbol]: any;
65
+ } | (number | boolean | any[] | Record<never, any> | null | undefined)>>(source: Source, ...defaults: Defaults) => defu.Defu<Source, Defaults>;
66
+ declare const preserveClassNames: string[];
67
+ declare function defaultMangleClassFilter(className: string): boolean;
68
+ declare function groupBy<T>(arr: T[], cb: (arg: T) => string): Record<string, T[]>;
69
+ declare const acceptChars: string[];
70
+ declare function stripEscapeSequence(words: string): string;
71
+ declare function isRegexp(value: unknown): boolean;
72
+ declare function isMap(value: unknown): boolean;
73
+ declare function regExpTest(arr: (string | RegExp)[] | undefined, str: string): boolean;
74
+
75
+ export { ClassGenerator, type IClassGenerator, type IClassGeneratorContextItem, type IClassGeneratorOptions, type MakeRegexOptions, acceptChars, defaultMangleClassFilter, defuOverrideArray, escapeStringRegexp, groupBy, isMap, isRegexp, isValidSelector, makeRegex, preserveClassNames, regExpTest, splitCode, stripEscapeSequence, validateFilterRE };
package/dist/index.js ADDED
@@ -0,0 +1,272 @@
1
+ // ../../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs
2
+ function isPlainObject(value) {
3
+ if (value === null || typeof value !== "object") {
4
+ return false;
5
+ }
6
+ const prototype = Object.getPrototypeOf(value);
7
+ if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
8
+ return false;
9
+ }
10
+ if (Symbol.iterator in value) {
11
+ return false;
12
+ }
13
+ if (Symbol.toStringTag in value) {
14
+ return Object.prototype.toString.call(value) === "[object Module]";
15
+ }
16
+ return true;
17
+ }
18
+ function _defu(baseObject, defaults, namespace = ".", merger) {
19
+ if (!isPlainObject(defaults)) {
20
+ return _defu(baseObject, {}, namespace, merger);
21
+ }
22
+ const object = Object.assign({}, defaults);
23
+ for (const key in baseObject) {
24
+ if (key === "__proto__" || key === "constructor") {
25
+ continue;
26
+ }
27
+ const value = baseObject[key];
28
+ if (value === null || value === void 0) {
29
+ continue;
30
+ }
31
+ if (merger && merger(object, key, value, namespace)) {
32
+ continue;
33
+ }
34
+ if (Array.isArray(value) && Array.isArray(object[key])) {
35
+ object[key] = [...value, ...object[key]];
36
+ } else if (isPlainObject(value) && isPlainObject(object[key])) {
37
+ object[key] = _defu(
38
+ value,
39
+ object[key],
40
+ (namespace ? `${namespace}.` : "") + key.toString(),
41
+ merger
42
+ );
43
+ } else {
44
+ object[key] = value;
45
+ }
46
+ }
47
+ return object;
48
+ }
49
+ function createDefu(merger) {
50
+ return (...arguments_) => (
51
+ // eslint-disable-next-line unicorn/no-array-reduce
52
+ arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
53
+ );
54
+ }
55
+ var defu = createDefu();
56
+ var defuFn = createDefu((object, key, currentValue) => {
57
+ if (object[key] !== void 0 && typeof currentValue === "function") {
58
+ object[key] = currentValue(object[key]);
59
+ return true;
60
+ }
61
+ });
62
+ var defuArrayFn = createDefu((object, key, currentValue) => {
63
+ if (Array.isArray(object[key]) && typeof currentValue === "function") {
64
+ object[key] = currentValue(object[key]);
65
+ return true;
66
+ }
67
+ });
68
+
69
+ // src/utils.ts
70
+ var defuOverrideArray = createDefu((obj, key, value) => {
71
+ if (Array.isArray(obj[key]) && Array.isArray(value)) {
72
+ obj[key] = value;
73
+ return true;
74
+ }
75
+ });
76
+ var preserveClassNames = [
77
+ // https://tailwindcss.com/docs/transition-timing-function start
78
+ // https://github.com/sonofmagic/tailwindcss-mangle/issues/21
79
+ "ease-out",
80
+ "ease-linear",
81
+ "ease-in",
82
+ "ease-in-out"
83
+ // https://tailwindcss.com/docs/transition-timing-function end
84
+ ];
85
+ var preserveClassNamesMap = preserveClassNames.reduce((acc, cur) => {
86
+ acc[cur] = true;
87
+ return acc;
88
+ }, {});
89
+ function defaultMangleClassFilter(className) {
90
+ if (preserveClassNamesMap[className]) {
91
+ return false;
92
+ }
93
+ return /[:-]/.test(className);
94
+ }
95
+ function groupBy(arr, cb) {
96
+ if (!Array.isArray(arr)) {
97
+ throw new TypeError("expected an array for first argument");
98
+ }
99
+ if (typeof cb !== "function") {
100
+ throw new TypeError("expected a function for second argument");
101
+ }
102
+ const result = {};
103
+ for (const item of arr) {
104
+ const bucketCategory = cb(item);
105
+ const bucket = result[bucketCategory];
106
+ if (Array.isArray(bucket)) {
107
+ result[bucketCategory].push(item);
108
+ } else {
109
+ result[bucketCategory] = [item];
110
+ }
111
+ }
112
+ return result;
113
+ }
114
+ var acceptChars = [..."abcdefghijklmnopqrstuvwxyz"];
115
+ function stripEscapeSequence(words) {
116
+ return words.replaceAll("\\", "");
117
+ }
118
+ function isRegexp(value) {
119
+ return Object.prototype.toString.call(value) === "[object RegExp]";
120
+ }
121
+ function isMap(value) {
122
+ return Object.prototype.toString.call(value) === "[object Map]";
123
+ }
124
+ function regExpTest(arr = [], str) {
125
+ if (Array.isArray(arr)) {
126
+ for (const item of arr) {
127
+ if (typeof item === "string") {
128
+ if (item === str) {
129
+ return true;
130
+ }
131
+ } else if (isRegexp(item)) {
132
+ item.lastIndex = 0;
133
+ if (item.test(str)) {
134
+ return true;
135
+ }
136
+ }
137
+ }
138
+ return false;
139
+ }
140
+ throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
141
+ }
142
+
143
+ // src/classGenerator.ts
144
+ var ClassGenerator = class {
145
+ newClassMap;
146
+ newClassSize;
147
+ context;
148
+ opts;
149
+ classPrefix;
150
+ constructor(opts = {}) {
151
+ this.newClassMap = {};
152
+ this.newClassSize = 0;
153
+ this.context = {};
154
+ this.opts = opts;
155
+ this.classPrefix = opts.classPrefix ?? "tw-";
156
+ }
157
+ defaultClassGenerate() {
158
+ const chars = [];
159
+ let rest = (this.newClassSize - this.newClassSize % acceptChars.length) / acceptChars.length;
160
+ if (rest > 0) {
161
+ while (true) {
162
+ rest -= 1;
163
+ const m = rest % acceptChars.length;
164
+ const c = acceptChars[m];
165
+ chars.push(c);
166
+ rest -= m;
167
+ if (rest === 0) {
168
+ break;
169
+ }
170
+ rest /= acceptChars.length;
171
+ }
172
+ }
173
+ const prefixIndex = this.newClassSize % acceptChars.length;
174
+ const newClassName = `${this.classPrefix}${acceptChars[prefixIndex]}${chars.join("")}`;
175
+ return newClassName;
176
+ }
177
+ ignoreClassName(className) {
178
+ return regExpTest(this.opts.ignoreClass, className);
179
+ }
180
+ includeFilePath(filePath) {
181
+ const { include } = this.opts;
182
+ return Array.isArray(include) ? regExpTest(include, filePath) : true;
183
+ }
184
+ excludeFilePath(filePath) {
185
+ const { exclude } = this.opts;
186
+ return Array.isArray(exclude) ? regExpTest(exclude, filePath) : false;
187
+ }
188
+ isFileIncluded(filePath) {
189
+ return this.includeFilePath(filePath) && !this.excludeFilePath(filePath);
190
+ }
191
+ transformCssClass(className) {
192
+ const key = stripEscapeSequence(className);
193
+ const cn = this.newClassMap[key];
194
+ if (cn) {
195
+ return cn.name;
196
+ }
197
+ return className;
198
+ }
199
+ generateClassName(original) {
200
+ const opts = this.opts;
201
+ original = stripEscapeSequence(original);
202
+ const cn = this.newClassMap[original];
203
+ if (cn) {
204
+ return cn;
205
+ }
206
+ let newClassName;
207
+ if (opts.customGenerate && typeof opts.customGenerate === "function") {
208
+ newClassName = opts.customGenerate(original, opts, this.context);
209
+ }
210
+ if (!newClassName) {
211
+ newClassName = this.defaultClassGenerate();
212
+ }
213
+ if (opts.reserveClassName && regExpTest(opts.reserveClassName, newClassName)) {
214
+ if (opts.log) {
215
+ console.log(`The class name has been reserved. ${newClassName}`);
216
+ }
217
+ this.newClassSize++;
218
+ return this.generateClassName(original);
219
+ }
220
+ if (opts.log) {
221
+ console.log(`Minify class name from ${original} to ${newClassName}`);
222
+ }
223
+ const newClass = {
224
+ name: newClassName,
225
+ usedBy: /* @__PURE__ */ new Set()
226
+ };
227
+ this.newClassMap[original] = newClass;
228
+ this.newClassSize++;
229
+ return newClass;
230
+ }
231
+ };
232
+
233
+ // src/regex.ts
234
+ function escapeStringRegexp(str) {
235
+ if (typeof str !== "string") {
236
+ throw new TypeError("Expected a string");
237
+ }
238
+ return str.replaceAll(/[$()*+.?[\\\]^{|}]/g, "\\$&").replaceAll("-", "\\x2d");
239
+ }
240
+ function makeRegex(str, options = {
241
+ exact: true
242
+ }) {
243
+ return new RegExp(`(?<=^|[\\s"])${escapeStringRegexp(str)}${options.exact ? '(?=$|[\\s"])' : ""}`, "g");
244
+ }
245
+
246
+ // src/split.ts
247
+ var validateFilterRE = /[\w\u00A0-\uFFFF%-?]/;
248
+ function isValidSelector(selector = "") {
249
+ return validateFilterRE.test(selector);
250
+ }
251
+ function splitCode(code, options = { splitQuote: true }) {
252
+ const regex = options.splitQuote ? /[\s"]+/ : /\s+/;
253
+ return code.split(regex).filter((x) => isValidSelector(x));
254
+ }
255
+ export {
256
+ ClassGenerator,
257
+ acceptChars,
258
+ defaultMangleClassFilter,
259
+ defu,
260
+ defuOverrideArray,
261
+ escapeStringRegexp,
262
+ groupBy,
263
+ isMap,
264
+ isRegexp,
265
+ isValidSelector,
266
+ makeRegex,
267
+ preserveClassNames,
268
+ regExpTest,
269
+ splitCode,
270
+ stripEscapeSequence,
271
+ validateFilterRE
272
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tailwindcss-mangle/shared",
3
3
  "type": "module",
4
- "version": "4.0.0",
4
+ "version": "4.0.1-alpha.0",
5
5
  "description": "The shared utils of tailwindcss-mangle",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",