@plumeria/turbopack-loader 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 +62 -33
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -205,17 +205,45 @@ const foldDynamicVars = (vars) => {
205
205
  return `"${cssVar}": ${value}`;
206
206
  });
207
207
  };
208
- const findVarProp = (style, cssVar) => {
208
+ const isUnitlessProp = (prop) => zss_engine_1.exceptionCamelCase.includes(prop) || prop.startsWith('--');
209
+ const collectVarProps = (style, cssVar, props) => {
210
+ const reference = `var(${cssVar})`;
211
+ for (const [prop, value] of Object.entries(style)) {
212
+ if (typeof value === 'string' && value.includes(reference))
213
+ props.push(prop);
214
+ else if (value !== null && typeof value === 'object')
215
+ collectVarProps(value, cssVar, props);
216
+ }
217
+ };
218
+ const retargetVar = (style, cssVar, next, unitless) => {
219
+ const reference = `var(${cssVar})`;
209
220
  for (const [prop, value] of Object.entries(style)) {
210
- if (typeof value === 'string' && value.includes(cssVar))
211
- return prop;
212
- if (value !== null && typeof value === 'object') {
213
- const nested = findVarProp(value, cssVar);
214
- if (nested)
215
- return nested;
221
+ if (typeof value === 'string' && value.includes(reference)) {
222
+ if (isUnitlessProp(prop) === unitless)
223
+ style[prop] = value
224
+ .split(reference)
225
+ .join(`var(${next})`);
226
+ }
227
+ else if (value !== null && typeof value === 'object') {
228
+ retargetVar(value, cssVar, next, unitless);
216
229
  }
217
230
  }
218
- return undefined;
231
+ };
232
+ const splitVarByUnit = (style, cssVar) => {
233
+ const props = [];
234
+ collectVarProps(style, cssVar, props);
235
+ if (props.length === 0)
236
+ return [];
237
+ const lead = props[0];
238
+ const split = props.find((prop) => isUnitlessProp(prop) !== isUnitlessProp(lead));
239
+ if (!split)
240
+ return [{ cssVar, prop: lead }];
241
+ const splitVar = `${cssVar}-${(0, zss_engine_1.camelToKebabCase)(split).replace(/^-+/, '')}`;
242
+ retargetVar(style, cssVar, splitVar, isUnitlessProp(split));
243
+ return [
244
+ { cssVar, prop: lead },
245
+ { cssVar: splitVar, prop: split },
246
+ ];
219
247
  };
220
248
  const applyVarFallback = (style, cssVar, literal) => {
221
249
  const reference = `var(${cssVar})`;
@@ -1036,47 +1064,48 @@ async function loader(source) {
1036
1064
  const style = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
1037
1065
  if (!style)
1038
1066
  return null;
1039
- withDefault.forEach(([param, expr]) => {
1067
+ const varGroups = new Map();
1068
+ varParams.forEach((param) => {
1040
1069
  const cssVar = cssVars[param];
1041
- const targetProp = cssVar ? findVarProp(style, cssVar) : undefined;
1042
- if (!targetProp)
1043
- return;
1070
+ if (cssVar)
1071
+ varGroups.set(param, splitVarByUnit(style, cssVar));
1072
+ });
1073
+ withDefault.forEach(([param, expr]) => {
1044
1074
  const literal = expr.type === 'StringLiteral' || expr.type === 'NumericLiteral'
1045
1075
  ? expr.value
1046
1076
  : undefined;
1047
1077
  if (literal === undefined)
1048
1078
  return;
1049
- applyVarFallback(style, cssVar, literal);
1079
+ (varGroups.get(param) ?? []).forEach(({ cssVar }) => applyVarFallback(style, cssVar, literal));
1050
1080
  });
1051
1081
  const vars = [];
1052
1082
  runtime.forEach(({ param, source }) => {
1053
- const cssVar = cssVars[param];
1054
- const targetProp = cssVar ? findVarProp(style, cssVar) : undefined;
1055
- if (!targetProp)
1083
+ const groups = varGroups.get(param);
1084
+ if (!groups?.length)
1056
1085
  return;
1057
1086
  const argStart = source.span.start - baseByteOffset;
1058
1087
  const argEnd = source.span.end - baseByteOffset;
1059
1088
  const argSource = sourceBuffer
1060
1089
  .subarray(argStart, argEnd)
1061
1090
  .toString('utf-8');
1062
- let valueExpr;
1063
- const kebabProp = (0, zss_engine_1.camelToKebabCase)(targetProp);
1064
- const isUnitless = zss_engine_1.exceptionCamelCase.includes(targetProp) ||
1065
- targetProp.startsWith('--');
1066
1091
  const maybeNumber = Number(argSource);
1067
- if (!isNaN(maybeNumber) && argSource.trim() === String(maybeNumber)) {
1068
- valueExpr = JSON.stringify((0, zss_engine_1.applyCssValue)(maybeNumber, kebabProp));
1069
- }
1070
- else if ((argSource.startsWith('"') && argSource.endsWith('"')) ||
1071
- (argSource.startsWith("'") && argSource.endsWith("'"))) {
1072
- valueExpr = JSON.stringify((0, zss_engine_1.applyCssValue)(argSource.slice(1, -1), kebabProp));
1073
- }
1074
- else {
1075
- valueExpr = isUnitless
1076
- ? argSource
1077
- : `(typeof (${argSource}) === 'number' ? (${argSource}) + 'px' : (${argSource}))`;
1078
- }
1079
- vars.push({ cssVar, valueExpr });
1092
+ groups.forEach(({ cssVar, prop }) => {
1093
+ let valueExpr;
1094
+ const kebabProp = (0, zss_engine_1.camelToKebabCase)(prop);
1095
+ if (!isNaN(maybeNumber) && argSource.trim() === String(maybeNumber)) {
1096
+ valueExpr = JSON.stringify((0, zss_engine_1.applyCssValue)(maybeNumber, kebabProp));
1097
+ }
1098
+ else if ((argSource.startsWith('"') && argSource.endsWith('"')) ||
1099
+ (argSource.startsWith("'") && argSource.endsWith("'"))) {
1100
+ valueExpr = JSON.stringify((0, zss_engine_1.applyCssValue)(argSource.slice(1, -1), kebabProp));
1101
+ }
1102
+ else {
1103
+ valueExpr = isUnitlessProp(prop)
1104
+ ? argSource
1105
+ : `(typeof (${argSource}) === 'number' ? (${argSource}) + 'px' : (${argSource}))`;
1106
+ }
1107
+ vars.push({ cssVar, valueExpr });
1108
+ });
1080
1109
  });
1081
1110
  return { style, vars };
1082
1111
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/turbopack-loader",
3
- "version": "18.3.17",
3
+ "version": "18.3.18",
4
4
  "description": "Plumeria Turbopack-loader",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -29,8 +29,8 @@
29
29
  "zero-virtual.css"
30
30
  ],
31
31
  "dependencies": {
32
- "@plumeria/compiler": "^18.3.17",
33
- "@plumeria/utils": "^18.3.17"
32
+ "@plumeria/compiler": "^18.3.18",
33
+ "@plumeria/utils": "^18.3.18"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@swc/core": "1.15.47",