@plumeria/utils 18.2.12 → 18.2.13

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type { CSSObject, StaticTable, KeyframesHashTable, ViewTransitionHashTable, CreateHashTable, VariantsHashTable, CreateThemeHashTable, CreateStaticHashTable, } from './types';
1
+ export type { CSSObject, StaticTable, KeyframesHashTable, ViewTransitionHashTable, CreateHashTable, CreateFunctionTable, VariantsHashTable, CreateThemeHashTable, CreateStaticHashTable, } from './types';
2
2
  export { objectExpressionToObject, collectLocalConsts, traverse, t, getRootIdentifier, extractOndemandStyles, deepMerge, scanAll, getFileDependencies, resolveExport, resolveComponentKey, } from './parser';
3
3
  export type { StyleRecord } from './create';
4
4
  export { getStyleRecords } from './create';
package/dist/parser.js CHANGED
@@ -358,6 +358,202 @@ function collectLocalConsts(ast) {
358
358
  }
359
359
  return localConsts;
360
360
  }
361
+ function paramNames(func) {
362
+ const names = new Set();
363
+ const add = (pattern) => {
364
+ if (!pattern)
365
+ return;
366
+ if (exports.t.isIdentifier(pattern)) {
367
+ names.add(pattern.value);
368
+ }
369
+ else if (pattern.type === 'ObjectPattern') {
370
+ for (const item of pattern.properties ?? []) {
371
+ if (item.type === 'AssignmentPatternProperty')
372
+ add(item.key);
373
+ else if (item.type === 'KeyValuePatternProperty')
374
+ add(item.value);
375
+ else if (item.type === 'RestElement')
376
+ add(item.argument);
377
+ }
378
+ }
379
+ else if (pattern.type === 'ArrayPattern') {
380
+ (pattern.elements ?? []).forEach(add);
381
+ }
382
+ else if (pattern.type === 'AssignmentPattern') {
383
+ add(pattern.left);
384
+ }
385
+ else if (pattern.type === 'RestElement') {
386
+ add(pattern.argument);
387
+ }
388
+ };
389
+ func.params.forEach((param) => add(param?.pat ?? param));
390
+ return names;
391
+ }
392
+ function readsParams(node, params) {
393
+ if (Array.isArray(node))
394
+ return node.some((item) => readsParams(item, params));
395
+ if (!node || typeof node !== 'object')
396
+ return false;
397
+ if (node.type === 'Identifier')
398
+ return params.has(node.value);
399
+ if (node.type === 'KeyValueProperty')
400
+ return readsParams(node.value, params);
401
+ if (node.type === 'MemberExpression')
402
+ return (readsParams(node.object, params) ||
403
+ (node.property?.type === 'Computed' && readsParams(node.property, params)));
404
+ return Object.entries(node).some(([key, child]) => key !== 'span' && readsParams(child, params));
405
+ }
406
+ function literalFromValue(value, span) {
407
+ if (typeof value === 'string')
408
+ return { type: 'StringLiteral', span, value };
409
+ if (typeof value === 'number')
410
+ return { type: 'NumericLiteral', span, value };
411
+ if (typeof value === 'boolean')
412
+ return { type: 'BooleanLiteral', span, value };
413
+ if (typeof value !== 'object' || value === null)
414
+ return undefined;
415
+ const properties = [];
416
+ for (const [key, entry] of Object.entries(value)) {
417
+ const node = literalFromValue(entry, span);
418
+ if (!node)
419
+ return undefined;
420
+ properties.push({
421
+ type: 'KeyValueProperty',
422
+ key: { type: 'StringLiteral', span, value: key },
423
+ value: node,
424
+ });
425
+ }
426
+ return { type: 'ObjectExpression', span, properties };
427
+ }
428
+ function inlineOuterScope(objExpression, resolveProperty) {
429
+ const rewriteProperty = (prop, params) => {
430
+ if (prop.type === 'KeyValueProperty') {
431
+ const val = prop.value;
432
+ if (val.type === 'ObjectExpression') {
433
+ const rewritten = rewriteObject(val, params);
434
+ return rewritten === val ? prop : { ...prop, value: rewritten };
435
+ }
436
+ if (exports.t.isStringLiteral(val) ||
437
+ exports.t.isNumericLiteral(val) ||
438
+ exports.t.isBooleanLiteral(val))
439
+ return prop;
440
+ }
441
+ else if (prop.type !== 'SpreadElement' && !exports.t.isIdentifier(prop)) {
442
+ return prop;
443
+ }
444
+ if (readsParams(prop, params)) {
445
+ if (prop.type !== 'KeyValueProperty')
446
+ return prop;
447
+ const value = rewriteExpression(prop.value, params);
448
+ return value === prop.value ? prop : { ...prop, value };
449
+ }
450
+ const entries = Object.entries(resolveProperty(prop));
451
+ if (entries.length === 0)
452
+ return prop;
453
+ const span = (prop.value ?? prop).span;
454
+ if (prop.type === 'KeyValueProperty') {
455
+ const value = literalFromValue(entries[0][1], span);
456
+ return value ? { ...prop, value } : prop;
457
+ }
458
+ const properties = [];
459
+ for (const [key, entry] of entries) {
460
+ const value = literalFromValue(entry, span);
461
+ if (!value)
462
+ return prop;
463
+ properties.push({
464
+ type: 'KeyValueProperty',
465
+ key: { type: 'StringLiteral', span, value: key },
466
+ value,
467
+ });
468
+ }
469
+ return properties;
470
+ };
471
+ const rewriteExpression = (node, params) => {
472
+ if (!node || typeof node !== 'object')
473
+ return node;
474
+ if (!readsParams(node, params)) {
475
+ const entries = Object.entries(resolveProperty({
476
+ type: 'KeyValueProperty',
477
+ key: { type: 'Identifier', span: node.span, value: 'value' },
478
+ value: node,
479
+ }));
480
+ if (entries.length !== 1)
481
+ return node;
482
+ return literalFromValue(entries[0][1], node.span) ?? node;
483
+ }
484
+ if (node.type === 'ObjectExpression')
485
+ return rewriteObject(node, params);
486
+ if (node.type === 'ParenthesisExpression') {
487
+ const inner = rewriteExpression(node.expression, params);
488
+ return inner === node.expression ? node : { ...node, expression: inner };
489
+ }
490
+ if (node.type === 'TemplateLiteral') {
491
+ let changed = false;
492
+ const expressions = node.expressions.map((expr) => {
493
+ const rewritten = rewriteExpression(expr, params);
494
+ if (rewritten !== expr)
495
+ changed = true;
496
+ return rewritten;
497
+ });
498
+ return changed ? { ...node, expressions } : node;
499
+ }
500
+ if (node.type === 'BinaryExpression') {
501
+ const left = rewriteExpression(node.left, params);
502
+ const right = rewriteExpression(node.right, params);
503
+ return left === node.left && right === node.right
504
+ ? node
505
+ : { ...node, left, right };
506
+ }
507
+ return node;
508
+ };
509
+ const rewriteObject = (node, params) => {
510
+ let changed = false;
511
+ const properties = [];
512
+ for (const prop of node.properties) {
513
+ const rewritten = rewriteProperty(prop, params);
514
+ if (rewritten !== prop)
515
+ changed = true;
516
+ if (Array.isArray(rewritten))
517
+ properties.push(...rewritten);
518
+ else
519
+ properties.push(rewritten);
520
+ }
521
+ return changed ? { ...node, properties } : node;
522
+ };
523
+ const rewriteBody = (node, params) => {
524
+ if (node?.type === 'ParenthesisExpression') {
525
+ const inner = rewriteBody(node.expression, params);
526
+ return inner === node.expression ? node : { ...node, expression: inner };
527
+ }
528
+ if (node?.type === 'BlockStatement') {
529
+ const first = node.stmts?.[0];
530
+ if (first?.type !== 'ReturnStatement' || !first.argument)
531
+ return node;
532
+ const inner = rewriteBody(first.argument, params);
533
+ if (inner === first.argument)
534
+ return node;
535
+ const stmts = [{ ...first, argument: inner }, ...node.stmts.slice(1)];
536
+ return { ...node, stmts };
537
+ }
538
+ if (node?.type === 'ObjectExpression')
539
+ return rewriteObject(node, params);
540
+ return node;
541
+ };
542
+ let changed = false;
543
+ const properties = objExpression.properties.map((prop) => {
544
+ if (prop.type !== 'KeyValueProperty' ||
545
+ (prop.value.type !== 'ArrowFunctionExpression' &&
546
+ prop.value.type !== 'FunctionExpression'))
547
+ return prop;
548
+ const func = prop.value;
549
+ const body = rewriteBody(func.body, paramNames(func));
550
+ if (body === func.body)
551
+ return prop;
552
+ changed = true;
553
+ return { ...prop, value: { ...func, body } };
554
+ });
555
+ return changed ? { ...objExpression, properties } : objExpression;
556
+ }
361
557
  function getPropertyKey(node, staticTable, keyframesHashTable, viewTransitionHashTable, createThemeHashTable, createThemeObjectTable, createStaticHashTable, createStaticObjectTable) {
362
558
  if (exports.t.isIdentifier(node)) {
363
559
  return node.value;
@@ -714,6 +910,7 @@ const globalAgregatedTables = {
714
910
  createThemeSelectorTable: {},
715
911
  createHashTable: {},
716
912
  createObjectTable: {},
913
+ createFunctionTable: {},
717
914
  createAtomicMapTable: {},
718
915
  variantsHashTable: {},
719
916
  variantsObjectTable: {},
@@ -765,6 +962,7 @@ function stripFileContributions(filePath, cached) {
765
962
  }
766
963
  for (const key of Object.keys(cached.createHashTable)) {
767
964
  delete localTables.createHashTable[`${filePath}-${key}`];
965
+ delete localTables.createFunctionTable[`${filePath}-${key}`];
768
966
  }
769
967
  for (const key of Object.keys(cached.variantsHashTable)) {
770
968
  delete localTables.variantsHashTable[`${filePath}-${key}`];
@@ -921,6 +1119,7 @@ function scanAll() {
921
1119
  const isFirstPass = passNumber === 1;
922
1120
  for (const { filePath, ast, mtimeMs } of parsedFiles) {
923
1121
  try {
1122
+ let localConsts;
924
1123
  const localDependencies = new Set();
925
1124
  const localStaticTable = {};
926
1125
  const localKeyframesHashTable = {};
@@ -1098,6 +1297,10 @@ function scanAll() {
1098
1297
  }
1099
1298
  return undefined;
1100
1299
  };
1300
+ const scopedStaticTable = {
1301
+ ...(localConsts ??= collectLocalConsts(ast)),
1302
+ ...localStaticTable,
1303
+ };
1101
1304
  const isPassOneMethod = method === 'createStatic' ||
1102
1305
  method === 'createTheme' ||
1103
1306
  method === 'keyframes' ||
@@ -1107,7 +1310,7 @@ function scanAll() {
1107
1310
  const objExpression = isCreateTheme
1108
1311
  ? init.arguments[1].expression
1109
1312
  : init.arguments[0].expression;
1110
- const obj = objectExpressionToObject(objExpression, localStaticTable, localKeyframesHashTable, localViewTransitionHashTable, localCreateThemeHashTable, localCreateThemeObjectTable, localCreateHashTable, localCreateStaticHashTable, localCreateStaticObjectTable, localVariantsHashTable, resolveVariable);
1313
+ const obj = objectExpressionToObject(objExpression, scopedStaticTable, localKeyframesHashTable, localViewTransitionHashTable, localCreateThemeHashTable, localCreateThemeObjectTable, localCreateHashTable, localCreateStaticHashTable, localCreateStaticObjectTable, localVariantsHashTable, resolveVariable);
1111
1314
  const uniqueKey = `${filePath}-${name}`;
1112
1315
  if (method === 'createStatic') {
1113
1316
  localStaticTable[name] = obj;
@@ -1167,6 +1370,15 @@ function scanAll() {
1167
1370
  localTables.createObjectTable[hash] = obj;
1168
1371
  localCreateObjectTable[hash] = obj;
1169
1372
  registerObjectOwner('createObjectTable', hash, filePath);
1373
+ const hasFunctionKey = objExpression.properties.some((prop) => prop.type === 'KeyValueProperty' &&
1374
+ (prop.value.type === 'ArrowFunctionExpression' ||
1375
+ prop.value.type === 'FunctionExpression'));
1376
+ if (hasFunctionKey)
1377
+ localTables.createFunctionTable[uniqueKey] =
1378
+ inlineOuterScope(objExpression, (property) => objectExpressionToObject({
1379
+ ...objExpression,
1380
+ properties: [property],
1381
+ }, scopedStaticTable, localKeyframesHashTable, localViewTransitionHashTable, localCreateThemeHashTable, localCreateThemeObjectTable, localCreateHashTable, localCreateStaticHashTable, localCreateStaticObjectTable, localVariantsHashTable, resolveVariable));
1170
1382
  const hashMap = {};
1171
1383
  Object.entries(obj).forEach(([key, style]) => {
1172
1384
  const records = (0, create_1.getStyleRecords)(style);
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CSSProperties } from 'zss-engine';
2
+ import type { ObjectExpression } from '@swc/core';
2
3
  type CSSPrimitive = string | number | boolean;
3
4
  export type CSSValue = CSSPrimitive | CSSObject;
4
5
  export type CSSObject = {
@@ -12,6 +13,7 @@ export type ViewTransitionObjectTable = Record<string, CSSObject>;
12
13
  export type CreateThemeObjectTable = Record<string, CSSObject>;
13
14
  export type CreateHashTable = Record<string, string>;
14
15
  export type CreateObjectTable = Record<string, CSSObject>;
16
+ export type CreateFunctionTable = Record<string, ObjectExpression>;
15
17
  export type VariantsHashTable = Record<string, string>;
16
18
  export type VariantsObjectTable = Record<string, CSSObject>;
17
19
  export type CreateAtomicMapTable = Record<string, Record<string, Record<string, string>>>;
@@ -37,6 +39,7 @@ export interface Tables {
37
39
  createThemeObjectTable: CreateThemeObjectTable;
38
40
  createHashTable: CreateHashTable;
39
41
  createObjectTable: CreateObjectTable;
42
+ createFunctionTable: CreateFunctionTable;
40
43
  createAtomicMapTable: CreateAtomicMapTable;
41
44
  createStaticHashTable: CreateStaticHashTable;
42
45
  createStaticObjectTable: CreateStaticObjectTable;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/utils",
3
- "version": "18.2.12",
3
+ "version": "18.2.13",
4
4
  "description": "Plumeria Utils",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",