@unocss/transformer-directives 0.47.4 → 0.47.5

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
@@ -62,13 +62,14 @@ To use rules with `:`, you will need to quote the value
62
62
  }
63
63
  ```
64
64
 
65
- This feature is enabled by default (with prefix `--at-`), you can configure it or disable it via:
65
+ This feature is enabled by default with a few alias, you can configure or disable it via:
66
66
 
67
67
  ```js
68
68
  transformerDirectives({
69
- varStyle: '--my-at-',
69
+ // the defaults
70
+ applyVariable: ['--at-apply', '--uno-apply', '--uno'],
70
71
  // or disable with:
71
- // varStyle: false
72
+ // applyVariable: false
72
73
  })
73
74
  ```
74
75
 
@@ -78,16 +79,16 @@ The `@screen` directive allows you to create media queries that reference your b
78
79
 
79
80
  ```css
80
81
  .grid {
81
- @apply grid grid-cols-2;
82
+ --uno: grid grid-cols-2;
82
83
  }
83
84
  @screen xs {
84
85
  .grid {
85
- @apply grid-cols-1;
86
+ --uno: grid-cols-1;
86
87
  }
87
88
  }
88
89
  @screen sm {
89
90
  .grid {
90
- @apply grid-cols-3;
91
+ --uno: grid-cols-3;
91
92
  }
92
93
  }
93
94
  /* ... */
@@ -121,16 +122,16 @@ Will be transformed to:
121
122
 
122
123
  ```css
123
124
  .grid {
124
- @apply grid grid-cols-2;
125
+ --uno: grid grid-cols-2;
125
126
  }
126
127
  @screen lt-xs {
127
128
  .grid {
128
- @apply grid-cols-1;
129
+ --uno: grid-cols-1;
129
130
  }
130
131
  }
131
132
  @screen lt-sm {
132
133
  .grid {
133
- @apply grid-cols-3;
134
+ --uno: grid-cols-3;
134
135
  }
135
136
  }
136
137
  /* ... */
@@ -160,21 +161,21 @@ Will be transformed to:
160
161
 
161
162
  ```css
162
163
  .grid {
163
- @apply grid grid-cols-2;
164
+ --uno: grid grid-cols-2;
164
165
  }
165
166
  @screen at-xs {
166
167
  .grid {
167
- @apply grid-cols-1;
168
+ --uno: grid-cols-1;
168
169
  }
169
170
  }
170
171
  @screen at-xl {
171
172
  .grid {
172
- @apply grid-cols-3;
173
+ --uno: grid-cols-3;
173
174
  }
174
175
  }
175
176
  @screen at-xxl {
176
177
  .grid {
177
- @apply grid-cols-4;
178
+ --uno: grid-cols-4;
178
179
  }
179
180
  }
180
181
  /* ... */
package/dist/index.cjs CHANGED
@@ -107,13 +107,12 @@ async function handleApply(ctx, node) {
107
107
  }).toArray()
108
108
  );
109
109
  }
