@unocss/svelte-scoped 0.52.6 → 0.52.7

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.
@@ -336,36 +336,20 @@ async function parseUtils(classNames, uno) {
336
336
  return foundUtils.flat();
337
337
  }
338
338
 
339
- const DEFAULT_APPLY_VARIABLES = ["--at-apply"];
340
- async function transformApply({ content, uno, prepend, applyVariables, filename }) {
341
- applyVariables = core.toArray(applyVariables || DEFAULT_APPLY_VARIABLES);
342
- const hasApply = content.includes("@apply") || applyVariables.some((v) => content.includes(v));
343
- if (!hasApply)
344
- return;
345
- const s = new MagicString__default(content);
346
- await walkCss({ s, uno, applyVariables });
347
- if (!s.hasChanged())
348
- return;
349
- if (prepend)
350
- s.prepend(prepend);
351
- return {
352
- code: s.toString(),
353
- map: s.generateMap({ hires: true, source: filename || "" })
354
- };
355
- }
356
- async function walkCss(ctx) {
339
+ async function transformApply(ctx) {
357
340
  const ast = cssTree.parse(ctx.s.original, {
358
341
  parseAtrulePrelude: false,
359
342
  positions: true
360
343
  });
361
344
  if (ast.type !== "StyleSheet")
362
- return;
345
+ return ctx.s;
363
346
  const stack = [];
364
347
  cssTree.walk(ast, (node) => {
365
348
  if (node.type === "Rule")
366
349
  stack.push(handleApply(ctx, node));
367
350
  });
368
351
  await Promise.all(stack);
352
+ return ctx.s;
369
353
  }
370
354
  async function handleApply(ctx, node) {
371
355
  const parsePromises = node.block.children.map(async (childNode) => {
@@ -391,6 +375,47 @@ function getChildNodeValue(childNode, applyVariables) {
391
375
  return removeOuterQuotes(childNode.value.value.trim());
392
376
  }
393
377
 
378
+ const themeRE = /theme\((.+?)\)/g;
379
+ function transformTheme(s, theme) {
380
+ return s.replace(themeRE, (_, match) => {
381
+ const argumentsWithoutQuotes = match.slice(1, -1);
382
+ return getThemeValue(argumentsWithoutQuotes, theme);
383
+ });
384
+ }
385
+ function getThemeValue(rawArguments, theme) {
386
+ const keys = rawArguments.split(".");
387
+ let current = theme;
388
+ for (const key of keys) {
389
+ if (current[key] === void 0)
390
+ throw new Error(`"${rawArguments}" is not found in your theme`);
391
+ else
392
+ current = current[key];
393
+ }
394
+ return current;
395
+ }
396
+
397
+ const DEFAULT_APPLY_VARIABLES = ["--at-apply"];
398
+ async function transformStyle({ content, uno, prepend, applyVariables, filename }) {
399
+ applyVariables = core.toArray(applyVariables || DEFAULT_APPLY_VARIABLES);
400
+ const hasApply = content.includes("@apply") || applyVariables.some((v) => content.includes(v));
401
+ const hasThemeFn = content.match(themeRE);
402
+ if (!hasApply && !hasThemeFn)
403
+ return;
404
+ const s = new MagicString__default(content);
405
+ if (hasApply)
406
+ await transformApply({ s, uno, applyVariables });
407
+ if (hasThemeFn)
408
+ transformTheme(s, uno.config.theme);
409
+ if (!s.hasChanged())
410
+ return;
411
+ if (prepend)
412
+ s.prepend(prepend);
413
+ return {
414
+ code: s.toString(),
415
+ map: s.generateMap({ hires: true, source: filename || "" })
416
+ };
417
+ }
418
+
394
419
  function UnocssSveltePreprocess(options = {}, unoContextFromVite, isViteBuild) {
395
420
  if (!options.classPrefix)
396
421
  options.classPrefix = "spu-";
@@ -407,7 +432,8 @@ function UnocssSveltePreprocess(options = {}, unoContextFromVite, isViteBuild) {
407
432
  const addPreflights = !!attributes["uno:preflights"];
408
433
  const addSafelist = !!attributes["uno:safelist"];
409
434
  const checkForApply = options.applyVariables !== false;
410
- const changeNeeded = addPreflights || addSafelist || checkForApply;
435
+ const hasThemeFn = content.match(themeRE);
436
+ const changeNeeded = addPreflights || addSafelist || checkForApply || hasThemeFn;
411
437
  if (!changeNeeded)
412
438
  return;
413
439
  if (!uno)
@@ -419,8 +445,8 @@ function UnocssSveltePreprocess(options = {}, unoContextFromVite, isViteBuild) {
419
445
  const { css } = await uno.generate([], { preflights: addPreflights, safelist: addSafelist, minify: true });
420
446
  preflightsSafelistCss = css;
421
447
  }
422
- if (checkForApply) {
423
- return await transformApply({
448
+ if (checkForApply || hasThemeFn) {
449
+ return await transformStyle({
424
450
  content,
425
451
  prepend: preflightsSafelistCss,
426
452
  uno,
@@ -329,36 +329,20 @@ async function parseUtils(classNames, uno) {
329
329
  return foundUtils.flat();
330
330
  }
331
331
 
332
- const DEFAULT_APPLY_VARIABLES = ["--at-apply"];
333
- async function transformApply({ content, uno, prepend, applyVariables, filename }) {
334
- applyVariables = toArray(applyVariables || DEFAULT_APPLY_VARIABLES);
335
- const hasApply = content.includes("@apply") || applyVariables.some((v) => content.includes(v));
336
- if (!hasApply)
337
- return;
338
- const s = new MagicString(content);
339
- await walkCss({ s, uno, applyVariables });
340
- if (!s.hasChanged())
341
- return;
342
- if (prepend)
343
- s.prepend(prepend);
344
- return {
345
- code: s.toString(),
346
- map: s.generateMap({ hires: true, source: filename || "" })
347
- };
348
- }
349
- async function walkCss(ctx) {
332
+ async function transformApply(ctx) {
350
333
  const ast = parse(ctx.s.original, {
351
334
  parseAtrulePrelude: false,
352
335
  positions: true
353
336
  });
354
337
  if (ast.type !== "StyleSheet")
355
- return;
338
+ return ctx.s;
356
339
  const stack = [];
357
340
  walk(ast, (node) => {
358
341
  if (node.type === "Rule")
359
342
  stack.push(handleApply(ctx, node));
360
343
  });
361
344
  await Promise.all(stack);
345
+ return ctx.s;
362
346
  }
363
347
  async function handleApply(ctx, node) {
364
348
  const parsePromises = node.block.children.map(async (childNode) => {
@@ -384,6 +368,47 @@ function getChildNodeValue(childNode, applyVariables) {
384
368
  return removeOuterQuotes(childNode.value.value.trim());
385
369
  }
386
370
 
371
+ const themeRE = /theme\((.+?)\)/g;
372
+ function transformTheme(s, theme) {
373
+ return s.replace(themeRE, (_, match) => {
374
+ const argumentsWithoutQuotes = match.slice(1, -1);
375
+ return getThemeValue(argumentsWithoutQuotes, theme);
376
+ });
377
+ }
378
+ function getThemeValue(rawArguments, theme) {
379
+ const keys = rawArguments.split(".");
380
+ let current = theme;
381
+ for (const key of keys) {
382
+ if (current[key] === void 0)
383
+ throw new Error(`"${rawArguments}" is not found in your theme`);
384
+ else
385
+ current = current[key];
386
+ }
387
+ return current;
388
+ }
389
+
390
+ const DEFAULT_APPLY_VARIABLES = ["--at-apply"];
391
+ async function transformStyle({ content, uno, prepend, applyVariables, filename }) {
392
+ applyVariables = toArray(applyVariables || DEFAULT_APPLY_VARIABLES);
393
+ const hasApply = content.includes("@apply") || applyVariables.some((v) => content.includes(v));
394
+ const hasThemeFn = content.match(themeRE);
395
+ if (!hasApply && !hasThemeFn)
396
+ return;
397
+ const s = new MagicString(content);
398
+ if (hasApply)
399
+ await transformApply({ s, uno, applyVariables });
400
+ if (hasThemeFn)
401
+ transformTheme(s, uno.config.theme);
402
+ if (!s.hasChanged())
403
+ return;
404
+ if (prepend)
405
+ s.prepend(prepend);
406
+ return {
407
+ code: s.toString(),
408
+ map: s.generateMap({ hires: true, source: filename || "" })
409
+ };
410
+ }
411
+
387
412
  function UnocssSveltePreprocess(options = {}, unoContextFromVite, isViteBuild) {
388
413
  if (!options.classPrefix)
389
414
  options.classPrefix = "spu-";
@@ -400,7 +425,8 @@ function UnocssSveltePreprocess(options = {}, unoContextFromVite, isViteBuild) {
400
425
  const addPreflights = !!attributes["uno:preflights"];
401
426
  const addSafelist = !!attributes["uno:safelist"];
402
427
  const checkForApply = options.applyVariables !== false;
403
- const changeNeeded = addPreflights || addSafelist || checkForApply;
428
+ const hasThemeFn = content.match(themeRE);
429
+ const changeNeeded = addPreflights || addSafelist || checkForApply || hasThemeFn;
404
430
  if (!changeNeeded)
405
431
  return;
406
432
  if (!uno)
@@ -412,8 +438,8 @@ function UnocssSveltePreprocess(options = {}, unoContextFromVite, isViteBuild) {
412
438
  const { css } = await uno.generate([], { preflights: addPreflights, safelist: addSafelist, minify: true });
413
439
  preflightsSafelistCss = css;
414
440
  }
415
- if (checkForApply) {
416
- return await transformApply({
441
+ if (checkForApply || hasThemeFn) {
442
+ return await transformStyle({
417
443
  content,
418
444
  prepend: preflightsSafelistCss,
419
445
  uno,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/svelte-scoped",
3
3
  "type": "module",
4
- "version": "0.52.6",
4
+ "version": "0.52.7",
5
5
  "description": "Use UnoCSS in a modular fashion with styles being stored only in the Svelte component they are used in: Vite plugin for apps, preprocessor for component libraries",
6
6
  "author": "Jacob Bowdoin",
7
7
  "license": "MIT",
@@ -54,8 +54,8 @@
54
54
  "dependencies": {
55
55
  "css-tree": "^2.3.1",
56
56
  "magic-string": "^0.30.0",
57
- "@unocss/config": "0.52.6",
58
- "@unocss/reset": "0.52.6"
57
+ "@unocss/config": "0.52.7",
58
+ "@unocss/reset": "0.52.7"
59
59
  },
60
60
  "devDependencies": {
61
61
  "prettier-plugin-svelte": "^2.10.1",