@unocss/transformer-directives 0.59.4 → 0.60.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.
package/dist/index.d.mts CHANGED
@@ -35,7 +35,7 @@ interface TransformerDirectivesContext {
35
35
  offset?: number;
36
36
  filename?: string;
37
37
  }
38
+
38
39
  declare function transformerDirectives(options?: TransformerDirectivesOptions): SourceCodeTransformer;
39
- declare function transformDirectives(code: MagicString, uno: UnoGenerator, options: TransformerDirectivesOptions, filename?: string, originalCode?: string, offset?: number): Promise<void>;
40
40
 
41
- export { type TransformerDirectivesContext, type TransformerDirectivesOptions, transformerDirectives as default, transformDirectives };
41
+ export { type TransformerDirectivesContext, type TransformerDirectivesOptions, transformerDirectives as default };
package/dist/index.d.ts CHANGED
@@ -35,7 +35,7 @@ interface TransformerDirectivesContext {
35
35
  offset?: number;
36
36
  filename?: string;
37
37
  }
38
+
38
39
  declare function transformerDirectives(options?: TransformerDirectivesOptions): SourceCodeTransformer;
39
- declare function transformDirectives(code: MagicString, uno: UnoGenerator, options: TransformerDirectivesOptions, filename?: string, originalCode?: string, offset?: number): Promise<void>;
40
40
 
41
- export { type TransformerDirectivesContext, type TransformerDirectivesOptions, transformerDirectives as default, transformDirectives };
41
+ export { type TransformerDirectivesContext, type TransformerDirectivesOptions, transformerDirectives as default };
package/dist/index.mjs CHANGED
@@ -1,13 +1,6 @@
1
- import { expandVariantGroup, notNull, regexScopePlaceholder, cssIdRE, toArray } from '@unocss/core';
1
+ import { expandVariantGroup, notNull, regexScopePlaceholder, toArray, cssIdRE } from '@unocss/core';
2
2
  import { generate, parse, clone, List, walk } from 'css-tree';
3
- import { transformThemeFn, hasThemeFn } from '@unocss/rule-utils';
4
-
5
- function handleThemeFn({ code, uno, options }, node) {
6
- const { throwOnMissing = true } = options;
7
- const offset = node.value.loc.start.offset;
8
- const str = code.original.slice(offset, node.value.loc.end.offset);
9
- code.overwrite(offset, node.value.loc.end.offset, transformThemeFn(str, uno.config.theme, throwOnMissing));
10
- }
3
+ import { transformThemeString, hasThemeFn } from '@unocss/rule-utils';
11
4
 
12
5
  const screenRuleRE = /(@screen) (.+) /g;