110
- async function parseApply({ code, uno, options, offset }, node, childNode) {
111
- const { varStyle = "--at-" } = options;
110
+ async function parseApply({ code, uno, offset, applyVariable }, node, childNode) {
112
111
  const calcOffset = (pos) => offset ? pos + offset : pos;
113
112
  let body;
114
113
  if (childNode.type === "Atrule" && childNode.name === "apply" && childNode.prelude && childNode.prelude.type === "Raw") {
115
114
  body = childNode.prelude.value.trim();
116
- } else if (varStyle !== false && childNode.type === "Declaration" && childNode.property === `${varStyle}apply` && childNode.value.type === "Raw") {
115
+ } else if (childNode.type === "Declaration" && applyVariable.includes(childNode.property) && childNode.value.type === "Raw") {
117
116
  body = childNode.value.value.trim();
118
117
  if (body.match(/^(['"]).*\1$/))
119
118
  body = body.slice(1, -1);
@@ -178,11 +177,18 @@ function transformerDirectives(options = {}) {
178
177
  };
179
178
  }
180
179
  async function transformDirectives(code, uno, options, filename, originalCode, offset) {
181
- const { varStyle = "--at-" } = options;
182
- const isApply = code.original.includes("@apply") || varStyle !== false && code.original.includes(varStyle);
183
- const isScreen = code.original.includes("@screen");
180
+ let { applyVariable } = options;
181
+ const varStyle = options.varStyle;
182
+ if (applyVariable === void 0) {
183
+ if (varStyle !== void 0)
184
+ applyVariable = varStyle ? [`${varStyle}apply`] : [];
185
+ applyVariable = ["--at-apply", "--uno-apply", "--uno"];
186
+ }
187
+ applyVariable = core.toArray(applyVariable || []);
188
+ const hasApply = code.original.includes("@apply") || applyVariable.some((s) => code.original.includes(s));
189
+ const hasScreen = code.original.includes("@screen");
184
190
  const hasThemeFn = code.original.match(themeFnRE);
185
- if (!isApply && !hasThemeFn && !isScreen)
191
+ if (!hasApply && !hasThemeFn && !hasScreen)
186
192
  return;
187
193
  const ast = cssTree.parse(originalCode || code.original, {
188
194
  parseAtrulePrelude: false,
@@ -192,13 +198,20 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
192
198
  if (ast.type !== "StyleSheet")
193
199
  return;
194
200
  const stack = [];
201
+ const ctx = {
202
+ options,
203
+ applyVariable,
204
+ uno,
205
+ code,
206
+ filename,
207
+ offset
208
+ };
195
209
  const processNode = async (node, _item, _list) => {
196
- const ctx = { options, uno, code, filename, offset };
197
- if (isScreen && node.type === "Atrule")
210
+ if (hasScreen && node.type === "Atrule")
198
211
  handleScreen(ctx, node);
199
212
  if (hasThemeFn && node.type === "Declaration")
200
213
  handleThemeFn(ctx, node);
201
- if (isApply && node.type === "Rule")
214
+ if (hasApply && node.type === "Rule")
202
215
  await handleApply(ctx, node);
203
216
  };
204
217
  cssTree.walk(ast, (...args) => stack.push(processNode(...args)));
package/dist/index.d.ts CHANGED
@@ -3,25 +3,35 @@ import MagicString from 'magic-string';
3
3
 
4
4
  interface TransformerDirectivesOptions {
5
5
  enforce?: SourceCodeTransformer['enforce'];
6
+ /**
7
+ * Throw an error if utils or themes are not found.
8
+ *
9
+ * @default true
10
+ */
11
+ throwOnMissing?: boolean;
12
+ /**
13
+ * Treat CSS variables as @apply directives for CSS syntax compatible.
14
+ *
15
+ * Pass `false` to disable.
16
+ *
17
+ * @default ['--at-apply', '--uno-apply', '--uno']
18
+ */
19
+ applyVariable?: false | string | string[];
6
20
  /**
7
21
  * Treat CSS variables as directives for CSS syntax compatible.
8
22
  *
9
23
  * Pass `false` to disable, or a string to use as a prefix.
10
24
  *
25
+ * @deprecated use `applyVariable` to specify the full var name instead.
11
26
  * @default '--at-'
12
27
  */
13
28
  varStyle?: false | string;
14
- /**
15
- * Throw an error if utils or themes are not found.
16
- *
17
- * @default true
18
- */
19
- throwOnMissing?: boolean;
20
29
  }
21
30
  interface TransformerDirectivesContext {
22
31
  code: MagicString;
23
32
  uno: UnoGenerator;
24
33
  options: TransformerDirectivesOptions;
34
+ applyVariable: string[];
25
35
  offset?: number;
26
36
  filename?: string;
27
37
  }
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { expandVariantGroup, notNull, regexScopePlaceholder, cssIdRE } from '@unocss/core';
1
+ import { expandVariantGroup, notNull, regexScopePlaceholder, cssIdRE, toArray } from '@unocss/core';
2
2
  import { generate, parse, clone, walk } from 'css-tree';
3
3
 
4
4
  const themeFnRE = /theme\((.*?)\)/g;
@@ -103,13 +103,12 @@ async function handleApply(ctx, node) {
103
103
  }).toArray()
104
104
  );
105
105
  }
106
- async function parseApply({ code, uno, options, offset }, node, childNode) {
107
- const { varStyle = "--at-" } = options;
106
+ async function parseApply({ code, uno, offset, applyVariable }, node, childNode) {
108
107
  const calcOffset = (pos) => offset ? pos + offset : pos;
109
108
  let body;
110
109
  if (childNode.type === "Atrule" && childNode.name === "apply" && childNode.prelude && childNode.prelude.type === "Raw") {
111
110
  body = childNode.prelude.value.trim();
112
- } else if (varStyle !== false && childNode.type === "Declaration" && childNode.property === `${varStyle}apply` && childNode.value.type === "Raw") {
111
+ } else if (childNode.type === "Declaration" && applyVariable.includes(childNode.property) && childNode.value.type === "Raw") {
113
112
  body = childNode.value.value.trim();
114
113
  if (body.match(/^(['"]).*\1$/))
115
114
  body = body.slice(1, -1);
@@ -174,11 +173,18 @@ function transformerDirectives(options = {}) {
174
173
  };
175
174
  }
176
175
  async function transformDirectives(code, uno, options, filename, originalCode, offset) {
177
- const { varStyle = "--at-" } = options;
178
- const isApply = code.original.includes("@apply") || varStyle !== false && code.original.includes(varStyle);
179
- const isScreen = code.original.includes("@screen");
176
+ let { applyVariable } = options;
177
+ const varStyle = options.varStyle;
178
+ if (applyVariable === void 0) {
179
+ if (varStyle !== void 0)
180
+ applyVariable = varStyle ? [`${varStyle}apply`] : [];
181
+ applyVariable = ["--at-apply", "--uno-apply", "--uno"];
182
+ }
183
+ applyVariable = toArray(applyVariable || []);
184
+ const hasApply = code.original.includes("@apply") || applyVariable.some((s) => code.original.includes(s));
185
+ const hasScreen = code.original.includes("@screen");
180
186
  const hasThemeFn = code.original.match(themeFnRE);
181
- if (!isApply && !hasThemeFn && !isScreen)
187
+ if (!hasApply && !hasThemeFn && !hasScreen)
182
188
  return;
183
189
  const ast = parse(originalCode || code.original, {
184
190
  parseAtrulePrelude: false,
@@ -188,13 +194,20 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
188
194
  if (ast.type !== "StyleSheet")
189
195
  return;
190
196
  const stack = [];
197
+ const ctx = {
198
+ options,
199
+ applyVariable,
200
+ uno,
201
+ code,
202
+ filename,
203
+ offset
204
+ };
191
205
  const processNode = async (node, _item, _list) => {
192
- const ctx = { options, uno, code, filename, offset };
193
- if (isScreen && node.type === "Atrule")
206
+ if (hasScreen && node.type === "Atrule")
194
207
  handleScreen(ctx, node);
195
208
  if (hasThemeFn && node.type === "Declaration")
196
209
  handleThemeFn(ctx, node);
197
- if (isApply && node.type === "Rule")
210
+ if (hasApply && node.type === "Rule")
198
211
  await handleApply(ctx, node);
199
212
  };
200
213
  walk(ast, (...args) => stack.push(processNode(...args)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/transformer-directives",
3
- "version": "0.47.4",
3
+ "version": "0.47.5",
4
4
  "description": "UnoCSS transformer for `@apply` directive",
5
5
  "author": "hannoeru <me@hanlee.co>",
6
6
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  "dist"
33
33
  ],
34
34
  "dependencies": {
35
- "@unocss/core": "0.47.4",
35
+ "@unocss/core": "0.47.5",
36
36
  "css-tree": "^2.2.1"
37
37
  },
38
38
  "devDependencies": {