@projectwallace/format-css 2.1.1 → 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.
@@ -1,103 +1,104 @@
1
- import d from "css-tree/parser";
2
- const a = " ", c = "", $ = ":", x = ";", g = '"', I = "(", v = ")", ee = "[", te = "]", V = "{", W = "}", re = "{}", Z = ",", j = "Atrule", F = "Rule", G = "Block", Q = "SelectorList", Y = "Selector", w = "Declaration", B = "Operator";
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 ne(y, {
7
- minify: O = !1,
8
- tab_size: S = void 0
9
- } = /* @__PURE__ */ Object.create(null)) {
10
- if (S !== void 0 && Number(S) < 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 T = [], k = d(y, {
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: (n, e) => {
18
- T.push(e.start.offset, e.end.offset);
19
- }
18
+ onComment: J
20
19
  });
21
- const u = O ? c : `
22
- `, p = O ? c : a, h = O ? c : x;
20
+ const u = T ? p : `
21
+ `, c = T ? p : o, X = T ? p : x;
23
22
  let s = 0;
24
- function i(n) {
25
- return O === !0 ? c : S !== void 0 ? a.repeat(S * n) : " ".repeat(n);
23
+ function a(n) {
24
+ return T === !0 ? p : P !== void 0 ? o.repeat(P * n) : " ".repeat(n);
26
25
  }
27
- function P(n) {
26
+ function O(n) {
28
27
  let e = n.loc;
29
- return e == null ? c : 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
- return /** @type {import('css-tree').CssLocation} */ n.loc.start.offset;
31
+ var e;
32
+ return (e = n.loc) == null ? void 0 : e.start.offset;
33
33
  }
34
34
  function E(n) {
35
- return /** @type {import('css-tree').CssLocation} */ n.loc.end.offset;
35
+ var e;
36
+ return (e = n.loc) == null ? void 0 : e.end.offset;
36
37
  }
37
38
  function m(n, e) {
38
- if (O === !0 || n === void 0 || e === void 0)
39
- return c;
40
- let t = "";
41
- for (let r = 0; r < T.length; r += 2) {
42
- let l = T[r];
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 = T[r + 1];
45
+ let f = S[r + 1];
45
46
  if (f === void 0 || f > e) break;
46
- t.length > 0 && (t += u + i(s)), t += y.slice(l, f);
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 = q(t));
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 + i(s) + l), r.type === G && (e += K(r)), e;
55
+ return l && (e += u + a(s) + l), r.type === G && (e += U(r)), e;
55
56
  }
56
- function q(n) {
57
- let e = c;
57
+ function z(n) {
58
+ let e = p;
58
59
  return n.children.forEach((t, r) => {
59
- t.type === Y && (e += i(s) + A(t)), r.next !== null && (e += Z + u);
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 += i(s) + f + u);
62
+ f && (e += a(s) + f + u);
62
63
  }), e;
63
64
  }
64
- function A(n) {
65
- let e = c;
66
- return (n.children || []).forEach((r) => {
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
- e += a, r.name !== " " && (e += r.name + a);
74
+ r.name === " " ? e += o : e += c + r.name + c;
74
75
  break;
75
76
  }
76
77
  case "PseudoClassSelector":
77
- case "PseudoElementSelector": {
78
+ case q: {
78
79
  e += $;
79
80
  let l = C(r.name);
80
- (l === "before" || l === "after" || r.type === "PseudoElementSelector") && (e += $), e += l, r.children !== null && (e += I + A(r) + v);
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 === Y && (e += A(l)), f.next !== null && f.next.data.type === Y && (e += Z + p);
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
92
  if (l.type === "AnPlusB") {
92
- let f = l.a, o = l.b;
93
- f !== null && (e += f + "n"), f !== null && o !== null && (e += a), o !== null && (f !== null && !o.startsWith("-") && (e += "+" + a), e += o);
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);
94
95
  } else
95
- e += P(l);
96
- r.selector !== null && (e += a + "of" + a + A(r.selector));
96
+ e += O(l);
97
+ r.selector !== null && (e += o + "of" + o + v(r.selector));
97
98
  break;
98
99
  }
99
100
  case "AttributeSelector": {
100
- e += ee, e += r.name.name, r.matcher !== null && r.value !== null && (e += r.matcher, e += g, r.value.type === "String" ? e += r.value.value : r.value.type === "Identifier" && (e += r.value.name), e += g), r.flags !== null && (e += a + r.flags), e += te;
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;
101
102
  break;
102
103
  }
103
104
  case "NestingSelector": {
@@ -105,92 +106,83 @@ function ne(y, {
105
106
  break;
106
107
  }
107
108
  default: {
108
- e += P(r);
109
+ e += O(r);
109
110
  break;
110
111
  }
111
112
  }
112
113
  }), e;
113
114
  }
114
- function K(n) {
115
- let e = n.children, t = p;
115
+ function U(n) {
116
+ let e = n.children, t = c;
116
117
  if (e.isEmpty) {
117
118
  let f = m(b(n), E(n));
118
- return f ? (t += V + u, t += i(s + 1) + f, t += u + i(s) + W, t) : t + re;
119
+ return f ? (t += V + u, t += a(s + 1) + f, t += u + a(s) + W, t) : t + le;
119
120
  }
120
121
  t += V + u, s++;
121
- let r = m(b(n), b(
122
- /** @type {import('css-tree').CssNode} */
123
- e.first
124
- ));
125
- r && (t += i(s) + r + u), e.forEach((f, o) => {
126
- if (o.prev !== null) {
127
- let H = m(E(o.prev.data), b(f));
128
- 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);
129
127
  }
130
- f.type === w ? (t += X(f), o.next === null ? t += h : t += x) : (o.prev !== null && o.prev.data.type === w && (t += u), f.type === F ? t += M(f) : f.type === j ? t += U(f) : t += R(f, s)), o.next !== null && (t += u, f.type !== w && (t += u));
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));
131
129
  });
132
- let l = m(E(
133
- /** @type {import('css-tree').CssNode} */
134
- e.last
135
- ), E(n));
136
- 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;
137
132
  }
138
- function U(n) {
139
- let e = i(s) + "@", t = n.prelude, r = n.block;
140
- return e += C(n.name), t !== null && (e += a + J(t)), r === null ? e += x : r.type === G && (e += K(r)), 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;
141
136
  }
142
- function J(n) {
143
- let e = P(n);
144
- 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${p}$2${p}$3`).replace(/\s+/g, p).replace(/calc\(\s*([^()+\-*/]+)\s*([*/+-])\s*([^()+\-*/]+)\s*\)/g, (t, r, l, f) => {
145
- let o = l === "+" || l === "-" ? a : p;
146
- return `calc(${r.trim()}${o}${l}${o}${f.trim()})`;
147
- }).replace(/selector|url|supports|layer\(/ig, (t) => C(t));
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));
148
143
  }
149
- function X(n) {
144
+ function d(n) {
150
145
  let e = n.property;
151
146
  e.charCodeAt(0) === 45 && e.charCodeAt(1) === 45 || (e = C(e));
152
- let t = D(n.value);
153
- return e === "font" && (t = t.replace(/\s*\/\s*/, "/")), t === c && O === !0 && (t += a), n.important === !0 ? t += p + "!important" : typeof n.important == "string" && (t += p + "!" + C(n.important)), i(s) + e + $ + p + 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;
154
149
  }
155
- function N(n) {
156
- let e = c;
150
+ function R(n) {
151
+ let e = p;
157
152
  return n.forEach((t, r) => {
158
- t.type === "Identifier" ? e += t.name : t.type === "Function" ? e += C(t.name) + I + N(t.children) + v : t.type === "Dimension" ? e += t.value + C(t.unit) : t.type === "Value" ? e += D(t) : t.type === B ? e += z(t) : t.type === "Parentheses" ? e += I + N(t.children) + v : t.type === "Url" ? e += "url(" + g + t.value + g + v : e += P(t), t.type !== B && r.next !== null && r.next.data.type !== B && (e += a);
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);
159
154
  }), e;
160
155
  }
161
- function z(n) {
162
- let e = c, t = n.value.trim(), r = t.charCodeAt(0);
163
- return r === 43 || r === 45 ? e += a : r !== 44 && (e += p), e += t, r === 43 || r === 45 ? e += a : e += p, 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;
164
159
  }
165
- function D(n) {
166
- return n.type === "Raw" ? R(n, 0) : N(n.children);
160
+ function K(n) {
161
+ return n.type === "Raw" ? k(n, 0) : R(n.children);
167
162
  }
168
- function R(n, e) {
169
- return i(e) + P(n).trim();
163
+ function k(n, e) {
164
+ return a(e) + O(n).trim();
170
165
  }
171
- let L = k.children, _ = c;
172
- if (L.first !== null) {
173
- let n = m(0, b(L.first));
174
- n && (_ += n + u), L.forEach((t, r) => {
175
- if (t.type === F ? _ += M(t) : t.type === j ? _ += U(t) : _ += R(t, s), r.next !== null) {
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) {
176
171
  _ += u;
177
172
  let l = m(E(t), b(r.next.data));
178
- l && (_ += i(s) + l), _ += u;
173
+ l && (_ += a(s) + l), _ += u;
179
174
  }
180
175
  });
181
- let e = m(E(
182
- /** @type {import('css-tree').CssNode} */
183
- L.last
184
- ), E(k));
176
+ let e = m(E(A.last), E(N));
185
177
  e && (_ += u + e);
186
178
  } else
187
- _ += m(0, E(k));
179
+ _ += m(0, E(N));
188
180
  return _;
189
181
  }
190
- function fe(y) {
191
- return ne(y, { minify: !0 });
182
+ function se(y) {
183
+ return fe(y, { minify: !0 });
192
184
  }
193
185
  export {
194
- ne as format,
195
- fe as minify
186
+ fe as format,
187
+ se as minify
196
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, }?: Options): string;
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.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": "c8 --reporter=lcov uvu",
25
+ "test": "vitest --coverage",
25
26
  "check": "tsc",
26
- "prettier": "prettier --check index.js test/**/*.js"
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.0",
30
- "@types/css-tree": "^2.3.10",
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
- "prettier": "^3.5.3",
33
- "typescript": "^5.8.3",
34
- "uvu": "^0.5.6",
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.3"
41
+ "vite-plugin-dts": "^4.5.4",
42
+ "vitest": "^4.0.3"
37
43
  },
38
44
  "dependencies": {
39
- "css-tree": "^3.0.1"
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
+ }