@plumeria/compiler 18.3.17 → 18.3.18

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/dist/index.js +47 -4
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -103,6 +103,46 @@ const namedParamsOf = (params, defaults) => {
103
103
  }
104
104
  return named.length > 0 ? named : undefined;
105
105
  };
106
+ const isUnitlessProp = (prop) => zss_engine_1.exceptionCamelCase.includes(prop) || prop.startsWith('--');
107
+ const collectVarProps = (style, cssVar, props) => {
108
+ const reference = `var(${cssVar})`;
109
+ for (const [prop, value] of Object.entries(style)) {
110
+ if (typeof value === 'string' && value.includes(reference))
111
+ props.push(prop);
112
+ else if (value !== null && typeof value === 'object')
113
+ collectVarProps(value, cssVar, props);
114
+ }
115
+ };
116
+ const retargetVar = (style, cssVar, next, unitless) => {
117
+ const reference = `var(${cssVar})`;
118
+ for (const [prop, value] of Object.entries(style)) {
119
+ if (typeof value === 'string' && value.includes(reference)) {
120
+ if (isUnitlessProp(prop) === unitless)
121
+ style[prop] = value
122
+ .split(reference)
123
+ .join(`var(${next})`);
124
+ }
125
+ else if (value !== null && typeof value === 'object') {
126
+ retargetVar(value, cssVar, next, unitless);
127
+ }
128
+ }
129
+ };
130
+ const splitVarByUnit = (style, cssVar) => {
131
+ const props = [];
132
+ collectVarProps(style, cssVar, props);
133
+ if (props.length === 0)
134
+ return [];
135
+ const lead = props[0];
136
+ const split = props.find((prop) => isUnitlessProp(prop) !== isUnitlessProp(lead));
137
+ if (!split)
138
+ return [{ cssVar, prop: lead }];
139
+ const splitVar = `${cssVar}-${(0, zss_engine_1.camelToKebabCase)(split).replace(/^-+/, '')}`;
140
+ retargetVar(style, cssVar, splitVar, isUnitlessProp(split));
141
+ return [
142
+ { cssVar, prop: lead },
143
+ { cssVar: splitVar, prop: split },
144
+ ];
145
+ };
106
146
  const applyVarFallback = (style, cssVar, literal) => {
107
147
  const reference = `var(${cssVar})`;
108
148
  for (const [prop, value] of Object.entries(style)) {
@@ -700,16 +740,19 @@ function compileCSS(options) {
700
740
  const style = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
701
741
  if (!style)
702
742
  return null;
703
- withDefault.forEach(([param, expr]) => {
743
+ const varGroups = new Map();
744
+ varParams.forEach((param) => {
704
745
  const cssVar = cssVars[param];
705
- if (!cssVar)
706
- return;
746
+ if (cssVar)
747
+ varGroups.set(param, splitVarByUnit(style, cssVar));
748
+ });
749
+ withDefault.forEach(([param, expr]) => {
707
750
  const literal = expr.type === 'StringLiteral' || expr.type === 'NumericLiteral'
708
751
  ? expr.value
709
752
  : undefined;
710
753
  if (literal === undefined)
711
754
  return;
712
- applyVarFallback(style, cssVar, literal);
755
+ (varGroups.get(param) ?? []).forEach(({ cssVar }) => applyVarFallback(style, cssVar, literal));
713
756
  });
714
757
  return style;
715
758
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "18.3.17",
3
+ "version": "18.3.18",
4
4
  "description": "Plumeria swc based compiler for statically extracting css",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -21,7 +21,7 @@
21
21
  "dist/"
22
22
  ],
23
23
  "dependencies": {
24
- "@plumeria/utils": "^18.3.17"
24
+ "@plumeria/utils": "^18.3.18"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@rust-gear/glob": "1.1.1",