@plumeria/utils 21.2.1 → 21.2.3

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.
package/dist/compile.js CHANGED
@@ -828,6 +828,7 @@ function compileCSS(options) {
828
828
  ...argExpr,
829
829
  properties: argExpr.properties.filter((prop) => prop.type === 'Identifier' ||
830
830
  (prop.type === 'KeyValueProperty' &&
831
+ prop.key.type !== 'Computed' &&
831
832
  isStaticArgValue(prop.value))),
832
833
  }, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, undefined, ctx.mergedVarTypeTable, ctx.mergedPositionTryTable);
833
834
  if (func.named) {
package/dist/parser.js CHANGED
@@ -287,6 +287,10 @@ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTra
287
287
  if (typeof spreadVal === 'object' && spreadVal !== null) {
288
288
  Object.assign(obj, spreadVal);
289
289
  }
290
+ else if (spreadVal === null &&
291
+ !exports.t.isNullLiteral(unwrapExpression(arg))) {
292
+ throw new Error(`[plumeria] Cannot spread ${(0, exports.expressionLabel)(arg)} at build time. Spread a const object, or pass a runtime value through the function form, css.create({ name: (value) => ({ ... }) }).`);
293
+ }
290
294
  return;
291
295
  }
292
296
  if (exports.t.isIdentifier(prop) && !exports.t.isObjectProperty(prop)) {
@@ -298,13 +302,16 @@ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTra
298
302
  return;
299
303
  }
300
304
  }
301
- if (staticTable[key] !== undefined) {
302
- obj[key] = staticTable[key];
305
+ try {
306
+ obj[key] = evaluateExpression(prop, staticTable, keyframesHashTable, viewTransitionHashTable, varTypeHashTable, positionTryHashTable, createThemeHashTable, createThemeObjectTable, createStaticHashTable, createStaticObjectTable);
307
+ }
308
+ catch {
303
309
  }
304
310
  return;
305
311
  }
306
- if (!exports.t.isObjectProperty(prop))
307
- return;
312
+ if (!exports.t.isObjectProperty(prop)) {
313
+ throw new Error(`[plumeria] Cannot read the method or accessor "${prop.key?.value ?? ''}" at build time. Write it as a plain property.`);
314
+ }
308
315
  const rawKey = getPropertyKey(prop.key, staticTable, keyframesHashTable, viewTransitionHashTable, varTypeHashTable, positionTryHashTable, createThemeHashTable, createThemeObjectTable, createStaticHashTable, createStaticObjectTable);
309
316
  if (!rawKey)
310
317
  return;
@@ -358,12 +365,12 @@ function objectExpressionToObject(node, staticTable, keyframesHashTable, viewTra
358
365
  obj[key] = 'cr-' + resolvedCreate;
359
366
  return;
360
367
  }
361
- const resolvedTheme = resolveCreateThemeTableMemberExpressionByNode(val, createThemeHashTable, createThemeObjectTable);
368
+ const resolvedTheme = resolveCreateThemeTableMemberExpressionByNode(val, staticTable, createThemeHashTable, createThemeObjectTable);
362
369
  if (resolvedTheme !== undefined) {
363
370
  obj[key] = resolvedTheme;
364
371
  return;
365
372
  }
