@unocss/vite 0.50.0 → 0.50.2

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.cjs CHANGED
@@ -399,14 +399,12 @@ function GlobalModeBuildPlugin(ctx) {
399
399
  ];
400
400
  if (config.build.rollupOptions.output) {
401
401
  const outputOptions = config.build.rollupOptions.output;
402
- if (Array.isArray(outputOptions)) {
403
- distDirs.push(
404
- ...outputOptions.map((option) => option.dir).filter(Boolean)
405
- );
406
- } else {
407
- if (outputOptions.dir)
408
- distDirs.push(outputOptions.dir);
409
- }
402
+ const outputDirs = Array.isArray(outputOptions) ? outputOptions.map((option) => option.dir).filter(Boolean) : outputOptions.dir ? [outputOptions.dir] : [];
403
+ outputDirs.forEach((dir) => {
404
+ distDirs.push(dir);
405
+ if (!path.isAbsolute(dir))
406
+ distDirs.push(path.resolve(config.root, dir));
407
+ });
410
408
  }
411
409
  const cssPostPlugin = config.plugins.find((i) => i.name === "vite:css-post");
412
410
  const cssPlugin = config.plugins.find((i) => i.name === "vite:css");
@@ -867,19 +865,6 @@ async function transformSvelteSFC(code, id, uno, options = {}) {
867
865
  const classes = [...code.matchAll(classesRE)];
868
866
  const classDirectives = [...code.matchAll(classesDirectivesRE)];
869
867
  const classDirectivesShorthand = [...code.matchAll(classDirectivesShorthandRE)];
870
- if (!classes.length && !classDirectives.length && !classDirectivesShorthand.length) {
871
- if (preflights || safelist) {
872
- if (alreadyHasStyles) {
873
- return {
874
- code: code.replace(/(<style[^>]*>)/, `$1${styles}`)
875
- };
876
- }
877
- return { code: `${code}
878
- <style>${styles}</style>` };
879
- } else {
880
- return;
881
- }
882
- }
883
868
  const originalShortcuts = uno.config.shortcuts;
884
869
  const shortcuts = {};
885
870
  const toGenerate = /* @__PURE__ */ new Set();
@@ -919,13 +904,14 @@ async function transformSvelteSFC(code, id, uno, options = {}) {
919
904
  async function sortKnownAndUnknownClasses(str) {
920
905
  const classArr = str.split(/\s+/);
921
906
  const result = await Promise.all(classArr.filter(Boolean).map(async (t) => [t, !!await uno.parseToken(t)]));
922
- const known = result.filter(([, matched]) => matched).map(([t]) => t).sort();
907
+ const known = result.filter(([, matched2]) => matched2).map(([t]) => t).sort();
923
908
  if (!known.length)
924
909
  return null;
925
- const replacements = result.filter(([, matched]) => !matched).map(([i]) => i);
910
+ const replacements = result.filter(([, matched2]) => !matched2).map(([i]) => i);
926
911
  const className = queueCompiledClass(known);
927
912
  return [className, ...replacements].join(" ");
928
913
  }
914
+ const processedMap = /* @__PURE__ */ new Set();
929
915
  for (const match of classes) {
930
916
  let body = core.expandVariantGroup(match[2].trim());
931
917
  const inlineConditionals = [...body.matchAll(classesFromInlineConditionalsRE)];
@@ -936,8 +922,10 @@ async function transformSvelteSFC(code, id, uno, options = {}) {
936
922
  }
937
923
  const replacement = await sortKnownAndUnknownClasses(body);
938
924
  if (replacement) {
939
- const start = match.index;
940
- s.overwrite(start + 7, start + match[0].length - 1, replacement);
925
+ const start = match.index + 7;
926
+ const end = match.index + match[0].length - 1;
927
+ processedMap.add(start);
928
+ s.overwrite(start, end, replacement);
941
929
  }
942
930
  }
943
931
  for (const match of classDirectives) {
@@ -947,6 +935,7 @@ async function transformSvelteSFC(code, id, uno, options = {}) {
947
935
  continue;
948
936
  const className = queueCompiledClass([token]);
949
937
  const start = match.index + "class:".length;
938
+ processedMap.add(start);
950
939
  s.overwrite(start, start + match[1].length, className);
951
940
  }
952
941
  for (const match of classDirectivesShorthand) {
@@ -956,8 +945,44 @@ async function transformSvelteSFC(code, id, uno, options = {}) {
956
945
  continue;
957
946
  const className = queueCompiledClass([token]);
958
947
  const start = match.index + "class:".length;
948
+ processedMap.add(start);
959
949
  s.overwrite(start, start + match[1].length, `${className}={${token}}`);
960
950
  }
951
+ const { matched } = await uno.generate(code, { preflights: false, safelist: false, minify: true });
952
+ for (const token of matched) {
953
+ const match = token.match(core.attributifyRE);
954
+ if (match) {
955
+ const [, name, value] = match;
956
+ if (!value) {
957
+ let start = 0;
958
+ code.split(/([\s"'`;*]|:\(|\)"|\)\s)/g).forEach((i) => {
959
+ const end = start + i.length;
960
+ if (i === name && !processedMap.has(start)) {
961
+ const className = queueCompiledClass([name]);
962
+ s.appendLeft(start, `class:${className}={true} `);
963
+ s.overwrite(start, end, "");
964
+ }
965
+ start = end;
966
+ });
967
+ } else {
968
+ const regex = new RegExp(`(${core.escapeRegExp(name)}=)(['"])[^\\2]*?${core.escapeRegExp(value)}[^\\2]*?\\2`, "g");
969
+ for (const match2 of code.matchAll(regex)) {
970
+ const escaped = match2[1];
971
+ const body = match2[0].slice(escaped.length);
972
+ let bodyIndex = body.match(`[\\b\\s'"]${core.escapeRegExp(value)}[\\b\\s'"]`)?.index ?? -1;
973
+ if (body[bodyIndex]?.match(/[\s'"]/))
974
+ bodyIndex++;
975
+ if (bodyIndex < 0)
976
+ return;
977
+ const [, base] = await uno.matchVariants(value);
978
+ const variants = value.replace(base, "");
979
+ const className = queueCompiledClass([`${variants + name}-${base}`]);
980
+ s.appendLeft(match2.index, `class:${className}={true} `);
981
+ s.overwrite(match2.index, match2.index + match2[0].length, "");
982
+ }
983
+ }
984
+ }
985
+ }
961
986
  uno.config.shortcuts = [...originalShortcuts, ...Object.entries(shortcuts)];
962
987
  const { css } = await uno.generate(toGenerate, { preflights: false, safelist: false, minify: true });
963
988
  styles += wrapSelectorsWithGlobal(css);
package/dist/index.mjs CHANGED
@@ -1,10 +1,10 @@
1
1
  import UnocssInspector from '@unocss/inspector';
2
- import { cssIdRE, createGenerator, BetterMap, notNull, expandVariantGroup, toEscapedSelector } from '@unocss/core';
2
+ import { cssIdRE, createGenerator, BetterMap, notNull, expandVariantGroup, attributifyRE, escapeRegExp, toEscapedSelector } from '@unocss/core';
3
3
  import { createFilter } from '@rollup/pluginutils';
4
4
  import { loadConfig } from '@unocss/config';
5
5
  import { createHash } from 'crypto';
6
6
  import MagicString from 'magic-string';
7
- import { resolve, dirname } from 'path';
7
+ import { resolve, isAbsolute, dirname } from 'path';
8
8
  import fs from 'fs/promises';
9
9
  import fg from 'fast-glob';
10
10
  import remapping from '@ampproject/remapping';
@@ -386,14 +386,12 @@ function GlobalModeBuildPlugin(ctx) {
386
386
  ];
387
387
  if (config.build.rollupOptions.output) {
388
388
  const outputOptions = config.build.rollupOptions.output;
389
- if (Array.isArray(outputOptions)) {
390
- distDirs.push(
391
- ...outputOptions.map((option) => option.dir).filter(Boolean)
392
- );
393
- } else {
394
- if (outputOptions.dir)
395
- distDirs.push(outputOptions.dir);
396
- }
389
+ const outputDirs = Array.isArray(outputOptions) ? outputOptions.map((option) => option.dir).filter(Boolean) : outputOptions.dir ? [outputOptions.dir] : [];
390
+ outputDirs.forEach((dir) => {
391
+ distDirs.push(dir);
392
+ if (!isAbsolute(dir))
393
+ distDirs.push(resolve(config.root, dir));
394
+ });
397
395
  }
398
396
  const cssPostPlugin = config.plugins.find((i) => i.name === "vite:css-post");
399
397
  const cssPlugin = config.plugins.find((i) => i.name === "vite:css");
@@ -854,19 +852,6 @@ async function transformSvelteSFC(code, id, uno, options = {}) {
854
852
  const classes = [...code.matchAll(classesRE)];
855
853
  const classDirectives = [...code.matchAll(classesDirectivesRE)];
856
854
  const classDirectivesShorthand = [...code.matchAll(classDirectivesShorthandRE)];
857
- if (!classes.length && !classDirectives.length && !classDirectivesShorthand.length) {
858
- if (preflights || safelist) {
859
- if (alreadyHasStyles) {
860
- return {
861
- code: code.replace(/(<style[^>]*>)/, `$1${styles}`)
862
- };
863
- }
864
- return { code: `${code}
865
- <style>${styles}</style>` };
866
- } else {
867
- return;
868
- }
869
- }
870
855
  const originalShortcuts = uno.config.shortcuts;
871
856
  const shortcuts = {};
872
857
  const toGenerate = /* @__PURE__ */ new Set();
@@ -906,13 +891,14 @@ async function transformSvelteSFC(code, id, uno, options = {}) {
906
891
  async function sortKnownAndUnknownClasses(str) {
907
892
  const classArr = str.split(/\s+/);
908
893
  const result = await Promise.all(classArr.filter(Boolean).map(async (t) => [t, !!await uno.parseToken(t)]));
909
- const known = result.filter(([, matched]) => matched).map(([t]) => t).sort();
894
+ const known = result.filter(([, matched2]) => matched2).map(([t]) => t).sort();
910
895
  if (!known.length)
911
896
  return null;
912
- const replacements = result.filter(([, matched]) => !matched).map(([i]) => i);
897
+ const replacements = result.filter(([, matched2]) => !matched2).map(([i]) => i);
913
898
  const className = queueCompiledClass(known);
914
899
  return [className, ...replacements].join(" ");
915
900
  }
901
+ const processedMap = /* @__PURE__ */ new Set();
916
902
  for (const match of classes) {
917
903
  let body = expandVariantGroup(match[2].trim());
918
904
  const inlineConditionals = [...body.matchAll(classesFromInlineConditionalsRE)];
@@ -923,8 +909,10 @@ async function transformSvelteSFC(code, id, uno, options = {}) {
923
909
  }
924
910
  const replacement = await sortKnownAndUnknownClasses(body);
925
911
  if (replacement) {
926
- const start = match.index;
927
- s.overwrite(start + 7, start + match[0].length - 1, replacement);
912
+ const start = match.index + 7;
913
+ const end = match.index + match[0].length - 1;
914
+ processedMap.add(start);
915
+ s.overwrite(start, end, replacement);
928
916
  }
929
917
  }
930
918
  for (const match of classDirectives) {
@@ -934,6 +922,7 @@ async function transformSvelteSFC(code, id, uno, options = {}) {
934
922
  continue;
935
923
  const className = queueCompiledClass([token]);
936
924
  const start = match.index + "class:".length;
925
+ processedMap.add(start);
937
926
  s.overwrite(start, start + match[1].length, className);
938
927
  }
939
928
  for (const match of classDirectivesShorthand) {
@@ -943,8 +932,44 @@ async function transformSvelteSFC(code, id, uno, options = {}) {
943
932
  continue;
944
933
  const className = queueCompiledClass([token]);
945
934
  const start = match.index + "class:".length;
935
+ processedMap.add(start);
946
936
  s.overwrite(start, start + match[1].length, `${className}={${token}}`);
947
937
  }
938
+ const { matched } = await uno.generate(code, { preflights: false, safelist: false, minify: true });
939
+ for (const token of matched) {
940
+ const match = token.match(attributifyRE);
941
+ if (match) {
942
+ const [, name, value] = match;
943
+ if (!value) {
944
+ let start = 0;
945
+ code.split(/([\s"'`;*]|:\(|\)"|\)\s)/g).forEach((i) => {
946
+ const end = start + i.length;
947
+ if (i === name && !processedMap.has(start)) {
948
+ const className = queueCompiledClass([name]);
949
+ s.appendLeft(start, `class:${className}={true} `);
950
+ s.overwrite(start, end, "");
951
+ }
952
+ start = end;
953
+ });
954
+ } else {
955
+ const regex = new RegExp(`(${escapeRegExp(name)}=)(['"])[^\\2]*?${escapeRegExp(value)}[^\\2]*?\\2`, "g");
956
+ for (const match2 of code.matchAll(regex)) {
957
+ const escaped = match2[1];
958
+ const body = match2[0].slice(escaped.length);
959
+ let bodyIndex = body.match(`[\\b\\s'"]${escapeRegExp(value)}[\\b\\s'"]`)?.index ?? -1;
960
+ if (body[bodyIndex]?.match(/[\s'"]/))
961
+ bodyIndex++;
962
+ if (bodyIndex < 0)
963
+ return;
964
+ const [, base] = await uno.matchVariants(value);
965
+ const variants = value.replace(base, "");
966
+ const className = queueCompiledClass([`${variants + name}-${base}`]);
967
+ s.appendLeft(match2.index, `class:${className}={true} `);
968
+ s.overwrite(match2.index, match2.index + match2[0].length, "");
969
+ }
970
+ }
971
+ }
972
+ }
948
973
  uno.config.shortcuts = [...originalShortcuts, ...Object.entries(shortcuts)];
949
974
  const { css } = await uno.generate(toGenerate, { preflights: false, safelist: false, minify: true });
950
975
  styles += wrapSelectorsWithGlobal(css);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/vite",
3
- "version": "0.50.0",
3
+ "version": "0.50.2",
4
4
  "description": "The Vite plugin for UnoCSS",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -44,17 +44,17 @@
44
44
  "dependencies": {
45
45
  "@ampproject/remapping": "^2.2.0",
46
46
  "@rollup/pluginutils": "^5.0.2",
47
- "@unocss/config": "0.50.0",
48
- "@unocss/core": "0.50.0",
49
- "@unocss/inspector": "0.50.0",
50
- "@unocss/scope": "0.50.0",
51
- "@unocss/transformer-directives": "0.50.0",
47
+ "@unocss/config": "0.50.2",
48
+ "@unocss/core": "0.50.2",
49
+ "@unocss/inspector": "0.50.2",
50
+ "@unocss/scope": "0.50.2",
51
+ "@unocss/transformer-directives": "0.50.2",
52
52
  "chokidar": "^3.5.3",
53
53
  "fast-glob": "^3.2.12",
54
- "magic-string": "^0.29.0"
54
+ "magic-string": "^0.30.0"
55
55
  },
56
56
  "devDependencies": {
57
- "@unocss/shared-integration": "0.50.0",
57
+ "@unocss/shared-integration": "0.50.2",
58
58
  "vite": "^4.1.4"
59
59
  },
60
60
  "scripts": {