@unocss/transformer-directives 0.62.3 → 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;
@@ -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) => {
@@ -159,6 +103,62 @@ function handleFunction({ code, uno, options }, node) {
159
103
  }
160
104
  }
161
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
+
162
162
  async function transformDirectives(code, uno, options, filename, originalCode, offset) {
163
163
  let { applyVariable } = options;
164
164
  const varStyle = options.varStyle;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/transformer-directives",
3
3
  "type": "module",
4
- "version": "0.62.3",
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/core": "0.62.3",
37
- "@unocss/rule-utils": "0.62.3"
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"