366
- const resolvedStatic = resolveCreateStaticTableMemberExpression(val, createStaticHashTable, createStaticObjectTable);
373
+ const resolvedStatic = resolveCreateStaticTableMemberExpression(val, staticTable, createStaticHashTable, createStaticObjectTable);
367
374
  if (resolvedStatic !== undefined) {
368
375
  obj[key] = resolvedStatic;
369
376
  return;
@@ -719,10 +726,10 @@ function getPropertyKey(node, staticTable, keyframesHashTable, viewTransitionHas
719
726
  const result = resolveStaticTableMemberExpression(expr, staticTable);
720
727
  if (typeof result === 'string')
721
728
  return result;
722
- const staticResult = resolveCreateStaticTableMemberExpression(expr, createStaticHashTable, createStaticObjectTable);
729
+ const staticResult = resolveCreateStaticTableMemberExpression(expr, staticTable, createStaticHashTable, createStaticObjectTable);
723
730
  if (typeof staticResult === 'string')
724
731
  return staticResult;
725
- const themeResult = resolveCreateThemeTableMemberExpressionByNode(expr, createThemeHashTable, createThemeObjectTable);
732
+ const themeResult = resolveCreateThemeTableMemberExpressionByNode(expr, staticTable, createThemeHashTable, createThemeObjectTable);
726
733
  if (typeof themeResult === 'string')
727
734
  return themeResult;
728
735
  }
@@ -732,7 +739,7 @@ function getPropertyKey(node, staticTable, keyframesHashTable, viewTransitionHas
732
739
  if (exports.t.isBinaryExpression(expr)) {
733
740
  return evaluateBinaryExpression(expr, staticTable, keyframesHashTable, viewTransitionHashTable, varTypeHashTable, positionTryHashTable, createThemeHashTable, createThemeObjectTable, createStaticHashTable, createStaticObjectTable);
734
741
  }
735
- return '';
742
+ throw new Error(`[plumeria] Cannot inline the computed key [${(0, exports.expressionLabel)(expr)}] at build time. Declare it with css.createStatic, such as const breakpoints = css.createStatic({ sm: '...' }), and read it as [breakpoints.sm]. A definition in a package needs a plumeria export condition.`);
736
743
  }
737
744
  if (exports.t.isTemplateLiteral(node)) {
738
745
  return evaluateTemplateLiteral(node, staticTable, keyframesHashTable, viewTransitionHashTable, varTypeHashTable, positionTryHashTable, createThemeHashTable, createThemeObjectTable, createStaticHashTable, createStaticObjectTable);
@@ -905,11 +912,11 @@ function evaluateExpression(node, staticTable, keyframesHashTable, viewTransitio
905
912
  if (resolved !== undefined) {
906
913
  return resolved;
907
914
  }
908
- const resolvedTheme = resolveCreateThemeTableMemberExpressionByNode(node, createThemeHashTable, createThemeObjectTable);
915
+ const resolvedTheme = resolveCreateThemeTableMemberExpressionByNode(node, staticTable, createThemeHashTable, createThemeObjectTable);
909
916
  if (resolvedTheme !== undefined) {
910
917
  return resolvedTheme;
911
918
  }
912
- const resolvedStatic = resolveCreateStaticTableMemberExpression(node, createStaticHashTable, createStaticObjectTable);
919
+ const resolvedStatic = resolveCreateStaticTableMemberExpression(node, staticTable, createStaticHashTable, createStaticObjectTable);
913
920
  if (resolvedStatic !== undefined) {
914
921
  return resolvedStatic;
915
922
  }
@@ -965,26 +972,30 @@ function resolveCreateTableMemberExpression(node, createHashTable) {
965
972
  return createHashTable[node.value];
966
973
  }
967
974
  }
975
+ function memberKey(node, staticTable) {
976
+ const property = node.property;
977
+ if (exports.t.isIdentifier(property))
978
+ return property.value;
979
+ if (property.type !== 'Computed')
980
+ return undefined;
981
+ const key = unwrapExpression(property.expression);
982
+ const value = exports.t.isStringLiteral(key) || exports.t.isNumericLiteral(key)
983
+ ? key.value
984
+ : exports.t.isIdentifier(key)
985
+ ? staticTable[key.value]
986
+ : undefined;
987
+ return typeof value === 'string' || typeof value === 'number'
988
+ ? String(value)
989
+ : undefined;
990
+ }
968
991
  function resolveStaticTableMemberExpression(node, staticTable) {
969
992
  const keys = [];
970
993
  let current = node;
971
994
  while (exports.t.isMemberExpression(current)) {
972
- const property = current.property;
973
- const key = exports.t.isIdentifier(property)
974
- ? property.value
975
- : property.type === 'Computed'
976
- ? unwrapExpression(property.expression)
977
- : undefined;
978
- const value = typeof key === 'string'
979
- ? key
980
- : exports.t.isStringLiteral(key) || exports.t.isNumericLiteral(key)
981
- ? String(key.value)
982
- : exports.t.isIdentifier(key)
983
- ? staticTable[key.value]
984
- : undefined;
985
- if (typeof value !== 'string' && typeof value !== 'number')
995
+ const key = memberKey(current, staticTable);
996
+ if (key === undefined)
986
997
  return undefined;
987
- keys.unshift(String(value));
998
+ keys.unshift(key);
988
999
  current = unwrapExpression(current.object);
989
1000
  }
990
1001
  if (!exports.t.isIdentifier(current))
@@ -997,21 +1008,14 @@ function resolveStaticTableMemberExpression(node, staticTable) {
997
1008
  }
998
1009
  return value;
999
1010
  }
1000
- function resolveCreateThemeTableMemberExpressionByNode(node, createThemeHashTable, createThemeObjectTable) {
1011
+ function resolveCreateThemeTableMemberExpressionByNode(node, staticTable, createThemeHashTable, createThemeObjectTable) {
1001
1012
  if (exports.t.isMemberExpression(node) && exports.t.isIdentifier(node.object)) {
1002
1013
  const varName = node.object.value;
1003
1014
  const hash = createThemeHashTable[varName];
1004
1015
  if (hash) {
1005
1016
  const themeObj = createThemeObjectTable[hash];
1006
1017
  if (themeObj) {
1007
- let key;
1008
- if (exports.t.isIdentifier(node.property)) {
1009
- key = node.property.value;
1010
- }
1011
- else if (node.property.type === 'Computed' &&
1012
- exports.t.isStringLiteral(node.property.expression)) {
1013
- key = node.property.expression.value;
1014
- }
1018
+ const key = memberKey(node, staticTable);
1015
1019
  if (key && themeObj[key] !== undefined) {
1016
1020
  const value = themeObj[key];
1017
1021
  const atomicHash = (0, zss_engine_1.genBase36Hash)({ _theme: hash, [key]: value }, 1, 8);
@@ -1023,21 +1027,14 @@ function resolveCreateThemeTableMemberExpressionByNode(node, createThemeHashTabl
1023
1027
  }
1024
1028
  return undefined;
1025
1029
  }
1026
- function resolveCreateStaticTableMemberExpression(node, createStaticHashTable, createStaticObjectTable) {
1030
+ function resolveCreateStaticTableMemberExpression(node, staticTable, createStaticHashTable, createStaticObjectTable) {
1027
1031
  if (exports.t.isMemberExpression(node) && exports.t.isIdentifier(node.object)) {
1028
1032
  const varName = node.object.value;
1029
1033
  const hash = createStaticHashTable[varName];
1030
1034
  if (hash) {
1031
1035
  const staticObj = createStaticObjectTable[hash];
1032
1036
  if (staticObj) {
1033
- let key;
1034
- if (exports.t.isIdentifier(node.property)) {
1035
- key = node.property.value;
1036
- }
1037
- else if (node.property.type === 'Computed' &&
1038
- exports.t.isStringLiteral(node.property.expression)) {
1039
- key = node.property.expression.value;
1040
- }
1037
+ const key = memberKey(node, staticTable);
1041
1038
  if (key && staticObj[key] !== undefined) {
1042
1039
  return staticObj[key];
1043
1040
  }
package/dist/transform.js CHANGED
@@ -1268,6 +1268,7 @@ const transformExpanded = async (env, scannedTables, namespaces) => {
1268
1268
  ...argExpr,
1269
1269
  properties: argExpr.properties.filter((prop) => prop.type === 'Identifier' ||
1270
1270
  (prop.type === 'KeyValueProperty' &&
1271
+ prop.key.type !== 'Computed' &&
1271
1272
  isStaticArgValue(prop.value))),
1272
1273
  }, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, undefined, mergedVarTypeTable, mergedPositionTryTable);
1273
1274
  if (func.named) {
@@ -9,6 +9,11 @@ const keyValue = (key) => {
9
9
  ? value
10
10
  : undefined;
11
11
  };
12
+ const asKeyValue = (prop) => prop.type === 'Identifier'
13
+ ? { key: prop, value: prop }
14
+ : prop.type === 'KeyValueProperty'
15
+ ? prop
16
+ : null;
12
17
  const unresolvableValue = (key, node, context) => new Error(`[plumeria] Cannot resolve the value of "${key}" at build time (${context?.sourceOf?.(node) ?? node.type}). ` +
13
18
  `Values in css.create must be statically known; pass a runtime value through the ` +
14
19
  `function form, css.create({ name: (value) => ({ ... }) }).`);
@@ -23,7 +28,8 @@ const overwrittenLater = (properties, index, key, context) => {
23
28
  return true;
24
29
  continue;
25
30
  }
26
- if (later.type === 'KeyValueProperty' && keyValue(later.key) === key)
31
+ const entry = asKeyValue(later);
32
+ if (entry && keyValue(entry.key) === key)
27
33
  return true;
28
34
  }
29
35
  return false;
@@ -86,14 +92,14 @@ const assertStyleFunction = (func, context) => {
86
92
  const walk = (object) => {
87
93
  const properties = object.properties ?? [];
88
94
  for (let index = 0; index < properties.length; index++) {
89
- const prop = properties[index];
90
- if (prop.type !== 'KeyValueProperty')
95
+ const entry = asKeyValue(properties[index]);
96
+ if (!entry)
91
97
  continue;
92
- const key = keyValue(prop.key);
98
+ const key = keyValue(entry.key);
93
99
  if ((typeof key === 'string' || typeof key === 'number') &&
94
100
  overwrittenLater(properties, index, key, context))
95
101
  continue;
96
- const value = prop.value;
102
+ const value = entry.value;
97
103
  if (!value)
98
104
  continue;
99
105
  if (value.type === 'ObjectExpression') {
@@ -123,9 +129,10 @@ const keysSuppliedAfter = (properties, index, context) => {
123
129
  keys.add(name);
124
130
  continue;
125
131
  }
126
- if (later.type !== 'KeyValueProperty')
132
+ const entry = asKeyValue(later);
133
+ if (!entry)
127
134
  continue;
128
- const key = keyValue(later.key);
135
+ const key = keyValue(entry.key);
129
136
  if (typeof key !== 'string' && typeof key !== 'number')
130
137
  return null;
131
138
  keys.add(String(key));
@@ -146,16 +153,17 @@ const assertNoDroppedValues = (node, resolved, context, ignore) => {
146
153
  (0, exports.assertNoDroppedValues)(provided.source, provided.values, context, replaced);
147
154
  continue;
148
155
  }
149
- if (prop.type !== 'KeyValueProperty')
156
+ const entry = asKeyValue(prop);
157
+ if (!entry)
150
158
  continue;
151
- const key = keyValue(prop.key);
159
+ const key = keyValue(entry.key);
152
160
  if (typeof key !== 'string' && typeof key !== 'number')
153
161
  continue;
154
162
  if (ignore?.has(String(key)))
155
163
  continue;
156
164
  if (overwrittenLater(node.properties, index, key, context))
157
165
  continue;
158
- const value = prop.value;
166
+ const value = entry.value;
159
167
  if (!value)
160
168
  continue;
161
169
  if (value.type === 'ArrowFunctionExpression' ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/utils",
3
- "version": "21.2.1",
3
+ "version": "21.2.3",
4
4
  "description": "Plumeria Utils",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",