13
6
  function handleScreen({ code, uno }, node) {
@@ -79,14 +72,19 @@ async function handleApply(ctx, node) {
79
72
  async function parseApply({ code, uno, offset, applyVariable }, node, childNode) {
80
73
  const calcOffset = (pos) => offset ? pos + offset : pos;
81
74
  let body;
82
- if (childNode.type === "Atrule" && childNode.name === "apply" && childNode.prelude && childNode.prelude.type === "Raw")
83
- body = childNode.prelude.value.trim();
84
- else if (childNode.type === "Declaration" && applyVariable.includes(childNode.property) && childNode.value.type === "Raw")
85
- body = childNode.value.value.trim();
75
+ if (childNode.type === "Atrule" && childNode.name === "apply" && childNode.prelude && childNode.prelude.type === "Raw") {
76
+ body = removeQuotes(childNode.prelude.value.trim());
77
+ } else if (childNode.type === "Declaration" && applyVariable.includes(childNode.property) && childNode.value.type === "Value") {
78
+ let rawValue = code.original.slice(
79
+ calcOffset(childNode.value.loc.start.offset),
80
+ calcOffset(childNode.value.loc.end.offset)
81
+ );
82
+ rawValue = removeQuotes(rawValue);
83
+ const items = rawValue.split(/\s+/g).filter(Boolean).map((i) => removeQuotes(i));
84
+ body = items.join(" ");
85
+ }
86
86
  if (!body)
87
87
  return;
88
- if (/^(['"]).*\1$/.test(body))
89
- body = body.slice(1, -1);
90
88
  const classNames = expandVariantGroup(body).split(/\s+/g).map((className) => className.trim().replace(/\\/, ""));
91
89
  const utils = (await Promise.all(
92
90
  classNames.map((i) => uno.parseToken(i, "-"))
@@ -138,17 +136,24 @@ async function parseApply({ code, uno, offset, applyVariable }, node, childNode)
138
136
  calcOffset(childNode.loc.end.offset + simicolonOffset)
139
137
  );
140
138
  }
139
+ function removeQuotes(value) {
140
+ return value.replace(/^(['"])(.*)\1$/, "$2");
141
+ }
141
142
 
142
- function transformerDirectives(options = {}) {
143
- return {
144
- name: "@unocss/transformer-directives",
145
- enforce: options?.enforce,
146
- idFilter: (id) => cssIdRE.test(id),
147
- transform: (code, id, ctx) => {
148
- return transformDirectives(code, ctx.uno, options, id);
143
+ function handleFunction({ code, uno, options }, node) {
144
+ const { throwOnMissing = true } = options;
145
+ switch (node.name) {
146
+ case "theme": {
147
+ if (node.children.size !== 1)
148
+ throw new Error("theme() expect exact one argument");
149
+ const themeStr = node.children.first.value;
150
+ const value = transformThemeString(themeStr, uno.config.theme, throwOnMissing);
151
+ if (value)
152
+ code.overwrite(node.loc.start.offset, node.loc.end.offset, value);
149
153
  }
150
- };
154
+ }
151
155
  }
156
+
152
157
  async function transformDirectives(code, uno, options, filename, originalCode, offset) {
153
158
  let { applyVariable } = options;
154
159
  const varStyle = options.varStyle;
@@ -164,6 +169,7 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
164
169
  if (!hasApply && !hasThemeFn$1 && !hasScreen)
165
170
  return;
166
171
  const ast = parse(originalCode || code.original, {
172
+ parseCustomProperty: true,
167
173
  parseAtrulePrelude: false,
168
174
  positions: true,
169
175
  filename
@@ -182,8 +188,8 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
182
188
  const processNode = async (node, _item, _list) => {
183
189
  if (hasScreen && node.type === "Atrule")
184
190
  handleScreen(ctx, node);
185
- if (hasThemeFn$1 && node.type === "Declaration")
186
- handleThemeFn(ctx, node);
191
+ if (node.type === "Function")
192
+ handleFunction(ctx, node);
187
193
  if (hasApply && node.type === "Rule")
188
194
  await handleApply(ctx, node);
189
195
  };
@@ -191,4 +197,15 @@ async function transformDirectives(code, uno, options, filename, originalCode, o
191
197
  await Promise.all(stack);
192
198
  }
193
199
 
194
- export { transformerDirectives as default, transformDirectives };
200
+ function transformerDirectives(options = {}) {
201
+ return {
202
+ name: "@unocss/transformer-directives",
203
+ enforce: options?.enforce,
204
+ idFilter: (id) => cssIdRE.test(id),
205
+ transform: (code, id, ctx) => {
206
+ return transformDirectives(code, ctx.uno, options, id);
207
+ }
208
+ };
209
+ }
210
+
211
+ export { transformerDirectives as default };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/transformer-directives",
3
3
  "type": "module",
4
- "version": "0.59.4",
4
+ "version": "0.60.1",
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.59.4",
37
- "@unocss/rule-utils": "0.59.4"
36
+ "@unocss/core": "0.60.1",
37
+ "@unocss/rule-utils": "0.60.1"
38
38
  },
39
39
  "devDependencies": {
40
40
  "magic-string": "^0.30.10"