@unocss/vite 0.50.1 → 0.50.3

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