@projectwallace/format-css 2.1.0 → 2.1.2
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/format-css.js +98 -103
- package/dist/index.d.ts +8 -21
- package/package.json +17 -11
package/dist/format-css.js
CHANGED
|
@@ -1,193 +1,188 @@
|
|
|
1
|
-
import
|
|
2
|
-
const
|
|
1
|
+
import { parse as te } from "css-tree";
|
|
2
|
+
const o = " ", p = "", $ = ":", x = ";", L = '"', Y = "(", g = ")", re = "[", ne = "]", V = "{", W = "}", le = "{}", Z = ",", j = "Atrule", F = "Rule", G = "Block", Q = "SelectorList", I = "Selector", q = "PseudoElementSelector", w = "Declaration", B = "Operator";
|
|
3
3
|
function C(y) {
|
|
4
4
|
return /[A-Z]/.test(y) ? y.toLowerCase() : y;
|
|
5
5
|
}
|
|
6
|
-
function
|
|
7
|
-
|
|
8
|
-
tab_size: T = void 0
|
|
9
|
-
} = /* @__PURE__ */ Object.create(null)) {
|
|
10
|
-
if (T !== void 0 && Number(T) < 1)
|
|
6
|
+
function fe(y, { minify: T = !1, tab_size: P = void 0 } = /* @__PURE__ */ Object.create(null)) {
|
|
7
|
+
if (P !== void 0 && Number(P) < 1)
|
|
11
8
|
throw new TypeError("tab_size must be a number greater than 0");
|
|
12
|
-
let
|
|
9
|
+
let S = [];
|
|
10
|
+
function J(n, e) {
|
|
11
|
+
S.push(e.start.offset, e.end.offset);
|
|
12
|
+
}
|
|
13
|
+
let N = te(y, {
|
|
13
14
|
positions: !0,
|
|
14
15
|
parseAtrulePrelude: !1,
|
|
15
16
|
parseCustomProperty: !0,
|
|
16
17
|
parseValue: !0,
|
|
17
|
-
onComment:
|
|
18
|
-
A.push(e.start.offset, e.end.offset);
|
|
19
|
-
}
|
|
18
|
+
onComment: J
|
|
20
19
|
});
|
|
21
|
-
const u =
|
|
22
|
-
`,
|
|
20
|
+
const u = T ? p : `
|
|
21
|
+
`, c = T ? p : o, X = T ? p : x;
|
|
23
22
|
let s = 0;
|
|
24
|
-
function
|
|
25
|
-
return
|
|
23
|
+
function a(n) {
|
|
24
|
+
return T === !0 ? p : P !== void 0 ? o.repeat(P * n) : " ".repeat(n);
|
|
26
25
|
}
|
|
27
|
-
function
|
|
26
|
+
function O(n) {
|
|
28
27
|
let e = n.loc;
|
|
29
|
-
return e ? y.slice(e.start.offset, e.end.offset)
|
|
28
|
+
return e == null ? p : y.slice(e.start.offset, e.end.offset);
|
|
30
29
|
}
|
|
31
30
|
function b(n) {
|
|
32
|
-
|
|
31
|
+
var e;
|
|
32
|
+
return (e = n.loc) == null ? void 0 : e.start.offset;
|
|
33
33
|
}
|
|
34
34
|
function E(n) {
|
|
35
|
-
|
|
35
|
+
var e;
|
|
36
|
+
return (e = n.loc) == null ? void 0 : e.end.offset;
|
|
36
37
|
}
|
|
37
38
|
function m(n, e) {
|
|
38
|
-
if (
|
|
39
|
-
return
|
|
40
|
-
let t =
|
|
41
|
-
for (let r = 0; r <
|
|
42
|
-
let l =
|
|
39
|
+
if (T === !0 || n === void 0 || e === void 0)
|
|
40
|
+
return p;
|
|
41
|
+
let t = p;
|
|
42
|
+
for (let r = 0; r < S.length; r += 2) {
|
|
43
|
+
let l = S[r];
|
|
43
44
|
if (l === void 0 || l < n) continue;
|
|
44
|
-
let f =
|
|
45
|
+
let f = S[r + 1];
|
|
45
46
|
if (f === void 0 || f > e) break;
|
|
46
|
-
t.length > 0 && (t += u +
|
|
47
|
+
t.length > 0 && (t += u + a(s)), t += y.slice(l, f);
|
|
47
48
|
}
|
|
48
49
|
return t;
|
|
49
50
|
}
|
|
50
51
|
function M(n) {
|
|
51
|
-
let e, t = n.prelude, r = n.block;
|
|
52
|
-
t.type === Q && (e =
|
|
52
|
+
let e = "", t = n.prelude, r = n.block;
|
|
53
|
+
t.type === Q && (e = z(t));
|
|
53
54
|
let l = m(E(t), b(r));
|
|
54
|
-
return l && (e += u +
|
|
55
|
+
return l && (e += u + a(s) + l), r.type === G && (e += U(r)), e;
|
|
55
56
|
}
|
|
56
|
-
function
|
|
57
|
-
let e =
|
|
57
|
+
function z(n) {
|
|
58
|
+
let e = p;
|
|
58
59
|
return n.children.forEach((t, r) => {
|
|
59
|
-
t.type ===
|
|
60
|
+
t.type === I && (e += a(s) + v(t)), r.next !== null && (e += Z + u);
|
|
60
61
|
let l = r.next !== null ? b(r.next.data) : E(n), f = m(E(t), l);
|
|
61
|
-
f && (e +=
|
|
62
|
+
f && (e += a(s) + f + u);
|
|
62
63
|
}), e;
|
|
63
64
|
}
|
|
64
|
-
function
|
|
65
|
-
let e =
|
|
66
|
-
return
|
|
65
|
+
function v(n) {
|
|
66
|
+
let e = p, t = n.children;
|
|
67
|
+
return t == null || t.forEach((r) => {
|
|
67
68
|
switch (r.type) {
|
|
68
69
|
case "TypeSelector": {
|
|
69
70
|
e += C(r.name);
|
|
70
71
|
break;
|
|
71
72
|
}
|
|
72
73
|
case "Combinator": {
|
|
73
|
-
|
|
74
|
+
r.name === " " ? e += o : e += c + r.name + c;
|
|
74
75
|
break;
|
|
75
76
|
}
|
|
76
77
|
case "PseudoClassSelector":
|
|
77
|
-
case
|
|
78
|
+
case q: {
|
|
78
79
|
e += $;
|
|
79
80
|
let l = C(r.name);
|
|
80
|
-
(l === "before" || l === "after" || r.type ===
|
|
81
|
+
(l === "before" || l === "after" || r.type === q) && (e += $), e += l, r.children !== null && (e += Y + v(r) + g);
|
|
81
82
|
break;
|
|
82
83
|
}
|
|
83
84
|
case Q: {
|
|
84
85
|
r.children.forEach((l, f) => {
|
|
85
|
-
l.type ===
|
|
86
|
+
l.type === I && (e += v(l)), f.next !== null && f.next.data.type === I && (e += Z + c);
|
|
86
87
|
});
|
|
87
88
|
break;
|
|
88
89
|
}
|
|
89
90
|
case "Nth": {
|
|
90
91
|
let l = r.nth;
|
|
91
|
-
if (l)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
r.selector !== null && (e += a + "of" + a + S(r.selector));
|
|
92
|
+
if (l.type === "AnPlusB") {
|
|
93
|
+
let f = l.a, i = l.b;
|
|
94
|
+
f !== null && (e += f + "n"), f !== null && i !== null && (e += o), i !== null && (f !== null && !i.startsWith("-") && (e += "+" + o), e += i);
|
|
95
|
+
} else
|
|
96
|
+
e += O(l);
|
|
97
|
+
r.selector !== null && (e += o + "of" + o + v(r.selector));
|
|
98
98
|
break;
|
|
99
99
|
}
|
|
100
100
|
case "AttributeSelector": {
|
|
101
|
-
e +=
|
|
101
|
+
e += re, e += r.name.name, r.matcher !== null && r.value !== null && (e += r.matcher, e += L, r.value.type === "String" ? e += r.value.value : r.value.type === "Identifier" && (e += r.value.name), e += L), r.flags !== null && (e += o + r.flags), e += ne;
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
case "NestingSelector": {
|
|
105
|
+
e += "&";
|
|
102
106
|
break;
|
|
103
107
|
}
|
|
104
108
|
default: {
|
|
105
|
-
e +=
|
|
109
|
+
e += O(r);
|
|
106
110
|
break;
|
|
107
111
|
}
|
|
108
112
|
}
|
|
109
113
|
}), e;
|
|
110
114
|
}
|
|
111
|
-
function
|
|
112
|
-
let e = n.children, t =
|
|
115
|
+
function U(n) {
|
|
116
|
+
let e = n.children, t = c;
|
|
113
117
|
if (e.isEmpty) {
|
|
114
118
|
let f = m(b(n), E(n));
|
|
115
|
-
return f ? (t += V + u, t +=
|
|
119
|
+
return f ? (t += V + u, t += a(s + 1) + f, t += u + a(s) + W, t) : t + le;
|
|
116
120
|
}
|
|
117
121
|
t += V + u, s++;
|
|
118
|
-
let r = m(b(n), b(
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (o.prev !== null) {
|
|
124
|
-
let H = m(E(o.prev.data), b(f));
|
|
125
|
-
H && (t += i(s) + H + u);
|
|
122
|
+
let r = m(b(n), b(e.first));
|
|
123
|
+
r && (t += a(s) + r + u), e.forEach((f, i) => {
|
|
124
|
+
if (i.prev !== null) {
|
|
125
|
+
let H = m(E(i.prev.data), b(f));
|
|
126
|
+
H && (t += a(s) + H + u);
|
|
126
127
|
}
|
|
127
|
-
f.type === w ? (t +=
|
|
128
|
+
f.type === w ? (t += d(f), i.next === null ? t += X : t += x) : (i.prev !== null && i.prev.data.type === w && (t += u), f.type === F ? t += M(f) : f.type === j ? t += D(f) : t += k(f, s)), i.next !== null && (t += u, f.type !== w && (t += u));
|
|
128
129
|
});
|
|
129
|
-
let l = m(E(
|
|
130
|
-
|
|
131
|
-
e.last
|
|
132
|
-
), E(n));
|
|
133
|
-
return l && (t += u + i(s) + l), s--, t += u + i(s) + W, t;
|
|
130
|
+
let l = m(E(e.last), E(n));
|
|
131
|
+
return l && (t += u + a(s) + l), s--, t += u + a(s) + W, t;
|
|
134
132
|
}
|
|
135
|
-
function
|
|
136
|
-
let e =
|
|
137
|
-
return e += C(n.name), t !== null && (e +=
|
|
133
|
+
function D(n) {
|
|
134
|
+
let e = a(s) + "@", t = n.prelude, r = n.block;
|
|
135
|
+
return e += C(n.name), t !== null && (e += o + h(t)), r === null ? e += x : r.type === G && (e += U(r)), e;
|
|
138
136
|
}
|
|
139
|
-
function
|
|
140
|
-
let e =
|
|
141
|
-
return e.replace(/\s*([:,])/g, e.toLowerCase().includes("selector(") ? "$1" : "$1 ").replace(/\)([a-zA-Z])/g, ") $1").replace(/\s*(=>|<=)\s*/g, " $1 ").replace(/([^<>=\s])([<>])([^<>=\s])/g, `$1${
|
|
142
|
-
let
|
|
143
|
-
return `calc(${r.trim()}${
|
|
144
|
-
}).replace(/selector|url|supports|layer\(/
|
|
137
|
+
function h(n) {
|
|
138
|
+
let e = O(n);
|
|
139
|
+
return e.replace(/\s*([:,])/g, e.toLowerCase().includes("selector(") ? "$1" : "$1 ").replace(/\)([a-zA-Z])/g, ") $1").replace(/\s*(=>|<=)\s*/g, " $1 ").replace(/([^<>=\s])([<>])([^<>=\s])/g, `$1${c}$2${c}$3`).replace(/\s+/g, c).replace(/calc\(\s*([^()+\-*/]+)\s*([*/+-])\s*([^()+\-*/]+)\s*\)/g, (t, r, l, f) => {
|
|
140
|
+
let i = l === "+" || l === "-" ? o : c;
|
|
141
|
+
return `calc(${r.trim()}${i}${l}${i}${f.trim()})`;
|
|
142
|
+
}).replace(/selector|url|supports|layer\(/gi, (t) => C(t));
|
|
145
143
|
}
|
|
146
|
-
function
|
|
144
|
+
function d(n) {
|
|
147
145
|
let e = n.property;
|
|
148
146
|
e.charCodeAt(0) === 45 && e.charCodeAt(1) === 45 || (e = C(e));
|
|
149
|
-
let t =
|
|
150
|
-
return e === "font" && (t = t.replace(/\s*\/\s*/, "/")), t ===
|
|
147
|
+
let t = K(n.value);
|
|
148
|
+
return e === "font" && (t = t.replace(/\s*\/\s*/, "/")), t === p && T === !0 && (t += o), n.important === !0 ? t += c + "!important" : typeof n.important == "string" && (t += c + "!" + C(n.important)), a(s) + e + $ + c + t;
|
|
151
149
|
}
|
|
152
|
-
function
|
|
153
|
-
let e =
|
|
150
|
+
function R(n) {
|
|
151
|
+
let e = p;
|
|
154
152
|
return n.forEach((t, r) => {
|
|
155
|
-
t.type === "Identifier" ? e += t.name : t.type === "Function" ? e += C(t.name) +
|
|
153
|
+
t.type === "Identifier" ? e += t.name : t.type === "Function" ? e += C(t.name) + Y + R(t.children) + g : t.type === "Dimension" ? e += t.value + C(t.unit) : t.type === "Value" ? e += K(t) : t.type === B ? e += ee(t) : t.type === "Parentheses" ? e += Y + R(t.children) + g : t.type === "Url" ? e += "url(" + L + t.value + L + g : e += O(t), t.type !== B && r.next !== null && r.next.data.type !== B && (e += o);
|
|
156
154
|
}), e;
|
|
157
155
|
}
|
|
158
|
-
function
|
|
159
|
-
let e =
|
|
160
|
-
return r === 43 || r === 45 ? e +=
|
|
156
|
+
function ee(n) {
|
|
157
|
+
let e = p, t = n.value.trim(), r = t.charCodeAt(0);
|
|
158
|
+
return r === 43 || r === 45 ? e += o : r !== 44 && (e += c), e += t, r === 43 || r === 45 ? e += o : e += c, e;
|
|
161
159
|
}
|
|
162
|
-
function
|
|
163
|
-
return n.type === "Raw" ?
|
|
160
|
+
function K(n) {
|
|
161
|
+
return n.type === "Raw" ? k(n, 0) : R(n.children);
|
|
164
162
|
}
|
|
165
|
-
function
|
|
166
|
-
return
|
|
163
|
+
function k(n, e) {
|
|
164
|
+
return a(e) + O(n).trim();
|
|
167
165
|
}
|
|
168
|
-
let
|
|
169
|
-
if (
|
|
170
|
-
let n = m(0, b(
|
|
171
|
-
n && (_ += n + u),
|
|
172
|
-
if (t.type === F ? _ += M(t) : t.type === j ? _ +=
|
|
166
|
+
let A = N.children, _ = p;
|
|
167
|
+
if (A.first !== null) {
|
|
168
|
+
let n = m(0, b(A.first));
|
|
169
|
+
n && (_ += n + u), A.forEach((t, r) => {
|
|
170
|
+
if (t.type === F ? _ += M(t) : t.type === j ? _ += D(t) : _ += k(t, s), r.next !== null) {
|
|
173
171
|
_ += u;
|
|
174
172
|
let l = m(E(t), b(r.next.data));
|
|
175
|
-
l && (_ +=
|
|
173
|
+
l && (_ += a(s) + l), _ += u;
|
|
176
174
|
}
|
|
177
175
|
});
|
|
178
|
-
let e = m(E(
|
|
179
|
-
/** @type {import('css-tree').CssNode} */
|
|
180
|
-
L.last
|
|
181
|
-
), E(R));
|
|
176
|
+
let e = m(E(A.last), E(N));
|
|
182
177
|
e && (_ += u + e);
|
|
183
178
|
} else
|
|
184
|
-
_ += m(0, E(
|
|
179
|
+
_ += m(0, E(N));
|
|
185
180
|
return _;
|
|
186
181
|
}
|
|
187
|
-
function
|
|
188
|
-
return
|
|
182
|
+
function se(y) {
|
|
183
|
+
return fe(y, { minify: !0 });
|
|
189
184
|
}
|
|
190
185
|
export {
|
|
191
|
-
|
|
192
|
-
|
|
186
|
+
fe as format,
|
|
187
|
+
se as minify
|
|
193
188
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,29 +1,16 @@
|
|
|
1
|
+
export type FormatOptions = {
|
|
2
|
+
/** Whether to minify the CSS or keep it formatted */
|
|
3
|
+
minify?: boolean;
|
|
4
|
+
/** Tell the formatter to use N spaces instead of tabs */
|
|
5
|
+
tab_size?: number;
|
|
6
|
+
};
|
|
1
7
|
/**
|
|
2
|
-
* @typedef {Object} Options
|
|
3
|
-
* @property {boolean} [minify] Whether to minify the CSS or keep it formatted
|
|
4
|
-
* @property {number} [tab_size] Tell the formatter to use N spaces instead of tabs
|
|
5
|
-
*
|
|
6
8
|
* Format a string of CSS using some simple rules
|
|
7
|
-
* @param {string} css The original CSS
|
|
8
|
-
* @param {Options} options
|
|
9
|
-
* @returns {string} The formatted CSS
|
|
10
9
|
*/
|
|
11
|
-
export function format(css: string, { minify, tab_size
|
|
10
|
+
export declare function format(css: string, { minify, tab_size }?: FormatOptions): string;
|
|
12
11
|
/**
|
|
13
12
|
* Minify a string of CSS
|
|
14
13
|
* @param {string} css The original CSS
|
|
15
14
|
* @returns {string} The minified CSS
|
|
16
15
|
*/
|
|
17
|
-
export function minify(css: string): string;
|
|
18
|
-
export type Options = {
|
|
19
|
-
/**
|
|
20
|
-
* Whether to minify the CSS or keep it formatted
|
|
21
|
-
*/
|
|
22
|
-
minify?: boolean | undefined;
|
|
23
|
-
/**
|
|
24
|
-
* Tell the formatter to use N spaces instead of tabs
|
|
25
|
-
*
|
|
26
|
-
* Format a string of CSS using some simple rules
|
|
27
|
-
*/
|
|
28
|
-
tab_size?: number | undefined;
|
|
29
|
-
};
|
|
16
|
+
export declare function minify(css: string): string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@projectwallace/format-css",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.2",
|
|
4
4
|
"description": "Fast, small, zero-config library to format or minify CSS with basic rules.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"homepage": "https://github.com/projectwallace/format-css",
|
|
10
10
|
"issues": "https://github.com/projectwallace/format-css/issues",
|
|
11
11
|
"license": "MIT",
|
|
12
|
+
"author": "Bart Veneman <bart@projectwallace.com>",
|
|
12
13
|
"engines": {
|
|
13
14
|
"node": ">=18.0.0"
|
|
14
15
|
},
|
|
@@ -21,22 +22,27 @@
|
|
|
21
22
|
"types": "./dist/index.d.ts",
|
|
22
23
|
"scripts": {
|
|
23
24
|
"build": "vite build",
|
|
24
|
-
"test": "
|
|
25
|
+
"test": "vitest --coverage",
|
|
25
26
|
"check": "tsc",
|
|
26
|
-
"prettier": "prettier --check index.
|
|
27
|
+
"prettier": "prettier --check index.ts test/**/*.ts",
|
|
28
|
+
"lint": "oxlint",
|
|
29
|
+
"lint-package": "publint"
|
|
27
30
|
},
|
|
28
31
|
"devDependencies": {
|
|
29
|
-
"@codecov/vite-plugin": "^1.9.
|
|
30
|
-
"@types/css-tree": "^2.3.
|
|
32
|
+
"@codecov/vite-plugin": "^1.9.1",
|
|
33
|
+
"@types/css-tree": "^2.3.11",
|
|
34
|
+
"@vitest/coverage-v8": "^4.0.3",
|
|
31
35
|
"c8": "^10.1.3",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
36
|
+
"oxlint": "^1.24.0",
|
|
37
|
+
"prettier": "^3.6.2",
|
|
38
|
+
"publint": "^0.3.15",
|
|
39
|
+
"typescript": "^5.9.3",
|
|
35
40
|
"vite": "^6.2.6",
|
|
36
|
-
"vite-plugin-dts": "^4.5.
|
|
41
|
+
"vite-plugin-dts": "^4.5.4",
|
|
42
|
+
"vitest": "^4.0.3"
|
|
37
43
|
},
|
|
38
44
|
"dependencies": {
|
|
39
|
-
"css-tree": "^3.0
|
|
45
|
+
"css-tree": "^3.1.0"
|
|
40
46
|
},
|
|
41
47
|
"files": [
|
|
42
48
|
"dist"
|
|
@@ -57,4 +63,4 @@
|
|
|
57
63
|
"printWidth": 140,
|
|
58
64
|
"singleQuote": true
|
|
59
65
|
}
|
|
60
|
-
}
|
|
66
|
+
}
|