@tailwindcss-mangle/shared 2.2.2 → 4.0.0-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/LICENSE +21 -0
- package/package.json +23 -24
- package/dist/index.cjs +0 -197
- package/dist/index.d.cts +0 -56
- package/dist/index.d.mts +0 -56
- package/dist/index.d.ts +0 -56
- package/dist/index.mjs +0 -182
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ice breaker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,14 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tailwindcss-mangle/shared",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "4.0.0-alpha.0",
|
|
4
5
|
"description": "The shared utils of tailwindcss-mangle",
|
|
6
|
+
"author": "SonOfMagic <qq1324318532@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/sonofmagic/tailwindcss-mangle",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/sonofmagic/tailwindcss-mangle.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"tailwindcss",
|
|
15
|
+
"patch",
|
|
16
|
+
"core",
|
|
17
|
+
"mangle",
|
|
18
|
+
"shared",
|
|
19
|
+
"utils"
|
|
20
|
+
],
|
|
5
21
|
"exports": {
|
|
6
22
|
".": {
|
|
7
23
|
"types": "./dist/index.d.ts",
|
|
8
|
-
"import": "./dist/index.
|
|
24
|
+
"import": "./dist/index.js",
|
|
9
25
|
"require": "./dist/index.cjs"
|
|
10
26
|
}
|
|
11
27
|
},
|
|
28
|
+
"main": "./dist/index.cjs",
|
|
29
|
+
"module": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
12
31
|
"typesVersions": {
|
|
13
32
|
"*": {
|
|
14
33
|
"*": [
|
|
@@ -17,36 +36,16 @@
|
|
|
17
36
|
]
|
|
18
37
|
}
|
|
19
38
|
},
|
|
20
|
-
"main": "./dist/index.cjs",
|
|
21
|
-
"module": "./dist/index.mjs",
|
|
22
|
-
"types": "./dist/index.d.ts",
|
|
23
39
|
"files": [
|
|
24
40
|
"dist"
|
|
25
41
|
],
|
|
26
|
-
"keywords": [
|
|
27
|
-
"tailwindcss",
|
|
28
|
-
"patch",
|
|
29
|
-
"core",
|
|
30
|
-
"mangle",
|
|
31
|
-
"shared",
|
|
32
|
-
"utils"
|
|
33
|
-
],
|
|
34
|
-
"author": "SonOfMagic <qq1324318532@gmail.com>",
|
|
35
|
-
"license": "MIT",
|
|
36
42
|
"publishConfig": {
|
|
37
43
|
"access": "public",
|
|
38
44
|
"registry": "https://registry.npmjs.org/"
|
|
39
45
|
},
|
|
40
|
-
"dependencies": {},
|
|
41
|
-
"devDependencies": {},
|
|
42
|
-
"homepage": "https://github.com/sonofmagic/tailwindcss-mangle",
|
|
43
|
-
"repository": {
|
|
44
|
-
"type": "git",
|
|
45
|
-
"url": "git+https://github.com/sonofmagic/tailwindcss-mangle.git"
|
|
46
|
-
},
|
|
47
46
|
"scripts": {
|
|
48
|
-
"dev": "
|
|
49
|
-
"build": "
|
|
47
|
+
"dev": "tsup --watch --sourcemap",
|
|
48
|
+
"build": "tsup",
|
|
50
49
|
"test": "vitest run --coverage.enabled",
|
|
51
50
|
"test:dev": "vitest"
|
|
52
51
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const preserveClassNames = [
|
|
4
|
-
// https://tailwindcss.com/docs/transition-timing-function start
|
|
5
|
-
// https://github.com/sonofmagic/tailwindcss-mangle/issues/21
|
|
6
|
-
"ease-out",
|
|
7
|
-
"ease-linear",
|
|
8
|
-
"ease-in",
|
|
9
|
-
"ease-in-out"
|
|
10
|
-
// https://tailwindcss.com/docs/transition-timing-function end
|
|
11
|
-
];
|
|
12
|
-
const preserveClassNamesMap = preserveClassNames.reduce((acc, cur) => {
|
|
13
|
-
acc[cur] = true;
|
|
14
|
-
return acc;
|
|
15
|
-
}, {});
|
|
16
|
-
const defaultMangleClassFilter = (className) => {
|
|
17
|
-
if (preserveClassNamesMap[className]) {
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
return /[:-]/.test(className);
|
|
21
|
-
};
|
|
22
|
-
function groupBy(arr, cb) {
|
|
23
|
-
if (!Array.isArray(arr)) {
|
|
24
|
-
throw new TypeError("expected an array for first argument");
|
|
25
|
-
}
|
|
26
|
-
if (typeof cb !== "function") {
|
|
27
|
-
throw new TypeError("expected a function for second argument");
|
|
28
|
-
}
|
|
29
|
-
const result = {};
|
|
30
|
-
for (const item of arr) {
|
|
31
|
-
const bucketCategory = cb(item);
|
|
32
|
-
const bucket = result[bucketCategory];
|
|
33
|
-
if (Array.isArray(bucket)) {
|
|
34
|
-
result[bucketCategory].push(item);
|
|
35
|
-
} else {
|
|
36
|
-
result[bucketCategory] = [item];
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return result;
|
|
40
|
-
}
|
|
41
|
-
const acceptChars = [..."abcdefghijklmnopqrstuvwxyz"];
|
|
42
|
-
function stripEscapeSequence(words) {
|
|
43
|
-
return words.replaceAll("\\", "");
|
|
44
|
-
}
|
|
45
|
-
function isRegexp(value) {
|
|
46
|
-
return Object.prototype.toString.call(value) === "[object RegExp]";
|
|
47
|
-
}
|
|
48
|
-
function isMap(value) {
|
|
49
|
-
return Object.prototype.toString.call(value) === "[object Map]";
|
|
50
|
-
}
|
|
51
|
-
function regExpTest(arr = [], str) {
|
|
52
|
-
if (Array.isArray(arr)) {
|
|
53
|
-
for (const item of arr) {
|
|
54
|
-
if (typeof item === "string") {
|
|
55
|
-
if (item === str) {
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
} else if (isRegexp(item)) {
|
|
59
|
-
item.lastIndex = 0;
|
|
60
|
-
if (item.test(str)) {
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
var __defProp = Object.defineProperty;
|
|
71
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
72
|
-
var __publicField = (obj, key, value) => {
|
|
73
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
74
|
-
return value;
|
|
75
|
-
};
|
|
76
|
-
class ClassGenerator {
|
|
77
|
-
constructor(opts = {}) {
|
|
78
|
-
__publicField(this, "newClassMap");
|
|
79
|
-
__publicField(this, "newClassSize");
|
|
80
|
-
__publicField(this, "context");
|
|
81
|
-
__publicField(this, "opts");
|
|
82
|
-
__publicField(this, "classPrefix");
|
|
83
|
-
this.newClassMap = {};
|
|
84
|
-
this.newClassSize = 0;
|
|
85
|
-
this.context = {};
|
|
86
|
-
this.opts = opts;
|
|
87
|
-
this.classPrefix = opts.classPrefix ?? "tw-";
|
|
88
|
-
}
|
|
89
|
-
defaultClassGenerate() {
|
|
90
|
-
const chars = [];
|
|
91
|
-
let rest = (this.newClassSize - this.newClassSize % acceptChars.length) / acceptChars.length;
|
|
92
|
-
if (rest > 0) {
|
|
93
|
-
while (true) {
|
|
94
|
-
rest -= 1;
|
|
95
|
-
const m = rest % acceptChars.length;
|
|
96
|
-
const c = acceptChars[m];
|
|
97
|
-
chars.push(c);
|
|
98
|
-
rest -= m;
|
|
99
|
-
if (rest === 0) {
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
rest /= acceptChars.length;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
const prefixIndex = this.newClassSize % acceptChars.length;
|
|
106
|
-
const newClassName = `${this.classPrefix}${acceptChars[prefixIndex]}${chars.join("")}`;
|
|
107
|
-
return newClassName;
|
|
108
|
-
}
|
|
109
|
-
ignoreClassName(className) {
|
|
110
|
-
return regExpTest(this.opts.ignoreClass, className);
|
|
111
|
-
}
|
|
112
|
-
includeFilePath(filePath) {
|
|
113
|
-
const { include } = this.opts;
|
|
114
|
-
return Array.isArray(include) ? regExpTest(include, filePath) : true;
|
|
115
|
-
}
|
|
116
|
-
excludeFilePath(filePath) {
|
|
117
|
-
const { exclude } = this.opts;
|
|
118
|
-
return Array.isArray(exclude) ? regExpTest(exclude, filePath) : false;
|
|
119
|
-
}
|
|
120
|
-
isFileIncluded(filePath) {
|
|
121
|
-
return this.includeFilePath(filePath) && !this.excludeFilePath(filePath);
|
|
122
|
-
}
|
|
123
|
-
transformCssClass(className) {
|
|
124
|
-
const key = stripEscapeSequence(className);
|
|
125
|
-
const cn = this.newClassMap[key];
|
|
126
|
-
if (cn)
|
|
127
|
-
return cn.name;
|
|
128
|
-
return className;
|
|
129
|
-
}
|
|
130
|
-
generateClassName(original) {
|
|
131
|
-
const opts = this.opts;
|
|
132
|
-
original = stripEscapeSequence(original);
|
|
133
|
-
const cn = this.newClassMap[original];
|
|
134
|
-
if (cn)
|
|
135
|
-
return cn;
|
|
136
|
-
let newClassName;
|
|
137
|
-
if (opts.customGenerate && typeof opts.customGenerate === "function") {
|
|
138
|
-
newClassName = opts.customGenerate(original, opts, this.context);
|
|
139
|
-
}
|
|
140
|
-
if (!newClassName) {
|
|
141
|
-
newClassName = this.defaultClassGenerate();
|
|
142
|
-
}
|
|
143
|
-
if (opts.reserveClassName && regExpTest(opts.reserveClassName, newClassName)) {
|
|
144
|
-
if (opts.log) {
|
|
145
|
-
console.log(`The class name has been reserved. ${newClassName}`);
|
|
146
|
-
}
|
|
147
|
-
this.newClassSize++;
|
|
148
|
-
return this.generateClassName(original);
|
|
149
|
-
}
|
|
150
|
-
if (opts.log) {
|
|
151
|
-
console.log(`Minify class name from ${original} to ${newClassName}`);
|
|
152
|
-
}
|
|
153
|
-
const newClass = {
|
|
154
|
-
name: newClassName,
|
|
155
|
-
usedBy: /* @__PURE__ */ new Set()
|
|
156
|
-
};
|
|
157
|
-
this.newClassMap[original] = newClass;
|
|
158
|
-
this.newClassSize++;
|
|
159
|
-
return newClass;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function escapeStringRegexp(str) {
|
|
164
|
-
if (typeof str !== "string") {
|
|
165
|
-
throw new TypeError("Expected a string");
|
|
166
|
-
}
|
|
167
|
-
return str.replaceAll(/[$()*+.?[\\\]^{|}]/g, "\\$&").replaceAll("-", "\\x2d");
|
|
168
|
-
}
|
|
169
|
-
function makeRegex(str, options = {
|
|
170
|
-
exact: true
|
|
171
|
-
}) {
|
|
172
|
-
return new RegExp('(?<=^|[\\s"])' + escapeStringRegexp(str) + (options.exact ? '(?=$|[\\s"])' : ""), "g");
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const validateFilterRE = /[\w%-?\u00A0-\uFFFF-]/;
|
|
176
|
-
function isValidSelector(selector = "") {
|
|
177
|
-
return validateFilterRE.test(selector);
|
|
178
|
-
}
|
|
179
|
-
const splitCode = (code, options = { splitQuote: true }) => {
|
|
180
|
-
const regex = options.splitQuote ? /[\s"]+/ : /\s+/;
|
|
181
|
-
return code.split(regex).filter((x) => isValidSelector(x));
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
exports.ClassGenerator = ClassGenerator;
|
|
185
|
-
exports.acceptChars = acceptChars;
|
|
186
|
-
exports.defaultMangleClassFilter = defaultMangleClassFilter;
|
|
187
|
-
exports.escapeStringRegexp = escapeStringRegexp;
|
|
188
|
-
exports.groupBy = groupBy;
|
|
189
|
-
exports.isMap = isMap;
|
|
190
|
-
exports.isRegexp = isRegexp;
|
|
191
|
-
exports.isValidSelector = isValidSelector;
|
|
192
|
-
exports.makeRegex = makeRegex;
|
|
193
|
-
exports.preserveClassNames = preserveClassNames;
|
|
194
|
-
exports.regExpTest = regExpTest;
|
|
195
|
-
exports.splitCode = splitCode;
|
|
196
|
-
exports.stripEscapeSequence = stripEscapeSequence;
|
|
197
|
-
exports.validateFilterRE = validateFilterRE;
|
package/dist/index.d.cts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
interface IClassGeneratorContextItem {
|
|
2
|
-
name: string;
|
|
3
|
-
usedBy: Set<string>;
|
|
4
|
-
}
|
|
5
|
-
interface IClassGeneratorOptions {
|
|
6
|
-
reserveClassName?: (string | RegExp)[];
|
|
7
|
-
customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined;
|
|
8
|
-
log?: boolean;
|
|
9
|
-
exclude?: (string | RegExp)[];
|
|
10
|
-
include?: (string | RegExp)[];
|
|
11
|
-
ignoreClass?: (string | RegExp)[];
|
|
12
|
-
classPrefix?: string;
|
|
13
|
-
}
|
|
14
|
-
interface IClassGenerator {
|
|
15
|
-
newClassMap: Record<string, IClassGeneratorContextItem>;
|
|
16
|
-
newClassSize: number;
|
|
17
|
-
context: Record<string, any>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
declare class ClassGenerator implements IClassGenerator {
|
|
21
|
-
newClassMap: Record<string, IClassGeneratorContextItem>;
|
|
22
|
-
newClassSize: number;
|
|
23
|
-
context: Record<string, any>;
|
|
24
|
-
opts: IClassGeneratorOptions;
|
|
25
|
-
classPrefix: string;
|
|
26
|
-
constructor(opts?: IClassGeneratorOptions);
|
|
27
|
-
defaultClassGenerate(): string;
|
|
28
|
-
ignoreClassName(className: string): boolean;
|
|
29
|
-
includeFilePath(filePath: string): boolean;
|
|
30
|
-
excludeFilePath(filePath: string): boolean;
|
|
31
|
-
isFileIncluded(filePath: string): boolean;
|
|
32
|
-
transformCssClass(className: string): string;
|
|
33
|
-
generateClassName(original: string): IClassGeneratorContextItem;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
declare function escapeStringRegexp(str: string): string;
|
|
37
|
-
declare function makeRegex(str: string, options?: {
|
|
38
|
-
exact: boolean;
|
|
39
|
-
}): RegExp;
|
|
40
|
-
|
|
41
|
-
declare const validateFilterRE: RegExp;
|
|
42
|
-
declare function isValidSelector(selector?: string): selector is string;
|
|
43
|
-
declare const splitCode: (code: string, options?: {
|
|
44
|
-
splitQuote?: boolean;
|
|
45
|
-
}) => string[];
|
|
46
|
-
|
|
47
|
-
declare const preserveClassNames: string[];
|
|
48
|
-
declare const defaultMangleClassFilter: (className: string) => boolean;
|
|
49
|
-
declare function groupBy<T>(arr: T[], cb: (arg: T) => string): Record<string, T[]>;
|
|
50
|
-
declare const acceptChars: string[];
|
|
51
|
-
declare function stripEscapeSequence(words: string): string;
|
|
52
|
-
declare function isRegexp(value: unknown): boolean;
|
|
53
|
-
declare function isMap(value: unknown): boolean;
|
|
54
|
-
declare function regExpTest(arr: (string | RegExp)[] | undefined, str: string): boolean;
|
|
55
|
-
|
|
56
|
-
export { ClassGenerator, type IClassGenerator, type IClassGeneratorContextItem, type IClassGeneratorOptions, acceptChars, defaultMangleClassFilter, escapeStringRegexp, groupBy, isMap, isRegexp, isValidSelector, makeRegex, preserveClassNames, regExpTest, splitCode, stripEscapeSequence, validateFilterRE };
|
package/dist/index.d.mts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
interface IClassGeneratorContextItem {
|
|
2
|
-
name: string;
|
|
3
|
-
usedBy: Set<string>;
|
|
4
|
-
}
|
|
5
|
-
interface IClassGeneratorOptions {
|
|
6
|
-
reserveClassName?: (string | RegExp)[];
|
|
7
|
-
customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined;
|
|
8
|
-
log?: boolean;
|
|
9
|
-
exclude?: (string | RegExp)[];
|
|
10
|
-
include?: (string | RegExp)[];
|
|
11
|
-
ignoreClass?: (string | RegExp)[];
|
|
12
|
-
classPrefix?: string;
|
|
13
|
-
}
|
|
14
|
-
interface IClassGenerator {
|
|
15
|
-
newClassMap: Record<string, IClassGeneratorContextItem>;
|
|
16
|
-
newClassSize: number;
|
|
17
|
-
context: Record<string, any>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
declare class ClassGenerator implements IClassGenerator {
|
|
21
|
-
newClassMap: Record<string, IClassGeneratorContextItem>;
|
|
22
|
-
newClassSize: number;
|
|
23
|
-
context: Record<string, any>;
|
|
24
|
-
opts: IClassGeneratorOptions;
|
|
25
|
-
classPrefix: string;
|
|
26
|
-
constructor(opts?: IClassGeneratorOptions);
|
|
27
|
-
defaultClassGenerate(): string;
|
|
28
|
-
ignoreClassName(className: string): boolean;
|
|
29
|
-
includeFilePath(filePath: string): boolean;
|
|
30
|
-
excludeFilePath(filePath: string): boolean;
|
|
31
|
-
isFileIncluded(filePath: string): boolean;
|
|
32
|
-
transformCssClass(className: string): string;
|
|
33
|
-
generateClassName(original: string): IClassGeneratorContextItem;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
declare function escapeStringRegexp(str: string): string;
|
|
37
|
-
declare function makeRegex(str: string, options?: {
|
|
38
|
-
exact: boolean;
|
|
39
|
-
}): RegExp;
|
|
40
|
-
|
|
41
|
-
declare const validateFilterRE: RegExp;
|
|
42
|
-
declare function isValidSelector(selector?: string): selector is string;
|
|
43
|
-
declare const splitCode: (code: string, options?: {
|
|
44
|
-
splitQuote?: boolean;
|
|
45
|
-
}) => string[];
|
|
46
|
-
|
|
47
|
-
declare const preserveClassNames: string[];
|
|
48
|
-
declare const defaultMangleClassFilter: (className: string) => boolean;
|
|
49
|
-
declare function groupBy<T>(arr: T[], cb: (arg: T) => string): Record<string, T[]>;
|
|
50
|
-
declare const acceptChars: string[];
|
|
51
|
-
declare function stripEscapeSequence(words: string): string;
|
|
52
|
-
declare function isRegexp(value: unknown): boolean;
|
|
53
|
-
declare function isMap(value: unknown): boolean;
|
|
54
|
-
declare function regExpTest(arr: (string | RegExp)[] | undefined, str: string): boolean;
|
|
55
|
-
|
|
56
|
-
export { ClassGenerator, type IClassGenerator, type IClassGeneratorContextItem, type IClassGeneratorOptions, acceptChars, defaultMangleClassFilter, escapeStringRegexp, groupBy, isMap, isRegexp, isValidSelector, makeRegex, preserveClassNames, regExpTest, splitCode, stripEscapeSequence, validateFilterRE };
|
package/dist/index.d.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
interface IClassGeneratorContextItem {
|
|
2
|
-
name: string;
|
|
3
|
-
usedBy: Set<string>;
|
|
4
|
-
}
|
|
5
|
-
interface IClassGeneratorOptions {
|
|
6
|
-
reserveClassName?: (string | RegExp)[];
|
|
7
|
-
customGenerate?: (original: string, opts: IClassGeneratorOptions, context: Record<string, any>) => string | undefined;
|
|
8
|
-
log?: boolean;
|
|
9
|
-
exclude?: (string | RegExp)[];
|
|
10
|
-
include?: (string | RegExp)[];
|
|
11
|
-
ignoreClass?: (string | RegExp)[];
|
|
12
|
-
classPrefix?: string;
|
|
13
|
-
}
|
|
14
|
-
interface IClassGenerator {
|
|
15
|
-
newClassMap: Record<string, IClassGeneratorContextItem>;
|
|
16
|
-
newClassSize: number;
|
|
17
|
-
context: Record<string, any>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
declare class ClassGenerator implements IClassGenerator {
|
|
21
|
-
newClassMap: Record<string, IClassGeneratorContextItem>;
|
|
22
|
-
newClassSize: number;
|
|
23
|
-
context: Record<string, any>;
|
|
24
|
-
opts: IClassGeneratorOptions;
|
|
25
|
-
classPrefix: string;
|
|
26
|
-
constructor(opts?: IClassGeneratorOptions);
|
|
27
|
-
defaultClassGenerate(): string;
|
|
28
|
-
ignoreClassName(className: string): boolean;
|
|
29
|
-
includeFilePath(filePath: string): boolean;
|
|
30
|
-
excludeFilePath(filePath: string): boolean;
|
|
31
|
-
isFileIncluded(filePath: string): boolean;
|
|
32
|
-
transformCssClass(className: string): string;
|
|
33
|
-
generateClassName(original: string): IClassGeneratorContextItem;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
declare function escapeStringRegexp(str: string): string;
|
|
37
|
-
declare function makeRegex(str: string, options?: {
|
|
38
|
-
exact: boolean;
|
|
39
|
-
}): RegExp;
|
|
40
|
-
|
|
41
|
-
declare const validateFilterRE: RegExp;
|
|
42
|
-
declare function isValidSelector(selector?: string): selector is string;
|
|
43
|
-
declare const splitCode: (code: string, options?: {
|
|
44
|
-
splitQuote?: boolean;
|
|
45
|
-
}) => string[];
|
|
46
|
-
|
|
47
|
-
declare const preserveClassNames: string[];
|
|
48
|
-
declare const defaultMangleClassFilter: (className: string) => boolean;
|
|
49
|
-
declare function groupBy<T>(arr: T[], cb: (arg: T) => string): Record<string, T[]>;
|
|
50
|
-
declare const acceptChars: string[];
|
|
51
|
-
declare function stripEscapeSequence(words: string): string;
|
|
52
|
-
declare function isRegexp(value: unknown): boolean;
|
|
53
|
-
declare function isMap(value: unknown): boolean;
|
|
54
|
-
declare function regExpTest(arr: (string | RegExp)[] | undefined, str: string): boolean;
|
|
55
|
-
|
|
56
|
-
export { ClassGenerator, type IClassGenerator, type IClassGeneratorContextItem, type IClassGeneratorOptions, acceptChars, defaultMangleClassFilter, escapeStringRegexp, groupBy, isMap, isRegexp, isValidSelector, makeRegex, preserveClassNames, regExpTest, splitCode, stripEscapeSequence, validateFilterRE };
|
package/dist/index.mjs
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
const preserveClassNames = [
|
|
2
|
-
// https://tailwindcss.com/docs/transition-timing-function start
|
|
3
|
-
// https://github.com/sonofmagic/tailwindcss-mangle/issues/21
|
|
4
|
-
"ease-out",
|
|
5
|
-
"ease-linear",
|
|
6
|
-
"ease-in",
|
|
7
|
-
"ease-in-out"
|
|
8
|
-
// https://tailwindcss.com/docs/transition-timing-function end
|
|
9
|
-
];
|
|
10
|
-
const preserveClassNamesMap = preserveClassNames.reduce((acc, cur) => {
|
|
11
|
-
acc[cur] = true;
|
|
12
|
-
return acc;
|
|
13
|
-
}, {});
|
|
14
|
-
const defaultMangleClassFilter = (className) => {
|
|
15
|
-
if (preserveClassNamesMap[className]) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
return /[:-]/.test(className);
|
|
19
|
-
};
|
|
20
|
-
function groupBy(arr, cb) {
|
|
21
|
-
if (!Array.isArray(arr)) {
|
|
22
|
-
throw new TypeError("expected an array for first argument");
|
|
23
|
-
}
|
|
24
|
-
if (typeof cb !== "function") {
|
|
25
|
-
throw new TypeError("expected a function for second argument");
|
|
26
|
-
}
|
|
27
|
-
const result = {};
|
|
28
|
-
for (const item of arr) {
|
|
29
|
-
const bucketCategory = cb(item);
|
|
30
|
-
const bucket = result[bucketCategory];
|
|
31
|
-
if (Array.isArray(bucket)) {
|
|
32
|
-
result[bucketCategory].push(item);
|
|
33
|
-
} else {
|
|
34
|
-
result[bucketCategory] = [item];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return result;
|
|
38
|
-
}
|
|
39
|
-
const acceptChars = [..."abcdefghijklmnopqrstuvwxyz"];
|
|
40
|
-
function stripEscapeSequence(words) {
|
|
41
|
-
return words.replaceAll("\\", "");
|
|
42
|
-
}
|
|
43
|
-
function isRegexp(value) {
|
|
44
|
-
return Object.prototype.toString.call(value) === "[object RegExp]";
|
|
45
|
-
}
|
|
46
|
-
function isMap(value) {
|
|
47
|
-
return Object.prototype.toString.call(value) === "[object Map]";
|
|
48
|
-
}
|
|
49
|
-
function regExpTest(arr = [], str) {
|
|
50
|
-
if (Array.isArray(arr)) {
|
|
51
|
-
for (const item of arr) {
|
|
52
|
-
if (typeof item === "string") {
|
|
53
|
-
if (item === str) {
|
|
54
|
-
return true;
|
|
55
|
-
}
|
|
56
|
-
} else if (isRegexp(item)) {
|
|
57
|
-
item.lastIndex = 0;
|
|
58
|
-
if (item.test(str)) {
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
throw new TypeError("paramater 'arr' should be a Array of Regexp | String !");
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
var __defProp = Object.defineProperty;
|
|
69
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
70
|
-
var __publicField = (obj, key, value) => {
|
|
71
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
72
|
-
return value;
|
|
73
|
-
};
|
|
74
|
-
class ClassGenerator {
|
|
75
|
-
constructor(opts = {}) {
|
|
76
|
-
__publicField(this, "newClassMap");
|
|
77
|
-
__publicField(this, "newClassSize");
|
|
78
|
-
__publicField(this, "context");
|
|
79
|
-
__publicField(this, "opts");
|
|
80
|
-
__publicField(this, "classPrefix");
|
|
81
|
-
this.newClassMap = {};
|
|
82
|
-
this.newClassSize = 0;
|
|
83
|
-
this.context = {};
|
|
84
|
-
this.opts = opts;
|
|
85
|
-
this.classPrefix = opts.classPrefix ?? "tw-";
|
|
86
|
-
}
|
|
87
|
-
defaultClassGenerate() {
|
|
88
|
-
const chars = [];
|
|
89
|
-
let rest = (this.newClassSize - this.newClassSize % acceptChars.length) / acceptChars.length;
|
|
90
|
-
if (rest > 0) {
|
|
91
|
-
while (true) {
|
|
92
|
-
rest -= 1;
|
|
93
|
-
const m = rest % acceptChars.length;
|
|
94
|
-
const c = acceptChars[m];
|
|
95
|
-
chars.push(c);
|
|
96
|
-
rest -= m;
|
|
97
|
-
if (rest === 0) {
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
rest /= acceptChars.length;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
const prefixIndex = this.newClassSize % acceptChars.length;
|
|
104
|
-
const newClassName = `${this.classPrefix}${acceptChars[prefixIndex]}${chars.join("")}`;
|
|
105
|
-
return newClassName;
|
|
106
|
-
}
|
|
107
|
-
ignoreClassName(className) {
|
|
108
|
-
return regExpTest(this.opts.ignoreClass, className);
|
|
109
|
-
}
|
|
110
|
-
includeFilePath(filePath) {
|
|
111
|
-
const { include } = this.opts;
|
|
112
|
-
return Array.isArray(include) ? regExpTest(include, filePath) : true;
|
|
113
|
-
}
|
|
114
|
-
excludeFilePath(filePath) {
|
|
115
|
-
const { exclude } = this.opts;
|
|
116
|
-
return Array.isArray(exclude) ? regExpTest(exclude, filePath) : false;
|
|
117
|
-
}
|
|
118
|
-
isFileIncluded(filePath) {
|
|
119
|
-
return this.includeFilePath(filePath) && !this.excludeFilePath(filePath);
|
|
120
|
-
}
|
|
121
|
-
transformCssClass(className) {
|
|
122
|
-
const key = stripEscapeSequence(className);
|
|
123
|
-
const cn = this.newClassMap[key];
|
|
124
|
-
if (cn)
|
|
125
|
-
return cn.name;
|
|
126
|
-
return className;
|
|
127
|
-
}
|
|
128
|
-
generateClassName(original) {
|
|
129
|
-
const opts = this.opts;
|
|
130
|
-
original = stripEscapeSequence(original);
|
|
131
|
-
const cn = this.newClassMap[original];
|
|
132
|
-
if (cn)
|
|
133
|
-
return cn;
|
|
134
|
-
let newClassName;
|
|
135
|
-
if (opts.customGenerate && typeof opts.customGenerate === "function") {
|
|
136
|
-
newClassName = opts.customGenerate(original, opts, this.context);
|
|
137
|
-
}
|
|
138
|
-
if (!newClassName) {
|
|
139
|
-
newClassName = this.defaultClassGenerate();
|
|
140
|
-
}
|
|
141
|
-
if (opts.reserveClassName && regExpTest(opts.reserveClassName, newClassName)) {
|
|
142
|
-
if (opts.log) {
|
|
143
|
-
console.log(`The class name has been reserved. ${newClassName}`);
|
|
144
|
-
}
|
|
145
|
-
this.newClassSize++;
|
|
146
|
-
return this.generateClassName(original);
|
|
147
|
-
}
|
|
148
|
-
if (opts.log) {
|
|
149
|
-
console.log(`Minify class name from ${original} to ${newClassName}`);
|
|
150
|
-
}
|
|
151
|
-
const newClass = {
|
|
152
|
-
name: newClassName,
|
|
153
|
-
usedBy: /* @__PURE__ */ new Set()
|
|
154
|
-
};
|
|
155
|
-
this.newClassMap[original] = newClass;
|
|
156
|
-
this.newClassSize++;
|
|
157
|
-
return newClass;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function escapeStringRegexp(str) {
|
|
162
|
-
if (typeof str !== "string") {
|
|
163
|
-
throw new TypeError("Expected a string");
|
|
164
|
-
}
|
|
165
|
-
return str.replaceAll(/[$()*+.?[\\\]^{|}]/g, "\\$&").replaceAll("-", "\\x2d");
|
|
166
|
-
}
|
|
167
|
-
function makeRegex(str, options = {
|
|
168
|
-
exact: true
|
|
169
|
-
}) {
|
|
170
|
-
return new RegExp('(?<=^|[\\s"])' + escapeStringRegexp(str) + (options.exact ? '(?=$|[\\s"])' : ""), "g");
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const validateFilterRE = /[\w%-?\u00A0-\uFFFF-]/;
|
|
174
|
-
function isValidSelector(selector = "") {
|
|
175
|
-
return validateFilterRE.test(selector);
|
|
176
|
-
}
|
|
177
|
-
const splitCode = (code, options = { splitQuote: true }) => {
|
|
178
|
-
const regex = options.splitQuote ? /[\s"]+/ : /\s+/;
|
|
179
|
-
return code.split(regex).filter((x) => isValidSelector(x));
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
export { ClassGenerator, acceptChars, defaultMangleClassFilter, escapeStringRegexp, groupBy, isMap, isRegexp, isValidSelector, makeRegex, preserveClassNames, regExpTest, splitCode, stripEscapeSequence, validateFilterRE };
|