@unocss/transformer-directives 0.62.2 → 0.62.4

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/README.md CHANGED
@@ -11,9 +11,9 @@ npm i -D @unocss/transformer-directives
11
11
  ```
12
12
 
13
13
  ```ts
14
+ import transformerDirectives from '@unocss/transformer-directives'
14
15
  // uno.config.ts
15
16
  import { defineConfig } from 'unocss'
16
- import transformerDirectives from '@unocss/transformer-directives'
17
17
 
18
18
  export default defineConfig({
19
19
  // ...
package/dist/index.mjs CHANGED
@@ -1,62 +1,6 @@
1
1
  import { expandVariantGroup, notNull, regexScopePlaceholder, toArray, cssIdRE } from '@unocss/core';
2
- import { generate, parse, clone, List, walk } from 'css-tree';
3
2
  import { transformThemeString, hasThemeFn } from '@unocss/rule-utils';
4
-
5
- const screenRuleRE = /(@screen [^{]+)(.+)/g;
6
- function handleScreen({ code, uno }, node) {
7
- let breakpointName = "";
8
- let prefix = "";
9
- if (node.name === "screen" && node.prelude?.type === "Raw")
10
- breakpointName = node.prelude.value.trim();
11
- if (!breakpointName)
12
- return;
13
- const match = breakpointName.match(/^(?:(lt|at)-)?(\w+)$/);
14
- if (match) {
15
- prefix = match[1];
16
- breakpointName = match[2];
17
- }
18
- const resolveBreakpoints = () => {
19
- let breakpoints;
20
- if (uno.userConfig && uno.userConfig.theme)
21
- breakpoints = uno.userConfig.theme.breakpoints;
22
- if (!breakpoints)
23
- breakpoints = uno.config.theme.breakpoints;
24
- return breakpoints ? Object.entries(breakpoints).sort((a, b) => Number.parseInt(a[1].replace(/[a-z]+/gi, "")) - Number.parseInt(b[1].replace(/[a-z]+/gi, ""))).map(([point, size]) => ({ point, size })) : void 0;
25
- };
26
- const variantEntries = (resolveBreakpoints() ?? []).map(({ point, size }, idx) => [point, size, idx]);
27
- const generateMediaQuery = (breakpointName2, prefix2) => {
28
- const [, size, idx] = variantEntries.find((i) => i[0] === breakpointName2);
29
- if (prefix2) {
30
- if (prefix2 === "lt")
31
- return `@media (max-width: ${calcMaxWidthBySize(size)})`;
32
- else if (prefix2 === "at")
33
- return `@media (min-width: ${size})${variantEntries[idx + 1] ? ` and (max-width: ${calcMaxWidthBySize(variantEntries[idx + 1][1])})` : ""}`;
34
- else
35
- throw new Error(`breakpoint variant not supported: ${prefix2}`);
36
- }
37
- return `@media (min-width: ${size})`;
38
- };
39
- if (!variantEntries.find((i) => i[0] === breakpointName))
40
- throw new Error(`breakpoint ${breakpointName} not found`);
41
- const offset = node.loc.start.offset;
42
- const str = code.original.slice(offset, node.loc.end.offset);
43
- const matches = Array.from(str.matchAll(screenRuleRE));
44
- if (!matches.length)
45
- return;
46
- for (const match2 of matches) {
47
- code.overwrite(
48
- offset + match2.index,
49
- offset + match2.index + match2[1].length,
50
- `${generateMediaQuery(breakpointName, prefix)}`
51
- );
52
- }
53
- }
54
- function calcMaxWidthBySize(size) {
55
- const value = size.match(/^-?\d+\.?\d*/)?.[0] || "";
56
- const unit = size.slice(value.length);
57
- const maxWidth = Number.parseFloat(value) - 0.1;
58
- return Number.isNaN(maxWidth) ? size : `${maxWidth}${unit}`;
59
- }
3
+ import { generate, parse, clone, List, walk } from 'css-tree';
60
4
 
61
5
  async function handleApply(ctx, node) {
62
6
  const { code, uno, options, filename } = ctx;
@@ -98,7 +42,7 @@ async function parseApply({ code, uno, applyVariable }, node, childNode) {
98
42
  }, []);
99
43
  if (!utils.length)
100
44
  return;
101
- const simicolonOffset = original[childNode.loc.end.offset] === ";" ? 1 : 0;
45
+ let simicolonOffset = original[childNode.loc.end.offset] === ";" ? 1 : original[childNode.loc.end.offset] === "@" ? -1 : 0;
102
46
  for (const i of utils) {
103
47
  const [, _selector, body2, parent] = i;
104
48
  const selectorOrGroup = _selector?.replace(regexScopePlaceholder, " ") || _selector;
@@ -110,7 +54,7 @@ async function parseApply({ code, uno, applyVariable }, node, childNode) {
110
54
  context: "rule"
111
55
  });
112
56
  const prelude = clone(node.prelude);
113
- prelude.children.forEach((child) => {
57
+ prelude.children?.forEach((child) => {
114
58
  const selectorListAst = clone(ruleAST.prelude);
115
59
  const classSelectors = new List();
116
60
  selectorListAst.children.forEach((selectorAst) => {
@@ -124,10 +68,11 @@ async function parseApply({ code, uno, applyVariable }, node, childNode) {
124
68
  let css = `${newSelector.replace(/.\\-/g, className)}{${body2}}`;
125
69
  if (parent)
126
70
  css = `${parent}{${css}}`;
71
+ simicolonOffset = 0;
127
72
  code.appendLeft(node.loc.end.offset, css);
128
73
  } else {
129
74
  if (body2.includes("@"))
130
- code.appendRight(original.length + simicolonOffset, body2);
75
+ code.appendRight(original.length, body2);
131
76
  else
132
77
  code.appendRight(childNode.loc.end.offset + simicolonOffset, body2);
133
78
  }
@@ -158,6 +103,62 @@ function handleFunction({ code, uno, options }, node) {
158
103
  }
159
104
  }
160
105
 
106
+ const screenRuleRE = /(@screen [^{]+)(.+)/g;
107
+ function handleScreen({ code, uno }, node) {
108
+ let breakpointName = "";
109
+ let prefix = "";
110
+ if (node.name === "screen" && node.prelude?.type === "Raw")
111
+ breakpointName = node.prelude.value.trim();
112
+ if (!breakpointName)
113
+ return;
114
+ const match = breakpointName.match(/^(?:(lt|at)-)?(\w+)$/);
115
+ if (match) {
116
+ prefix = match[1];
117
+ breakpointName = match[2];
118
+ }
119
+ const resolveBreakpoints = () => {
120
+ let breakpoints;
121
+ if (uno.userConfig && uno.userConfig.theme)
122
+ breakpoints = uno.userConfig.theme.breakpoints;
123
+ if (!breakpoints)
124
+ breakpoints = uno.config.theme.breakpoints;
125
+ return breakpoints ? Object.entries(breakpoints).sort((a, b) => Number.parseInt(a[1].replace(/[a-z]+/gi, "")) - Number.parseInt(b[1].replace(/[a-z]+/gi, ""))).map(([point, size]) => ({ point, size })) : void 0;
126
+ };
127
+ const variantEntries = (resolveBreakpoints() ?? []).map(({ point, size }, idx) => [point, size, idx]);
128
+ const generateMediaQuery = (breakpointName2, prefix2) => {
129
+ const [, size, idx] = variantEntries.find((i) => i[0] === breakpointName2);
130
+ if (prefix2) {
131
+ if (prefix2 === "lt")
132
+ return `@media (max-width: ${calcMaxWidthBySize(size)})`;
133
+ else if (prefix2 === "at")
134
+ return `@media (min-width: ${size})${variantEntries[idx + 1] ? ` and (max-width: ${calcMaxWidthBySize(variantEntries[idx + 1][1])})` : ""}`;
135
+ else
136
+ throw new Error(`breakpoint variant not supported: ${prefix2}`);
137
+ }
138
+ return `@media (min-width: ${size})`;
139
+ };
140
+ if (!variantEntries.find((i) => i[0] === breakpointName))
141
+ throw new Error(`breakpoint ${breakpointName} not found`);
142
+ const offset = node.loc.start.offset;
143
+ const str = code.original.slice(offset, node.loc.end.offset);
144
+ const matches = Array.from(str.matchAll(screenRuleRE));
145
+ if (!matches.length)
146
+ return;
147
+ for (const match2 of matches) {
148
+ code.overwrite(
149
+ offset + match2.index,
150
+ offset + match2.index + match2[1].length,
151
+ `${generateMediaQuery(breakpointName, prefix)}`
152
+ );
153
+ }
154
+ }
155
+ function calcMaxWidthBySize(size) {
156
+ const value = size.match(/^-?\d+\.?\d*/)?.[0] || "";
157
+ const unit = size.slice(value.length);
158
+ const maxWidth = Number.parseFloat(value) - 0.1;
159
+ return Number.isNaN(maxWidth) ? size : `${maxWidth}${unit}`;
160
+ }
161
+
161
162
  async function transformDirectives(code, uno, options, filename, originalCode, offset) {
162
163
  let { applyVariable } = options;
163
164
  const varStyle = options.varStyle;
@@ -167,12 +168,13 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
167
168
  applyVariable = ["--at-apply", "--uno-apply", "--uno"];
168
169
  }
169
170
  applyVariable = toArray(applyVariable || []);
170
- const hasApply = code.original.includes("@apply") || applyVariable.some((s) => code.original.includes(s));
171
- const hasScreen = code.original.includes("@screen");
172
- const hasThemeFn$1 = hasThemeFn(code.original);
171
+ const parseCode = originalCode || code.original;
172
+ const hasApply = parseCode.includes("@apply") || applyVariable.some((s) => parseCode.includes(s));
173
+ const hasScreen = parseCode.includes("@screen");
174
+ const hasThemeFn$1 = hasThemeFn(parseCode);
173
175
  if (!hasApply && !hasThemeFn$1 && !hasScreen)
174
176
  return;
175
- const ast = parse(originalCode || code.original, {
177
+ const ast = parse(parseCode, {
176
178
  parseCustomProperty: true,
177
179
  parseAtrulePrelude: false,
178
180
  positions: true,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/transformer-directives",
3
3
  "type": "module",
4
- "version": "0.62.2",
4
+ "version": "0.62.4",
5
5
  "description": "UnoCSS transformer for `@apply` directive",
6
6
  "author": "hannoeru <me@hanlee.co>",
7
7
  "license": "MIT",
@@ -33,8 +33,8 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "css-tree": "^2.3.1",
36
- "@unocss/rule-utils": "0.62.2",
37
- "@unocss/core": "0.62.2"
36
+ "@unocss/rule-utils": "0.62.4",
37
+ "@unocss/core": "0.62.4"
38
38
  },
39
39
  "devDependencies": {
40
40
  "magic-string": "^0.30.11"