@projectwallace/format-css 2.0.0-alpha.1 → 2.0.1

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.
Files changed (2) hide show
  1. package/dist/format-css.js +69 -66
  2. package/package.json +9 -11
@@ -1,10 +1,10 @@
1
1
  import h from "css-tree/parser";
2
- const o = " ", i = "", N = ":", x = ";", L = '"', I = "(", g = ")", d = "[", ee = "]", H = "{", V = "}", te = "{}", W = ",", Z = "Atrule", z = "Rule", F = "Block", G = "SelectorList", Y = "Selector", B = "Declaration", $ = "Operator";
3
- function b(_) {
4
- return /[A-Z]/.test(_) ? _.toLowerCase() : _;
2
+ const o = " ", c = "", N = ":", $ = ";", L = '"', x = "(", g = ")", d = "[", ee = "]", H = "{", V = "}", te = "{}", W = ",", Z = "Atrule", z = "Rule", F = "Block", G = "SelectorList", I = "Selector", Y = "Declaration", B = "Operator";
3
+ function C(y) {
4
+ return /[A-Z]/.test(y) ? y.toLowerCase() : y;
5
5
  }
6
- function re(_, { minify: P = !1 } = {}) {
7
- let O = [], v = h(_, {
6
+ function re(y, { minify: P = !1 } = {}) {
7
+ let O = [], v = h(y, {
8
8
  positions: !0,
9
9
  parseAtrulePrelude: !1,
10
10
  parseCustomProperty: !0,
@@ -13,55 +13,55 @@ function re(_, { minify: P = !1 } = {}) {
13
13
  O.push(e.start.offset, e.end.offset);
14
14
  }
15
15
  });
16
- const u = P ? i : `
17
- `, C = P ? i : o, Q = P ? i : x;
18
- let a = 0;
19
- function s(n) {
20
- return P ? i : " ".repeat(n);
16
+ const u = P ? c : `
17
+ `, p = P ? c : o, Q = P ? c : $;
18
+ let s = 0;
19
+ function a(n) {
20
+ return P ? c : " ".repeat(n);
21
21
  }
22
22
  function A(n) {
23
23
  let e = n.loc;
24
- return e ? _.slice(e.start.offset, e.end.offset) : i;
24
+ return e ? y.slice(e.start.offset, e.end.offset) : c;
25
25
  }
26
- function y(n) {
26
+ function b(n) {
27
27
  return /** @type {import('css-tree').CssLocation} */ n.loc.start.offset;
28
28
  }
29
- function c(n) {
29
+ function E(n) {
30
30
  return /** @type {import('css-tree').CssLocation} */ n.loc.end.offset;
31
31
  }
32
- function E(n, e) {
32
+ function m(n, e) {
33
33
  if (P || n === void 0 || e === void 0)
34
- return i;
34
+ return c;
35
35
  let t = "";
36
36
  for (let r = 0; r < O.length; r += 2) {
37
37
  let l = O[r];
38
38
  if (l === void 0 || l < n) continue;
39
39
  let f = O[r + 1];
40
40
  if (f === void 0 || f > e) break;
41
- t.length > 0 && (t += u + s(a)), t += _.slice(l, f);
41
+ t.length > 0 && (t += u + a(s)), t += y.slice(l, f);
42
42
  }
43
43
  return t;
44
44
  }
45
45
  function w(n) {
46
46
  let e, t = n.prelude, r = n.block;
47
47
  t.type === G && (e = j(t));
48
- let l = E(c(t), y(r));
49
- return l && (e += u + s(a) + l), r.type === F && (e += M(r)), e;
48
+ let l = m(E(t), b(r));
49
+ return l && (e += u + a(s) + l), r.type === F && (e += M(r)), e;
50
50
  }
51
51
  function j(n) {
52
- let e = i;
52
+ let e = c;
53
53
  return n.children.forEach((t, r) => {
54
- t.type === Y && (e += s(a) + S(t)), r.next !== null && (e += W + u);
55
- let l = r.next !== null ? y(r.next.data) : c(n), f = E(c(t), l);
56
- f && (e += s(a) + f + u);
54
+ t.type === I && (e += a(s) + S(t)), r.next !== null && (e += W + u);
55
+ let l = r.next !== null ? b(r.next.data) : E(n), f = m(E(t), l);
56
+ f && (e += a(s) + f + u);
57
57
  }), e;
58
58
  }
59
59
  function S(n) {
60
- let e = i;
60
+ let e = c;
61
61
  return (n.children || []).forEach((r) => {
62
62
  switch (r.type) {
63
63
  case "TypeSelector": {
64
- e += b(r.name);
64
+ e += C(r.name);
65
65
  break;
66
66
  }
67
67
  case "Combinator": {
@@ -71,13 +71,13 @@ function re(_, { minify: P = !1 } = {}) {
71
71
  case "PseudoClassSelector":
72
72
  case "PseudoElementSelector": {
73
73
  e += N;
74
- let l = b(r.name);
75
- (l === "before" || l === "after" || r.type === "PseudoElementSelector") && (e += N), e += l, r.children && (e += I + S(r) + g);
74
+ let l = C(r.name);
75
+ (l === "before" || l === "after" || r.type === "PseudoElementSelector") && (e += N), e += l, r.children && (e += x + S(r) + g);
76
76
  break;
77
77
  }
78
78
  case G: {
79
79
  r.children.forEach((l, f) => {
80
- l.type === Y && (e += S(l)), f.next && f.next.data.type === Y && (e += W + C);
80
+ l.type === I && (e += S(l)), f.next && f.next.data.type === I && (e += W + p);
81
81
  });
82
82
  break;
83
83
  }
@@ -85,8 +85,8 @@ function re(_, { minify: P = !1 } = {}) {
85
85
  let l = r.nth;
86
86
  if (l)
87
87
  if (l.type === "AnPlusB") {
88
- let f = l.a, p = l.b;
89
- f !== null && (e += f + "n"), f !== null && p !== null && (e += o), p !== null && (f !== null && !p.startsWith("-") && (e += "+" + o), e += p);
88
+ let f = l.a, i = l.b;
89
+ f !== null && (e += f + "n"), f !== null && i !== null && (e += o), i !== null && (f !== null && !i.startsWith("-") && (e += "+" + o), e += i);
90
90
  } else
91
91
  e += A(l);
92
92
  r.selector !== null && (e += o + "of" + o + S(r.selector));
@@ -104,80 +104,83 @@ function re(_, { minify: P = !1 } = {}) {
104
104
  }), e;
105
105
  }
106
106
  function M(n) {
107
- let e = n.children, t = C;
107
+ let e = n.children, t = p;
108
108
  if (e.isEmpty) {
109
- let f = E(y(n), c(n));
110
- return f ? (t += H + u, t += s(a + 1) + f, t += u + s(a) + V, t) : t + te;
109
+ let f = m(b(n), E(n));
110
+ return f ? (t += H + u, t += a(s + 1) + f, t += u + a(s) + V, t) : t + te;
111
111
  }
112
- t += H + u, a++;
113
- let r = E(y(n), y(
112
+ t += H + u, s++;
113
+ let r = m(b(n), b(
114
114
  /** @type {import('css-tree').CssNode} */
115
115
  e.first
116
116
  ));
117
- r && (t += s(a) + r + u), e.forEach((f, p) => {
118
- if (p.prev !== null) {
119
- let D = E(c(p.prev.data), y(f));
120
- D && (t += s(a) + D + u);
117
+ r && (t += a(s) + r + u), e.forEach((f, i) => {
118
+ if (i.prev !== null) {
119
+ let D = m(E(i.prev.data), b(f));
120
+ D && (t += a(s) + D + u);
121
121
  }
122
- f.type === B ? (t += J(f), p.next === null ? t += Q : t += x) : (p.prev !== null && p.prev.data.type === B && (t += u), f.type === z ? t += w(f) : f.type === Z ? t += K(f) : t += k(f, a)), p.next !== null && (t += u, f.type !== B && (t += u));
122
+ f.type === Y ? (t += J(f), i.next === null ? t += Q : t += $) : (i.prev !== null && i.prev.data.type === Y && (t += u), f.type === z ? t += w(f) : f.type === Z ? t += K(f) : t += k(f, s)), i.next !== null && (t += u, f.type !== Y && (t += u));
123
123
  });
124
- let l = E(c(
124
+ let l = m(E(
125
125
  /** @type {import('css-tree').CssNode} */
126
126
  e.last
127
- ), c(n));
128
- return l && (t += u + s(a) + l), a--, t += u + s(a) + V, t;
127
+ ), E(n));
128
+ return l && (t += u + a(s) + l), s--, t += u + a(s) + V, t;
129
129
  }
130
130
  function K(n) {
131
- let e = s(a) + "@", t = n.prelude, r = n.block;
132
- return e += b(n.name), t !== null && (e += o + q(t)), r === null ? e += x : r.type === F && (e += M(r)), e;
131
+ let e = a(s) + "@", t = n.prelude, r = n.block;
132
+ return e += C(n.name), t !== null && (e += o + q(t)), r === null ? e += $ : r.type === F && (e += M(r)), e;
133
133
  }
134
134
  function q(n) {
135
135
  let e = A(n);
136
- return e.replace(/\s*([:,])/g, e.toLowerCase().includes("selector(") ? "$1" : "$1 ").replace(/\s*(=>|<=)\s*/g, " $1 ").replace(/\)([a-zA-Z])/g, ") $1").replace(new RegExp("(?<!<=)(?<!=>)(?<!<= )([<>])(?![<= ])(?![=> ])(?![ =>])", "g"), " $1 ").replace(/calc\(([^*]*)\*([^*])/g, "calc($1 * $2").replace(/\s+/g, o).replace(/selector|url|supports|layer\(/ig, (t) => b(t));
136
+ 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) => {
137
+ let i = l === "+" || l === "-" ? o : p;
138
+ return `calc(${r.trim()}${i}${l}${i}${f.trim()})`;
139
+ }).replace(/selector|url|supports|layer\(/ig, (t) => C(t));
137
140
  }
138
141
  function J(n) {
139
142
  let e = n.property;
140
- e.charCodeAt(0) === 45 && e.charCodeAt(1) === 45 || (e = b(e));
143
+ e.charCodeAt(0) === 45 && e.charCodeAt(1) === 45 || (e = C(e));
141
144
  let t = U(n.value);
142
- return e === "font" && (t = t.replace(/\s*\/\s*/, "/")), t === i && P && (t += o), n.important === !0 ? t += C + "!important" : typeof n.important == "string" && (t += C + "!" + b(n.important)), s(a) + e + N + C + t;
145
+ return e === "font" && (t = t.replace(/\s*\/\s*/, "/")), t === c && P && (t += o), n.important === !0 ? t += p + "!important" : typeof n.important == "string" && (t += p + "!" + C(n.important)), a(s) + e + N + p + t;
143
146
  }
144
147
  function R(n) {
145
- let e = i;
148
+ let e = c;
146
149
  return n.forEach((t, r) => {
147
- t.type === "Identifier" ? e += t.name : t.type === "Function" ? e += b(t.name) + I + R(t.children) + g : t.type === "Dimension" ? e += t.value + b(t.unit) : t.type === "Value" ? e += U(t) : t.type === $ ? e += X(t) : t.type === "Parentheses" ? e += I + R(t.children) + g : t.type === "Url" ? e += "url(" + L + t.value + L + g : e += A(t), t.type !== $ && r.next !== null && r.next.data.type !== $ && (e += o);
150
+ t.type === "Identifier" ? e += t.name : t.type === "Function" ? e += C(t.name) + x + R(t.children) + g : t.type === "Dimension" ? e += t.value + C(t.unit) : t.type === "Value" ? e += U(t) : t.type === B ? e += X(t) : t.type === "Parentheses" ? e += x + R(t.children) + g : t.type === "Url" ? e += "url(" + L + t.value + L + g : e += A(t), t.type !== B && r.next !== null && r.next.data.type !== B && (e += o);
148
151
  }), e;
149
152
  }
150
153
  function X(n) {
151
- let e = i, t = n.value.trim(), r = t.charCodeAt(0);
152
- return r === 43 || r === 45 ? e += o : r !== 44 && (e += C), e += t, r === 43 || r === 45 ? e += o : e += C, e;
154
+ let e = c, t = n.value.trim(), r = t.charCodeAt(0);
155
+ return r === 43 || r === 45 ? e += o : r !== 44 && (e += p), e += t, r === 43 || r === 45 ? e += o : e += p, e;
153
156
  }
154
157
  function U(n) {
155
158
  return n.type === "Raw" ? k(n, 0) : R(n.children);
156
159
  }
157
160
  function k(n, e) {
158
- return s(e) + A(n).trim();
161
+ return a(e) + A(n).trim();
159
162
  }
160
- let T = v.children, m = i;
163
+ let T = v.children, _ = c;
161
164
  if (T.first) {
162
- let n = E(0, y(T.first));
163
- n && (m += n + u), T.forEach((t, r) => {
164
- if (t.type === z ? m += w(t) : t.type === Z ? m += K(t) : m += k(t, a), r.next !== null) {
165
- m += u;
166
- let l = E(c(t), y(r.next.data));
167
- l && (m += s(a) + l), m += u;
165
+ let n = m(0, b(T.first));
166
+ n && (_ += n + u), T.forEach((t, r) => {
167
+ if (t.type === z ? _ += w(t) : t.type === Z ? _ += K(t) : _ += k(t, s), r.next !== null) {
168
+ _ += u;
169
+ let l = m(E(t), b(r.next.data));
170
+ l && (_ += a(s) + l), _ += u;
168
171
  }
169
172
  });
170
- let e = E(c(
173
+ let e = m(E(
171
174
  /** @type {import('css-tree').CssNode} */
172
175
  T.last
173
- ), c(v));
174
- e && (m += u + e);
176
+ ), E(v));
177
+ e && (_ += u + e);
175
178
  } else
176
- m += E(0, c(v));
177
- return m;
179
+ _ += m(0, E(v));
180
+ return _;
178
181
  }
179
- function le(_) {
180
- return re(_, { minify: !0 });
182
+ function le(y) {
183
+ return re(y, { minify: !0 });
181
184
  }
182
185
  export {
183
186
  re as format,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@projectwallace/format-css",
3
- "version": "2.0.0-alpha.1",
4
- "description": "Fast, small, zero-config library to format CSS with basic rules.",
3
+ "version": "2.0.1",
4
+ "description": "Fast, small, zero-config library to format or minify CSS with basic rules.",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/projectwallace/format-css.git"
@@ -26,16 +26,14 @@
26
26
  "prettier": "prettier --check index.js test/**/*.js"
27
27
  },
28
28
  "devDependencies": {
29
- "@codecov/vite-plugin": "^1.4.0",
30
- "@codspeed/tinybench-plugin": "^3.1.1",
31
- "@types/css-tree": "^2.3.9",
32
- "c8": "^10.1.2",
33
- "prettier": "^3.3.3",
34
- "tinybench": "^2.8.0",
35
- "typescript": "^5.6.3",
29
+ "@codecov/vite-plugin": "^1.9.0",
30
+ "@types/css-tree": "^2.3.10",
31
+ "c8": "^10.1.3",
32
+ "prettier": "^3.5.3",
33
+ "typescript": "^5.8.3",
36
34
  "uvu": "^0.5.6",
37
- "vite": "^5.4.11",
38
- "vite-plugin-dts": "^4.3.0"
35
+ "vite": "^6.2.6",
36
+ "vite-plugin-dts": "^4.5.3"
39
37
  },
40
38
  "dependencies": {
41
39
  "css-tree": "^3.0.1"