tailwind-to-style 1.1.5 → 1.1.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.
Files changed (2) hide show
  1. package/index.js +53 -19
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -5678,28 +5678,62 @@ function jsonToStyle(json) {
5678
5678
  .join(" ");
5679
5679
  }
5680
5680
 
5681
- function replaceAndRemoveCSSVariables(styleString) {
5682
- const customProperties = {};
5683
- const variableRegex = /--([\w-]+):\s*([^;]+);/g;
5684
- let match;
5681
+ function separateAndResolveCSS(arr) {
5682
+ const cssProperties = [];
5683
+ const cssVariables = [];
5684
+ const variableMap = {};
5685
+
5686
+ arr.forEach((item) => {
5687
+ const declarations = item
5688
+ .split(";")
5689
+ .map((decl) => decl.trim())
5690
+ .filter((decl) => decl);
5691
+
5692
+ declarations.forEach((declaration) => {
5693
+ if (declaration.startsWith("--")) {
5694
+ const [key, value] = declaration.split(":").map((part) => part.trim());
5695
+ variableMap[key] = value;
5696
+ cssVariables.push(declaration + ";");
5697
+ } else {
5698
+ cssProperties.push(declaration + ";");
5699
+ }
5700
+ });
5701
+ });
5685
5702
 
5686
- while ((match = variableRegex.exec(styleString)) !== null) {
5687
- const [, variableName, value] = match;
5688
- customProperties[variableName] = value.trim();
5703
+ function resolveVariable(value) {
5704
+ return value.replace(/var\((--[\w-]+)\)/g, (_, varName) => {
5705
+ if (variableMap[varName]) {
5706
+ return resolveVariable(variableMap[varName]);
5707
+ }
5708
+ return `var(${varName})`;
5709
+ });
5689
5710
  }
5690
5711
 
5691
- let updatedStyleString = styleString.replace(
5692
- /var\(--([\w-]+)\)/g,
5693
- (_, variableName) => {
5694
- return customProperties[variableName] || `var(--${variableName})`;
5695
- }
5696
- );
5712
+ const resolvedVariables = cssVariables.map((variable) => {
5713
+ const [key, value] = variable.split(":").map((part) => part.trim());
5714
+ const resolvedValue = resolveVariable(value.slice(0, -1));
5715
+ return `${key}: ${resolvedValue};`;
5716
+ });
5717
+
5718
+ const uniqueVariables = [
5719
+ ...new Map(
5720
+ resolvedVariables.reverse().map((variable) => {
5721
+ const key = variable.split(":")[0].trim();
5722
+ return [key, variable];
5723
+ })
5724
+ ).values(),
5725
+ ].reverse();
5697
5726
 
5698
- updatedStyleString = updatedStyleString
5699
- .replace(/--[\w-]+:\s*[^;]+;/g, "")
5700
- .trim();
5727
+ uniqueVariables.forEach((variable) => {
5728
+ const [key, value] = variable.split(":").map((part) => part.trim());
5729
+ variableMap[key] = value.slice(0, -1);
5730
+ });
5731
+
5732
+ const resolvedCSSProperties = cssProperties.map((property) =>
5733
+ resolveVariable(property)
5734
+ );
5701
5735
 
5702
- return updatedStyleString;
5736
+ return resolvedCSSProperties;
5703
5737
  }
5704
5738
 
5705
5739
  const breakpoints = {
@@ -5745,8 +5779,8 @@ function tws(classNames, convertToJson) {
5745
5779
  return "";
5746
5780
  });
5747
5781
 
5748
- cssResult = replaceAndRemoveCSSVariables(cssResult.join(""));
5749
- cssResult = inlineStyleToJson(cssResult);
5782
+ cssResult = separateAndResolveCSS(cssResult);
5783
+ cssResult = inlineStyleToJson(cssResult.join(""));
5750
5784
 
5751
5785
  if (!convertToJson) {
5752
5786
  cssResult = jsonToStyle(cssResult);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tailwind-to-style",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "Convert tailwind classes to inline style",
5
5
  "main": "index.js",
6
6
  "scripts": {