@plumeria/turbopack-loader 18.1.0 → 18.1.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.
Files changed (2) hide show
  1. package/dist/index.js +116 -37
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -796,6 +796,8 @@ async function loader(source) {
796
796
  });
797
797
  const conditionals = [];
798
798
  let groupIdCounter = 0;
799
+ let sourceOrder = 0;
800
+ const baseChunks = [];
799
801
  let baseStyle = {};
800
802
  let isOptimizable = true;
801
803
  const resolveCreateObject = (varName) => {
@@ -820,15 +822,15 @@ async function loader(source) {
820
822
  const obj = resolveCreateObject(varName);
821
823
  return obj ? { varName, keyExpr: node.property.expression, obj } : null;
822
824
  };
823
- const buildDecision = (node, prefix, off, counter) => {
825
+ const buildDecision = (node, prefix, off, counter, scoped) => {
824
826
  if (node.type === 'ParenthesisExpression') {
825
- return buildDecision(node.expression, prefix, off, counter);
827
+ return buildDecision(node.expression, prefix, off, counter, scoped);
826
828
  }
827
829
  if (node.type === 'ConditionalExpression') {
828
- const a = buildDecision(node.consequent, prefix, off, counter);
830
+ const a = buildDecision(node.consequent, prefix, off, counter, scoped);
829
831
  if (!a)
830
832
  return null;
831
- const b = buildDecision(node.alternate, prefix, off, counter);
833
+ const b = buildDecision(node.alternate, prefix, off, counter, scoped);
832
834
  if (!b)
833
835
  return null;
834
836
  return {
@@ -839,7 +841,7 @@ async function loader(source) {
839
841
  };
840
842
  }
841
843
  if (node.type === 'BinaryExpression' && node.operator === '&&') {
842
- const right = buildDecision(node.right, prefix, off, counter);
844
+ const right = buildDecision(node.right, prefix, off, counter, scoped);
843
845
  if (!right)
844
846
  return null;
845
847
  return {
@@ -851,10 +853,14 @@ async function loader(source) {
851
853
  }
852
854
  const group = resolveBracketGroup(node);
853
855
  if (group) {
856
+ const tag = scoped ? `${counter.n++}:` : '';
857
+ const keySource = getSource(group.keyExpr);
854
858
  return {
855
- keyExpr: getSource(group.keyExpr),
859
+ keyExpr: tag
860
+ ? `(${JSON.stringify(tag)} + ${keySource})`
861
+ : keySource,
856
862
  options: Object.entries(group.obj).map(([value, style]) => ({
857
- value,
863
+ value: `${tag}${value}`,
858
864
  style: style,
859
865
  })),
860
866
  leaves: 1,
@@ -886,23 +892,30 @@ async function loader(source) {
886
892
  else {
887
893
  const group = resolveBracketGroup(node);
888
894
  if (group)
889
- Object.keys(group.obj).forEach((k) => acc.add(k));
895
+ Object.entries(group.obj).forEach(([key, style]) => {
896
+ let styles = acc.get(key);
897
+ if (!styles)
898
+ acc.set(key, (styles = new Set()));
899
+ styles.add(style);
900
+ });
890
901
  }
891
902
  };
892
903
  const resolveDecision = (node) => {
893
- const groupKeys = new Set();
904
+ const groupKeys = new Map();
894
905
  collectGroupKeys(node, groupKeys);
895
906
  let prefix = '#';
896
- while ([...groupKeys].some((k) => k.startsWith(prefix)))
907
+ while ([...groupKeys.keys()].some((k) => k.startsWith(prefix)))
897
908
  prefix += '#';
898
- const off = groupKeys.has('') ? `${prefix}off` : '';
899
- return buildDecision(node, prefix, off, { n: 0 });
909
+ const scoped = [...groupKeys.values()].some((styles) => styles.size > 1);
910
+ const off = !scoped && groupKeys.has('') ? `${prefix}off` : '';
911
+ return buildDecision(node, prefix, off, { n: 0 }, scoped);
900
912
  };
901
913
  const collectConditions = (node, currentTestStrings = []) => {
902
914
  const staticStyle = resolveStyleObject(node);
903
915
  if (staticStyle) {
904
916
  if (currentTestStrings.length === 0) {
905
917
  baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
918
+ baseChunks.push({ order: sourceOrder++, style: staticStyle });
906
919
  }
907
920
  else {
908
921
  conditionals.push({
@@ -911,6 +924,7 @@ async function loader(source) {
911
924
  truthy: staticStyle,
912
925
  falsy: {},
913
926
  varName: undefined,
927
+ order: sourceOrder++,
914
928
  });
915
929
  }
916
930
  return true;
@@ -927,6 +941,7 @@ async function loader(source) {
927
941
  truthy: trueStyle,
928
942
  falsy: falseStyle,
929
943
  varName: undefined,
944
+ order: sourceOrder++,
930
945
  });
931
946
  return true;
932
947
  }
@@ -977,6 +992,7 @@ async function loader(source) {
977
992
  }
978
993
  if (propPossibilities && propPossibilities.length > 0) {
979
994
  const currentGroupId = ++groupIdCounter;
995
+ const currentOrder = sourceOrder++;
980
996
  const uniqueEntries = [];
981
997
  propPossibilities.forEach((entry) => {
982
998
  if (!uniqueEntries.some((x) => x.key === entry.key)) {
@@ -991,6 +1007,7 @@ async function loader(source) {
991
1007
  truthy: entry.styleObj,
992
1008
  falsy: {},
993
1009
  groupId: currentGroupId,
1010
+ order: currentOrder,
994
1011
  groupName: undefined,
995
1012
  valueName: entry.key,
996
1013
  varName,
@@ -1047,11 +1064,18 @@ async function loader(source) {
1047
1064
  if (!groupVariants)
1048
1065
  continue;
1049
1066
  const currentGroupId = ++groupIdCounter;
1067
+ const currentOrder = sourceOrder++;
1050
1068
  const valSource = getSource(valExpr);
1051
1069
  if (valExpr.type === 'StringLiteral') {
1052
1070
  const groupVariantsAsObj = groupVariants;
1053
- if (groupVariantsAsObj[valExpr.value])
1054
- baseStyle = (0, utils_1.deepMerge)(baseStyle, groupVariantsAsObj[valExpr.value]);
1071
+ const picked = groupVariantsAsObj[valExpr.value];
1072
+ if (picked) {
1073
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, picked);
1074
+ baseChunks.push({
1075
+ order: sourceOrder++,
1076
+ style: picked,
1077
+ });
1078
+ }
1055
1079
  continue;
1056
1080
  }
1057
1081
  Object.entries(groupVariants).forEach(([optionName, style]) => {
@@ -1062,6 +1086,7 @@ async function loader(source) {
1062
1086
  truthy: style,
1063
1087
  falsy: {},
1064
1088
  groupId: currentGroupId,
1089
+ order: currentOrder,
1065
1090
  groupName,
1066
1091
  valueName: optionName,
1067
1092
  varName,
@@ -1073,11 +1098,15 @@ async function loader(source) {
1073
1098
  }
1074
1099
  const argSource = getSource(arg);
1075
1100
  if (utils_1.t.isStringLiteral(arg)) {
1076
- if (variantObj[arg.value])
1077
- baseStyle = (0, utils_1.deepMerge)(baseStyle, variantObj[arg.value]);
1101
+ const picked = variantObj[arg.value];
1102
+ if (picked) {
1103
+ baseStyle = (0, utils_1.deepMerge)(baseStyle, picked);
1104
+ baseChunks.push({ order: sourceOrder++, style: picked });
1105
+ }
1078
1106
  continue;
1079
1107
  }
1080
1108
  const currentGroupId = ++groupIdCounter;
1109
+ const currentOrder = sourceOrder++;
1081
1110
  Object.entries(variantObj).forEach(([key, style]) => {
1082
1111
  conditionals.push({
1083
1112
  test: arg,
@@ -1086,6 +1115,7 @@ async function loader(source) {
1086
1115
  truthy: style,
1087
1116
  falsy: {},
1088
1117
  groupId: currentGroupId,
1118
+ order: currentOrder,
1089
1119
  groupName: undefined,
1090
1120
  valueName: key,
1091
1121
  varName,
@@ -1113,6 +1143,7 @@ async function loader(source) {
1113
1143
  if (!groupVariants)
1114
1144
  return;
1115
1145
  const currentGroupId = ++groupIdCounter;
1146
+ const currentOrder = sourceOrder++;
1116
1147
  Object.entries(groupVariants).forEach(([optionName, style]) => {
1117
1148
  conditionals.push({
1118
1149
  test: expr,
@@ -1121,6 +1152,7 @@ async function loader(source) {
1121
1152
  truthy: style,
1122
1153
  falsy: {},
1123
1154
  groupId: currentGroupId,
1155
+ order: currentOrder,
1124
1156
  groupName,
1125
1157
  valueName: optionName,
1126
1158
  varName,
@@ -1139,6 +1171,7 @@ async function loader(source) {
1139
1171
  const dynExpr = expr.property.expression;
1140
1172
  const dynSource = getSource(dynExpr);
1141
1173
  const currentGroupId = ++groupIdCounter;
1174
+ const currentOrder = sourceOrder++;
1142
1175
  Object.entries(styleObj).forEach(([optionName, style]) => {
1143
1176
  conditionals.push({
1144
1177
  test: dynExpr,
@@ -1147,9 +1180,11 @@ async function loader(source) {
1147
1180
  truthy: style,
1148
1181
  falsy: {},
1149
1182
  groupId: currentGroupId,
1183
+ order: currentOrder,
1150
1184
  groupName: undefined,
1151
1185
  valueName: optionName,
1152
1186
  varName,
1187
+ ownKeys: true,
1153
1188
  });
1154
1189
  });
1155
1190
  continue;
@@ -1158,6 +1193,7 @@ async function loader(source) {
1158
1193
  const decision = resolveDecision(expr);
1159
1194
  if (decision && (decision.hasGroup || decision.leaves > 2)) {
1160
1195
  const groupId = ++groupIdCounter;
1196
+ const currentOrder = sourceOrder++;
1161
1197
  decision.options.forEach(({ value, style }) => conditionals.push({
1162
1198
  test: expr,
1163
1199
  testLHS: decision.keyExpr,
@@ -1165,9 +1201,11 @@ async function loader(source) {
1165
1201
  truthy: style,
1166
1202
  falsy: {},
1167
1203
  groupId,
1204
+ order: currentOrder,
1168
1205
  groupName: undefined,
1169
1206
  valueName: value,
1170
1207
  varName: undefined,
1208
+ ownKeys: true,
1171
1209
  }));
1172
1210
  continue;
1173
1211
  }
@@ -1317,17 +1355,38 @@ async function loader(source) {
1317
1355
  });
1318
1356
  if (Object.keys(baseConflict).length > 0 ||
1319
1357
  conflictConditionals.length > 0) {
1320
- const dimensions = [];
1358
+ const ordered = [];
1359
+ baseChunks.forEach(({ order, style }) => {
1360
+ const conflicting = {};
1361
+ Object.entries(style).forEach(([key, value]) => {
1362
+ if (conflictingKeys.has(key))
1363
+ conflicting[key] = value;
1364
+ });
1365
+ if (Object.keys(conflicting).length === 0)
1366
+ return;
1367
+ ordered.push({
1368
+ order,
1369
+ dimension: {
1370
+ type: 'const',
1371
+ options: [
1372
+ { value: undefined, style: conflicting, label: 'base' },
1373
+ ],
1374
+ },
1375
+ });
1376
+ });
1321
1377
  conflictConditionals
1322
1378
  .filter((c) => c.groupId === undefined)
1323
1379
  .forEach((c) => {
1324
- dimensions.push({
1325
- type: 'std',
1326
- testExpr: c.testString ?? getSource(c.test),
1327
- options: [
1328
- { value: 0, style: c.falsy, label: 'false' },
1329
- { value: 1, style: c.truthy, label: 'true' },
1330
- ],
1380
+ ordered.push({
1381
+ order: c.order ?? 0,
1382
+ dimension: {
1383
+ type: 'std',
1384
+ testExpr: c.testString ?? getSource(c.test),
1385
+ options: [
1386
+ { value: 0, style: c.falsy, label: 'false' },
1387
+ { value: 1, style: c.truthy, label: 'true' },
1388
+ ],
1389
+ },
1331
1390
  });
1332
1391
  });
1333
1392
  const conflictVarGroups = {};
@@ -1348,15 +1407,34 @@ async function loader(source) {
1348
1407
  opts.push({ ...off, truthy: {}, falsy: {} });
1349
1408
  });
1350
1409
  Object.entries(conflictVarGroups).forEach(([, opts]) => {
1351
- dimensions.push({
1352
- type: 'var',
1353
- testExpr: opts[0].testLHS ?? opts[0].testString ?? getSource(opts[0].test),
1354
- options: opts.map((opt) => ({
1355
- value: opt.valueName,
1356
- style: opt.truthy,
1357
- label: opt.valueName || 'default',
1358
- })),
1410
+ ordered.push({
1411
+ order: opts[0].order ?? 0,
1412
+ dimension: {
1413
+ type: 'var',
1414
+ testExpr: opts[0].testLHS ??
1415
+ opts[0].testString ??
1416
+ getSource(opts[0].test),
1417
+ options: opts.map((opt) => ({
1418
+ value: opt.valueName,
1419
+ style: opt.truthy,
1420
+ label: opt.valueName || 'default',
1421
+ })),
1422
+ ownKeys: opts[0].ownKeys,
1423
+ },
1424
+ });
1425
+ });
1426
+ ordered.sort((a, b) => a.order - b.order);
1427
+ const dimensions = ordered.map((o) => o.dimension);
1428
+ const joined = dimensions.filter((dim) => dim.type !== 'const').length > 1;
1429
+ dimensions.forEach((dim) => {
1430
+ if (!joined || dim.type !== 'var' || !dim.ownKeys)
1431
+ return;
1432
+ const numbers = {};
1433
+ dim.options.forEach((opt, index) => {
1434
+ numbers[String(opt.value)] = String(index);
1435
+ opt.value = String(index);
1359
1436
  });
1437
+ dim.testExpr = `(${JSON.stringify(numbers)}[${dim.testExpr}] || "")`;
1360
1438
  });
1361
1439
  const results = {};
1362
1440
  const recurse = (dimIndex, currentStyle, keyParts) => {
@@ -1368,18 +1446,19 @@ async function loader(source) {
1368
1446
  results[keyParts.join('__')] = className;
1369
1447
  return;
1370
1448
  }
1371
- dimensions[dimIndex].options.forEach((opt) => recurse(dimIndex + 1, (0, utils_1.deepMerge)(currentStyle, opt.style), [
1372
- ...keyParts,
1373
- String(opt.value),
1374
- ]));
1449
+ const dimension = dimensions[dimIndex];
1450
+ dimension.options.forEach((opt) => recurse(dimIndex + 1, (0, utils_1.deepMerge)(currentStyle, opt.style), dimension.type === 'const'
1451
+ ? keyParts
1452
+ : [...keyParts, String(opt.value)]));
1375
1453
  };
1376
- recurse(0, baseConflict, []);
1454
+ recurse(0, {}, []);
1377
1455
  const baseConflictClass = Object.keys(baseConflict).length > 0
1378
1456
  ? processStyleRecords(baseConflict)
1379
1457
  .map((r) => r.hash)
1380
1458
  .join(' ')
1381
1459
  : '';
1382
1460
  const masterKeyExpr = dimensions
1461
+ .filter((dim) => dim.type !== 'const')
1383
1462
  .map((dim) => dim.type === 'std'
1384
1463
  ? `(${dim.testExpr} ? "1" : "0")`
1385
1464
  : dim.testExpr || '""')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/turbopack-loader",
3
- "version": "18.1.0",
3
+ "version": "18.1.2",
4
4
  "description": "Plumeria Turbopack-loader",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -22,8 +22,8 @@
22
22
  "zero-virtual.css"
23
23
  ],
24
24
  "dependencies": {
25
- "@plumeria/compiler": "^18.1.0",
26
- "@plumeria/utils": "^18.1.0"
25
+ "@plumeria/compiler": "^18.1.2",
26
+ "@plumeria/utils": "^18.1.2"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@swc/core": "1.15.43",