@plumeria/turbopack-loader 18.0.1 → 18.1.0

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 +115 -9
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -809,6 +809,95 @@ async function loader(source) {
809
809
  return scannedTables.createObjectTable[hash];
810
810
  return null;
811
811
  };
812
+ const resolveBracketGroup = (node) => {
813
+ if (!utils_1.t.isMemberExpression(node) ||
814
+ !utils_1.t.isIdentifier(node.object) ||
815
+ node.property.type !== 'Computed' ||
816
+ utils_1.t.isStringLiteral(node.property.expression)) {
817
+ return null;
818
+ }
819
+ const varName = node.object.value;
820
+ const obj = resolveCreateObject(varName);
821
+ return obj ? { varName, keyExpr: node.property.expression, obj } : null;
822
+ };
823
+ const buildDecision = (node, prefix, off, counter) => {
824
+ if (node.type === 'ParenthesisExpression') {
825
+ return buildDecision(node.expression, prefix, off, counter);
826
+ }
827
+ if (node.type === 'ConditionalExpression') {
828
+ const a = buildDecision(node.consequent, prefix, off, counter);
829
+ if (!a)
830
+ return null;
831
+ const b = buildDecision(node.alternate, prefix, off, counter);
832
+ if (!b)
833
+ return null;
834
+ return {
835
+ keyExpr: `((${getSource(node.test)}) ? ${a.keyExpr} : ${b.keyExpr})`,
836
+ options: [...a.options, ...b.options],
837
+ leaves: a.leaves + b.leaves,
838
+ hasGroup: a.hasGroup || b.hasGroup,
839
+ };
840
+ }
841
+ if (node.type === 'BinaryExpression' && node.operator === '&&') {
842
+ const right = buildDecision(node.right, prefix, off, counter);
843
+ if (!right)
844
+ return null;
845
+ return {
846
+ keyExpr: `((${getSource(node.left)}) ? ${right.keyExpr} : ${JSON.stringify(off)})`,
847
+ options: right.options,
848
+ leaves: right.leaves,
849
+ hasGroup: right.hasGroup,
850
+ };
851
+ }
852
+ const group = resolveBracketGroup(node);
853
+ if (group) {
854
+ return {
855
+ keyExpr: getSource(group.keyExpr),
856
+ options: Object.entries(group.obj).map(([value, style]) => ({
857
+ value,
858
+ style: style,
859
+ })),
860
+ leaves: 1,
861
+ hasGroup: true,
862
+ };
863
+ }
864
+ const style = resolveStyleObject(node);
865
+ if (!style)
866
+ return null;
867
+ const value = `${prefix}${counter.n++}`;
868
+ return {
869
+ keyExpr: JSON.stringify(value),
870
+ options: [{ value, style }],
871
+ leaves: 1,
872
+ hasGroup: false,
873
+ };
874
+ };
875
+ const collectGroupKeys = (node, acc) => {
876
+ if (node.type === 'ParenthesisExpression') {
877
+ collectGroupKeys(node.expression, acc);
878
+ }
879
+ else if (node.type === 'ConditionalExpression') {
880
+ collectGroupKeys(node.consequent, acc);
881
+ collectGroupKeys(node.alternate, acc);
882
+ }
883
+ else if (node.type === 'BinaryExpression' && node.operator === '&&') {
884
+ collectGroupKeys(node.right, acc);
885
+ }
886
+ else {
887
+ const group = resolveBracketGroup(node);
888
+ if (group)
889
+ Object.keys(group.obj).forEach((k) => acc.add(k));
890
+ }
891
+ };
892
+ const resolveDecision = (node) => {
893
+ const groupKeys = new Set();
894
+ collectGroupKeys(node, groupKeys);
895
+ let prefix = '#';
896
+ while ([...groupKeys].some((k) => k.startsWith(prefix)))
897
+ prefix += '#';
898
+ const off = groupKeys.has('') ? `${prefix}off` : '';
899
+ return buildDecision(node, prefix, off, { n: 0 });
900
+ };
812
901
  const collectConditions = (node, currentTestStrings = []) => {
813
902
  const staticStyle = resolveStyleObject(node);
814
903
  if (staticStyle) {
@@ -862,14 +951,6 @@ async function loader(source) {
862
951
  else if (node.type === 'ParenthesisExpression') {
863
952
  return collectConditions(node.expression, currentTestStrings);
864
953
  }
865
- if (currentTestStrings.length > 0 &&
866
- utils_1.t.isMemberExpression(node) &&
867
- utils_1.t.isIdentifier(node.object) &&
868
- node.property.type === 'Computed' &&
869
- resolveCreateObject(node.object.value)) {
870
- const varName = node.object.value;
871
- throwCompilationError(`Plumeria: "${getSource(node)}" cannot be used inside a condition, because its bracket key is not a literal.\nMove the condition into the brackets instead, e.g. ${varName}[cond ? 'a' : 'b'].`, node);
872
- }
873
954
  assertResolvable(node);
874
955
  return false;
875
956
  };
@@ -1074,6 +1155,22 @@ async function loader(source) {
1074
1155
  continue;
1075
1156
  }
1076
1157
  }
1158
+ const decision = resolveDecision(expr);
1159
+ if (decision && (decision.hasGroup || decision.leaves > 2)) {
1160
+ const groupId = ++groupIdCounter;
1161
+ decision.options.forEach(({ value, style }) => conditionals.push({
1162
+ test: expr,
1163
+ testLHS: decision.keyExpr,
1164
+ testString: `${decision.keyExpr} === '${value}'`,
1165
+ truthy: style,
1166
+ falsy: {},
1167
+ groupId,
1168
+ groupName: undefined,
1169
+ valueName: value,
1170
+ varName: undefined,
1171
+ }));
1172
+ continue;
1173
+ }
1077
1174
  const handled = collectConditions(expr);
1078
1175
  if (handled)
1079
1176
  continue;
@@ -1203,7 +1300,7 @@ async function loader(source) {
1203
1300
  getSource(options[0].test);
1204
1301
  const lookupMap = {};
1205
1302
  options.forEach((opt) => {
1206
- if (opt.valueName && opt.truthy) {
1303
+ if (opt.valueName !== undefined && opt.truthy) {
1207
1304
  const className = processStyleRecords(opt.truthy)
1208
1305
  .map((r) => r.hash)
1209
1306
  .join(' ');
@@ -1241,6 +1338,15 @@ async function loader(source) {
1241
1338
  conflictVarGroups[c.groupId].push(c);
1242
1339
  }
1243
1340
  });
1341
+ Object.keys(conflictVarGroups).forEach((id) => {
1342
+ const groupId = Number(id);
1343
+ const opts = conflictVarGroups[groupId];
1344
+ if (opts.some((c) => c.valueName === ''))
1345
+ return;
1346
+ const off = conditionals.find((c) => c.groupId === groupId && c.valueName === '');
1347
+ if (off)
1348
+ opts.push({ ...off, truthy: {}, falsy: {} });
1349
+ });
1244
1350
  Object.entries(conflictVarGroups).forEach(([, opts]) => {
1245
1351
  dimensions.push({
1246
1352
  type: 'var',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/turbopack-loader",
3
- "version": "18.0.1",
3
+ "version": "18.1.0",
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.0.1",
26
- "@plumeria/utils": "^18.0.1"
25
+ "@plumeria/compiler": "^18.1.0",
26
+ "@plumeria/utils": "^18.1.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@swc/core": "1.15.43",