@plumeria/turbopack-loader 18.2.25 → 18.2.26

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 +89 -35
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -85,7 +85,7 @@ const isStaticArgValue = (node) => node.type === 'StringLiteral' ||
85
85
  (node.type === 'TemplateLiteral' && node.expressions.length === 0) ||
86
86
  utils_1.t.isIdentifier(node) ||
87
87
  utils_1.t.isMemberExpression(node);
88
- const namedParamsOf = (params) => {
88
+ const namedParamsOf = (params, defaults) => {
89
89
  if (params.length !== 1)
90
90
  return undefined;
91
91
  const first = params[0];
@@ -94,16 +94,24 @@ const namedParamsOf = (params) => {
94
94
  return undefined;
95
95
  const named = [];
96
96
  for (const prop of pattern.properties ?? []) {
97
- if (prop.type === 'AssignmentPatternProperty' &&
98
- utils_1.t.isIdentifier(prop.key) &&
99
- !prop.value) {
97
+ if (prop.type === 'AssignmentPatternProperty' && utils_1.t.isIdentifier(prop.key)) {
100
98
  named.push({ key: prop.key.value, local: prop.key.value });
99
+ if (prop.value)
100
+ defaults[prop.key.value] = prop.value;
101
101
  }
102
102
  else if (prop.type === 'KeyValuePatternProperty' &&
103
103
  utils_1.t.isIdentifier(prop.key) &&
104
104
  utils_1.t.isIdentifier(prop.value)) {
105
105
  named.push({ key: prop.key.value, local: prop.value.value });
106
106
  }
107
+ else if (prop.type === 'KeyValuePatternProperty' &&
108
+ utils_1.t.isIdentifier(prop.key) &&
109
+ prop.value?.type === 'AssignmentPattern' &&
110
+ utils_1.t.isIdentifier(prop.value.left)) {
111
+ const pattern = prop.value;
112
+ named.push({ key: String(prop.key.value), local: pattern.left.value });
113
+ defaults[pattern.left.value] = pattern.right;
114
+ }
107
115
  else {
108
116
  return undefined;
109
117
  }
@@ -119,14 +127,16 @@ const styleFunctionsOf = (objExpr) => {
119
127
  if (func.type !== 'ArrowFunctionExpression' &&
120
128
  func.type !== 'FunctionExpression')
121
129
  return;
130
+ const defaults = {};
122
131
  const params = func.params.map((p) => {
123
- if (utils_1.t.isIdentifier(p))
124
- return p.value;
125
- if (typeof p === 'object' &&
126
- p !== null &&
127
- 'pat' in p &&
128
- utils_1.t.isIdentifier(p.pat))
129
- return p.pat.value;
132
+ const pattern = typeof p === 'object' && p !== null && 'pat' in p ? p.pat : p;
133
+ if (utils_1.t.isIdentifier(pattern))
134
+ return pattern.value;
135
+ if (pattern?.type === 'AssignmentPattern' &&
136
+ utils_1.t.isIdentifier(pattern.left)) {
137
+ defaults[pattern.left.value] = pattern.right;
138
+ return pattern.left.value;
139
+ }
130
140
  return 'arg';
131
141
  });
132
142
  let actualBody = func.body;
@@ -142,7 +152,8 @@ const styleFunctionsOf = (objExpr) => {
142
152
  if (actualBody && actualBody.type === 'ObjectExpression') {
143
153
  styleFunctions[prop.key.value] = {
144
154
  params,
145
- named: namedParamsOf(func.params),
155
+ named: namedParamsOf(func.params, defaults),
156
+ defaults,
146
157
  body: actualBody,
147
158
  };
148
159
  }
@@ -182,6 +193,19 @@ const findVarProp = (style, cssVar) => {
182
193
  }
183
194
  return undefined;
184
195
  };
196
+ const replaceVarValue = (style, cssVar, next) => {
197
+ for (const [prop, value] of Object.entries(style)) {
198
+ if (typeof value === 'string' && value.includes(cssVar)) {
199
+ style[prop] = next;
200
+ return true;
201
+ }
202
+ if (value !== null && typeof value === 'object') {
203
+ if (replaceVarValue(value, cssVar, next))
204
+ return true;
205
+ }
206
+ }
207
+ return false;
208
+ };
185
209
  async function loader(source) {
186
210
  const callback = this.async();
187
211
  const loaderOptions = this.getOptions?.() ??
@@ -294,11 +318,12 @@ async function loader(source) {
294
318
  }
295
319
  }
296
320
  const appliedStyleProps = new Set();
321
+ const ownerComponentOf = (at) => components.find((c) => at.span.start >= c.node.span.start &&
322
+ at.span.start <= c.node.span.end)?.name;
297
323
  const markStylePropApplied = (name, at) => {
298
- const owner = components.find((c) => at.span.start >= c.node.span.start &&
299
- at.span.start <= c.node.span.end);
324
+ const owner = ownerComponentOf(at);
300
325
  if (owner) {
301
- appliedStyleProps.add(`${owner.name}:${name}`);
326
+ appliedStyleProps.add(`${owner}:${name}`);
302
327
  }
303
328
  };
304
329
  const extractedSheets = [];
@@ -730,7 +755,7 @@ async function loader(source) {
730
755
  }
731
756
  if (propName) {
732
757
  const args = node.arguments;
733
- if (propName === 'keyframes') {
758
+ if (propName === 'keyframes' && args.length > 0) {
734
759
  const expr = args[0].expression;
735
760
  if (utils_1.t.isObjectExpression(expr)) {
736
761
  const obj = (0, utils_1.objectExpressionToObject)(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
@@ -895,18 +920,19 @@ async function loader(source) {
895
920
  if (!func)
896
921
  return null;
897
922
  const callArgs = expr.arguments;
898
- if (callArgs.some((a) => a.spread) || callArgs.length === 0)
923
+ if (callArgs.some((a) => a.spread))
899
924
  return null;
900
925
  const tempStaticTable = { ...mergedStaticTable };
901
926
  const runtime = [];
902
927
  const resolveObjectArg = (argExpr) => (0, utils_1.objectExpressionToObject)(argExpr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
903
928
  if (func.named) {
904
- const argExpr = callArgs[0].expression;
905
- if (callArgs.length !== 1 || argExpr.type !== 'ObjectExpression') {
929
+ const argExpr = callArgs[0]?.expression;
930
+ if (callArgs.length > 1 ||
931
+ (argExpr && argExpr.type !== 'ObjectExpression')) {
906
932
  throwCompilationError(`Plumeria: ${getSource(expr)} takes one object argument, because ${callee.property.value} destructures its parameter.`, expr);
907
933
  }
908
934
  const given = new Map();
909
- argExpr.properties.forEach((prop) => {
935
+ (argExpr?.properties ?? []).forEach((prop) => {
910
936
  if (prop.type === 'Identifier') {
911
937
  given.set(prop.value, prop);
912
938
  }
@@ -915,10 +941,14 @@ async function loader(source) {
915
941
  given.set(String(prop.key.value), prop.value);
916
942
  }
917
943
  });
918
- const argObj = resolveObjectArg(argExpr) ?? {};
944
+ const argObj = argExpr
945
+ ? (resolveObjectArg(argExpr) ?? {})
946
+ : {};
919
947
  func.named.forEach(({ key, local }) => {
920
948
  const source = given.get(key);
921
949
  if (!source) {
950
+ if (func.defaults?.[local])
951
+ return;
922
952
  throwCompilationError(`Plumeria: ${getSource(expr)} leaves "${key}" unset, and a dynamic style function has no value to fall back on.`, expr);
923
953
  return;
924
954
  }
@@ -944,12 +974,17 @@ async function loader(source) {
944
974
  runtime.push({ param: p, source: callArg.expression });
945
975
  });
946
976
  }
977
+ const withDefault = Object.entries(func.defaults ?? {}).filter(([param]) => tempStaticTable[param] === undefined);
947
978
  const cssVars = {};
948
- if (runtime.length > 0) {
949
- runtime.forEach(({ param }) => (tempStaticTable[param] = param));
979
+ const varParams = Array.from(new Set([
980
+ ...runtime.map(({ param }) => param),
981
+ ...withDefault.map(([param]) => param),
982
+ ]));
983
+ if (varParams.length > 0) {
984
+ varParams.forEach((param) => (tempStaticTable[param] = param));
950
985
  const probe = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
951
986
  const hash = (0, zss_engine_1.genBase36Hash)(probe ?? {}, 1, 8);
952
- runtime.forEach(({ param }) => {
987
+ varParams.forEach((param) => {
953
988
  const cssVar = `--${hash}-${param}`;
954
989
  tempStaticTable[param] = `var(${cssVar})`;
955
990
  cssVars[param] = cssVar;
@@ -958,6 +993,18 @@ async function loader(source) {
958
993
  const style = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
959
994
  if (!style)
960
995
  return null;
996
+ withDefault.forEach(([param, expr]) => {
997
+ const cssVar = cssVars[param];
998
+ const targetProp = cssVar ? findVarProp(style, cssVar) : undefined;
999
+ if (!targetProp)
1000
+ return;
1001
+ const literal = expr.type === 'StringLiteral' || expr.type === 'NumericLiteral'
1002
+ ? expr.value
1003
+ : undefined;
1004
+ if (literal === undefined)
1005
+ return;
1006
+ replaceVarValue(style, cssVar, `var(${cssVar}, ${(0, zss_engine_1.applyCssValue)(literal, (0, zss_engine_1.camelToKebabCase)(targetProp))})`);
1007
+ });
961
1008
  const vars = [];
962
1009
  runtime.forEach(({ param, source }) => {
963
1010
  const cssVar = cssVars[param];
@@ -1195,13 +1242,18 @@ async function loader(source) {
1195
1242
  : expr.property.value;
1196
1243
  const source = utils_1.t.isIdentifier(expr) ? varName : getSource(expr);
1197
1244
  markStylePropApplied(varName, expr);
1198
- let possibilities;
1199
- for (const key of Object.keys(scannedTables.componentPropsTable || {})) {
1200
- if (!key.startsWith(`${resourcePath}-`))
1201
- continue;
1202
- if (scannedTables.componentPropsTable?.[key]?.[varName]) {
1203
- possibilities = scannedTables.componentPropsTable[key][varName];
1204
- break;
1245
+ const owner = ownerComponentOf(expr);
1246
+ let possibilities = owner
1247
+ ? scannedTables.componentPropsTable?.[`${resourcePath}-${owner}`]?.[varName]
1248
+ : undefined;
1249
+ if (!possibilities) {
1250
+ for (const key of Object.keys(scannedTables.componentPropsTable || {})) {
1251
+ if (!key.startsWith(`${resourcePath}-`))
1252
+ continue;
1253
+ if (scannedTables.componentPropsTable?.[key]?.[varName]) {
1254
+ possibilities = scannedTables.componentPropsTable[key][varName];
1255
+ break;
1256
+ }
1205
1257
  }
1206
1258
  }
1207
1259
  if (!possibilities || possibilities.length === 0)
@@ -1584,7 +1636,7 @@ async function loader(source) {
1584
1636
  });
1585
1637
  if (Object.keys(lookupMap).length > 0) {
1586
1638
  const entries = Object.entries(lookupMap)
1587
- .map(([k, v]) => `"${k}":"${v}"`)
1639
+ .map(([k, v]) => `${JSON.stringify(k)}:${JSON.stringify(v)}`)
1588
1640
  .join(',');
1589
1641
  classParts.push(`({${entries}}[${commonTestExpr}] || "")`);
1590
1642
  }
@@ -2072,9 +2124,8 @@ async function loader(source) {
2072
2124
  throwCompilationError(`Plumeria: css.use(${getSource(expr)}) does not support dynamic function keys.`, expr);
2073
2125
  }
2074
2126
  }
2075
- const { classParts, isOptimizable, baseStyle } = buildClassParts(args);
2076
- if (isOptimizable &&
2077
- (args.length > 0 || Object.keys(baseStyle).length > 0)) {
2127
+ const { classParts, isOptimizable } = buildClassParts(args);
2128
+ if (isOptimizable) {
2078
2129
  const replacement = classParts.length > 0 ? classParts.join(' + " " + ') : '""';
2079
2130
  replacements.push({
2080
2131
  start: node.span.start - baseByteOffset,
@@ -2082,6 +2133,9 @@ async function loader(source) {
2082
2133
  content: replacement,
2083
2134
  });
2084
2135
  }
2136
+ else {
2137
+ throwCompilationError(`Plumeria: Dynamic or unresolvable style object "${getSource(node)}" is not supported.`, node);
2138
+ }
2085
2139
  },
2086
2140
  });
2087
2141
  for (const { name, node } of components) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/turbopack-loader",
3
- "version": "18.2.25",
3
+ "version": "18.2.26",
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.2.25",
26
- "@plumeria/utils": "^18.2.25"
25
+ "@plumeria/compiler": "^18.2.26",
26
+ "@plumeria/utils": "^18.2.26"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@swc/core": "1.15.47",