@plumeria/unplugin 11.1.2 → 11.2.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.
package/dist/core.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createFilter } from '@rollup/pluginutils';
2
2
  import { parseSync } from '@swc/core';
3
3
  import * as path from 'path';
4
- import { applyCssValue, genBase36Hash, exceptionCamelCase } from 'zss-engine';
4
+ import { applyCssValue, genBase36Hash, exceptionCamelCase, camelToKebabCase, } from 'zss-engine';
5
5
  import { traverse, getStyleRecords, collectLocalConsts, objectExpressionToObject, t, extractOndemandStyles, deepMerge, scanAll, resolveImportPath, processVariants, getLeadingCommentLength, optimizer, } from '@plumeria/utils';
6
6
  export const TARGET_EXTENSIONS = ['ts', 'tsx', 'js', 'jsx'];
7
7
  export const EXTENSION_PATTERN = /\.(ts|tsx|js|jsx)$/;
@@ -254,6 +254,325 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
254
254
  const processedDecls = new Set();
255
255
  const idSpans = new Set();
256
256
  const excludedSpans = new Set();
257
+ const checkVariantAssignment = (decl) => {
258
+ const init = decl.init;
259
+ if (init && t.isCallExpression(init) && t.isIdentifier(init.callee)) {
260
+ const varName = init.callee.value;
261
+ if ((localCreateStyles[varName] &&
262
+ localCreateStyles[varName].type === 'variant') ||
263
+ mergedVariantsTable[varName]) {
264
+ throw new Error(`Plumeria: Assigning the return value of "css.variants" to a variable is not supported.\nPlease pass the variant function directly to "css.use". Found assignment to: ${t.isIdentifier(decl.id) ? decl.id.value : 'unknown'}`);
265
+ }
266
+ }
267
+ };
268
+ const registerStyle = (node, declSpan, isExported) => {
269
+ let propName;
270
+ const init = node.init;
271
+ if (t.isIdentifier(node.id) &&
272
+ init &&
273
+ t.isCallExpression(init) &&
274
+ init.arguments.length >= 1) {
275
+ const callee = init.callee;
276
+ if (t.isMemberExpression(callee) &&
277
+ t.isIdentifier(callee.object) &&
278
+ t.isIdentifier(callee.property)) {
279
+ const objectName = callee.object.value;
280
+ const propertyName = callee.property.value;
281
+ const alias = plumeriaAliases[objectName];
282
+ if (alias === 'NAMESPACE') {
283
+ propName = propertyName;
284
+ }
285
+ }
286
+ else if (t.isIdentifier(callee)) {
287
+ const calleeName = callee.value;
288
+ const originalName = plumeriaAliases[calleeName];
289
+ if (originalName) {
290
+ propName = originalName;
291
+ }
292
+ }
293
+ }
294
+ if (propName && init && t.isCallExpression(init)) {
295
+ if (propName === 'create' &&
296
+ t.isObjectExpression(init.arguments[0].expression)) {
297
+ const obj = objectExpressionToObject(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
298
+ if (obj) {
299
+ const hashMap = {};
300
+ Object.entries(obj).forEach(([key, style]) => {
301
+ if (typeof style !== 'object' || style === null)
302
+ return;
303
+ const records = getStyleRecords(style);
304
+ const atomMap = {};
305
+ records.forEach((r) => (atomMap[r.key] = r.hash));
306
+ hashMap[key] = atomMap;
307
+ });
308
+ const styleFunctions = {};
309
+ const objExpr = init.arguments[0].expression;
310
+ objExpr.properties.forEach((prop) => {
311
+ if (prop.type !== 'KeyValueProperty' ||
312
+ prop.key.type !== 'Identifier')
313
+ return;
314
+ const func = prop.value;
315
+ if (func.type !== 'ArrowFunctionExpression' &&
316
+ func.type !== 'FunctionExpression')
317
+ return;
318
+ const params = func.params.map((p) => {
319
+ if (t.isIdentifier(p))
320
+ return p.value;
321
+ if (typeof p === 'object' &&
322
+ p !== null &&
323
+ 'pat' in p &&
324
+ t.isIdentifier(p.pat))
325
+ return p.pat.value;
326
+ return 'arg';
327
+ });
328
+ let actualBody = func.body;
329
+ if (actualBody?.type === 'ParenthesisExpression')
330
+ actualBody = actualBody.expression;
331
+ if (actualBody?.type === 'BlockStatement') {
332
+ const first = actualBody.stmts?.[0];
333
+ if (first?.type === 'ReturnStatement')
334
+ actualBody = first.argument;
335
+ if (actualBody?.type === 'ParenthesisExpression')
336
+ actualBody = actualBody.expression;
337
+ }
338
+ if (actualBody && actualBody.type === 'ObjectExpression') {
339
+ styleFunctions[prop.key.value] = {
340
+ params,
341
+ body: actualBody,
342
+ };
343
+ }
344
+ });
345
+ if (t.isIdentifier(node.id)) {
346
+ idSpans.add(node.id.span.start);
347
+ }
348
+ if (t.isIdentifier(node.id)) {
349
+ localCreateStyles[node.id.value] = {
350
+ name: node.id.value,
351
+ type: 'create',
352
+ obj,
353
+ hashMap,
354
+ isExported,
355
+ initSpan: {
356
+ start: init.span.start - baseByteOffset,
357
+ end: init.span.end - baseByteOffset,
358
+ },
359
+ declSpan: {
360
+ start: declSpan.start - baseByteOffset,
361
+ end: declSpan.end - baseByteOffset,
362
+ },
363
+ functions: styleFunctions,
364
+ };
365
+ }
366
+ }
367
+ }
368
+ else if (propName === 'variants' &&
369
+ t.isObjectExpression(init.arguments[0].expression)) {
370
+ if (t.isIdentifier(node.id)) {
371
+ idSpans.add(node.id.span.start);
372
+ }
373
+ const obj = objectExpressionToObject(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable, (name) => {
374
+ if (localCreateStyles[name]) {
375
+ return localCreateStyles[name].obj;
376
+ }
377
+ if (mergedCreateTable[name]) {
378
+ const hash = mergedCreateTable[name];
379
+ if (scannedTables.createObjectTable[hash]) {
380
+ return scannedTables.createObjectTable[hash];
381
+ }
382
+ }
383
+ return undefined;
384
+ });
385
+ const { hashMap } = processVariants(obj);
386
+ if (t.isIdentifier(node.id)) {
387
+ localCreateStyles[node.id.value] = {
388
+ name: node.id.value,
389
+ type: 'variant',
390
+ obj,
391
+ hashMap,
392
+ isExported,
393
+ initSpan: {
394
+ start: init.span.start - baseByteOffset,
395
+ end: init.span.end - baseByteOffset,
396
+ },
397
+ declSpan: {
398
+ start: declSpan.start - baseByteOffset,
399
+ end: declSpan.end - baseByteOffset,
400
+ },
401
+ };
402
+ }
403
+ }
404
+ else if (propName === 'createTheme' &&
405
+ t.isObjectExpression(init.arguments[0].expression)) {
406
+ if (t.isIdentifier(node.id)) {
407
+ idSpans.add(node.id.span.start);
408
+ }
409
+ const obj = objectExpressionToObject(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
410
+ const hash = genBase36Hash(obj, 1, 8);
411
+ if (t.isIdentifier(node.id)) {
412
+ const uniqueKey = `${resourcePath}-${node.id.value}`;
413
+ scannedTables.createThemeHashTable[uniqueKey] = hash;
414
+ scannedTables.createThemeObjectTable[hash] = obj;
415
+ mergedCreateThemeHashTable[node.id.value] = hash;
416
+ const themeHashMap = {};
417
+ for (const [key] of Object.entries(obj)) {
418
+ const cssVarName = camelToKebabCase(key);
419
+ themeHashMap[key] = `var(--${hash}-${cssVarName})`;
420
+ }
421
+ localCreateStyles[node.id.value] = {
422
+ name: node.id.value,
423
+ type: 'constant',
424
+ obj,
425
+ hashMap: themeHashMap,
426
+ isExported,
427
+ initSpan: {
428
+ start: init.span.start - baseByteOffset,
429
+ end: init.span.end - baseByteOffset,
430
+ },
431
+ declSpan: {
432
+ start: declSpan.start - baseByteOffset,
433
+ end: declSpan.end - baseByteOffset,
434
+ },
435
+ };
436
+ }
437
+ }
438
+ }
439
+ };
440
+ traverse(ast, {
441
+ ImportDeclaration({ node }) {
442
+ if (node.source.value === '@plumeria/core') {
443
+ if (node.typeOnly)
444
+ return;
445
+ const typeOnlySpecs = node.specifiers.filter((s) => s.type === 'ImportSpecifier' && s.isTypeOnly);
446
+ if (typeOnlySpecs.length > 0) {
447
+ const names = typeOnlySpecs
448
+ .map((s) => {
449
+ if (s.type !== 'ImportSpecifier')
450
+ return;
451
+ const imported = s.imported
452
+ ? s.imported.value
453
+ : s.local.value;
454
+ const local = s.local.value;
455
+ return imported === local
456
+ ? imported
457
+ : `${imported} as ${local}`;
458
+ })
459
+ .join(', ');
460
+ replacements.push({
461
+ start: node.span.start - baseByteOffset,
462
+ end: node.span.end - baseByteOffset,
463
+ content: `import type { ${names} } from '@plumeria/core'`,
464
+ });
465
+ }
466
+ else {
467
+ replacements.push({
468
+ start: node.span.start - baseByteOffset,
469
+ end: node.span.end - baseByteOffset,
470
+ content: '',
471
+ });
472
+ }
473
+ }
474
+ node.specifiers.forEach((specifier) => {
475
+ if (specifier.local) {
476
+ excludedSpans.add(specifier.local.span.start);
477
+ }
478
+ if (specifier.type === 'ImportSpecifier' && specifier.imported) {
479
+ excludedSpans.add(specifier.imported.span.start);
480
+ }
481
+ });
482
+ },
483
+ ExportDeclaration({ node }) {
484
+ if (t.isVariableDeclaration(node.declaration)) {
485
+ processedDecls.add(node.declaration);
486
+ node.declaration.declarations.forEach((decl) => {
487
+ checkVariantAssignment(decl);
488
+ registerStyle(decl, node.span, true);
489
+ });
490
+ }
491
+ },
492
+ VariableDeclaration({ node }) {
493
+ if (processedDecls.has(node))
494
+ return;
495
+ node.declarations.forEach((decl) => {
496
+ checkVariantAssignment(decl);
497
+ registerStyle(decl, node.span, false);
498
+ });
499
+ },
500
+ CallExpression({ node }) {
501
+ const callee = node.callee;
502
+ let propName;
503
+ if (t.isMemberExpression(callee) &&
504
+ t.isIdentifier(callee.object) &&
505
+ t.isIdentifier(callee.property)) {
506
+ const objectName = callee.object.value;
507
+ const propertyName = callee.property.value;
508
+ const alias = plumeriaAliases[objectName];
509
+ if (alias === 'NAMESPACE') {
510
+ propName = propertyName;
511
+ }
512
+ }
513
+ else if (t.isIdentifier(callee)) {
514
+ const calleeName = callee.value;
515
+ const originalName = plumeriaAliases[calleeName];
516
+ if (originalName) {
517
+ propName = originalName;
518
+ }
519
+ }
520
+ if (propName) {
521
+ const args = node.arguments;
522
+ if (propName === 'keyframes') {
523
+ const expr = args[0].expression;
524
+ if (t.isObjectExpression(expr)) {
525
+ const obj = objectExpressionToObject(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
526
+ const hash = genBase36Hash(obj, 1, 8);
527
+ scannedTables.keyframesObjectTable[hash] = obj;
528
+ replacements.push({
529
+ start: node.span.start - baseByteOffset,
530
+ end: node.span.end - baseByteOffset,
531
+ content: JSON.stringify(`kf-${hash}`),
532
+ });
533
+ }
534
+ }
535
+ else if (propName === 'viewTransition' &&
536
+ args.length > 0 &&
537
+ t.isObjectExpression(args[0].expression)) {
538
+ const obj = objectExpressionToObject(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
539
+ const hash = genBase36Hash(obj, 1, 8);
540
+ scannedTables.viewTransitionObjectTable[hash] = obj;
541
+ replacements.push({
542
+ start: node.span.start - baseByteOffset,
543
+ end: node.span.end - baseByteOffset,
544
+ content: JSON.stringify(`vt-${hash}`),
545
+ });
546
+ }
547
+ else if ((propName === 'createTheme' || propName === 'createStatic') &&
548
+ args.length > 0 &&
549
+ t.isObjectExpression(args[0].expression)) {
550
+ const obj = objectExpressionToObject(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
551
+ const hash = genBase36Hash(obj, 1, 8);
552
+ if (propName === 'createTheme') {
553
+ scannedTables.createThemeObjectTable[hash] = obj;
554
+ }
555
+ else {
556
+ scannedTables.createStaticObjectTable[hash] = obj;
557
+ }
558
+ const prefix = propName === 'createTheme' ? 'tm-' : 'st-';
559
+ replacements.push({
560
+ start: node.span.start - baseByteOffset,
561
+ end: node.span.end - baseByteOffset,
562
+ content: JSON.stringify(`${prefix}${hash}`),
563
+ });
564
+ }
565
+ else if (propName === 'create' &&
566
+ args.length > 0 &&
567
+ t.isObjectExpression(args[0].expression)) {
568
+ const obj = objectExpressionToObject(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
569
+ const hash = genBase36Hash(obj, 1, 8);
570
+ scannedTables.createObjectTable[hash] = obj;
571
+ }
572
+ }
573
+ },
574
+ });
575
+ const jsxOpeningElementMap = new Map();
257
576
  const getSource = (node) => {
258
577
  const start = node.span.start - baseByteOffset;
259
578
  const end = node.span.end - baseByteOffset;
@@ -380,9 +699,9 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
380
699
  if (variantObj) {
381
700
  const callArgs = expr.arguments;
382
701
  if (callArgs.length === 1 && !callArgs[0].spread) {
383
- const innerArg = callArgs[0].expression;
384
- if (innerArg.type === 'ObjectExpression') {
385
- for (const prop of innerArg.properties) {
702
+ const arg = callArgs[0].expression;
703
+ if (arg.type === 'ObjectExpression') {
704
+ for (const prop of arg.properties) {
386
705
  let groupName;
387
706
  let valExpr;
388
707
  if (prop.type === 'KeyValueProperty' &&
@@ -423,16 +742,16 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
423
742
  }
424
743
  continue;
425
744
  }
426
- const argSource = getSource(innerArg);
427
- if (t.isStringLiteral(innerArg)) {
428
- if (variantObj[innerArg.value])
429
- baseStyle = deepMerge(baseStyle, variantObj[innerArg.value]);
745
+ const argSource = getSource(arg);
746
+ if (t.isStringLiteral(arg)) {
747
+ if (variantObj[arg.value])
748
+ baseStyle = deepMerge(baseStyle, variantObj[arg.value]);
430
749
  continue;
431
750
  }
432
751
  const currentGroupId = ++groupIdCounter;
433
752
  Object.entries(variantObj).forEach(([key, style]) => {
434
753
  conditionals.push({
435
- test: innerArg,
754
+ test: arg,
436
755
  testLHS: argSource,
437
756
  testString: `${argSource} === '${key}'`,
438
757
  truthy: style,
@@ -608,10 +927,12 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
608
927
  indepVarGroups[c.groupId].push(c);
609
928
  }
610
929
  });
611
- Object.values(indepVarGroups).forEach((opts) => {
612
- const commonTestExpr = opts[0].testLHS ?? opts[0].testString ?? getSource(opts[0].test);
930
+ Object.values(indepVarGroups).forEach((options) => {
931
+ const commonTestExpr = options[0].testLHS ??
932
+ options[0].testString ??
933
+ getSource(options[0].test);
613
934
  const lookupMap = {};
614
- opts.forEach((opt) => {
935
+ options.forEach((opt) => {
615
936
  if (opt.valueName && opt.truthy) {
616
937
  const className = processStyleRecords(opt.truthy)
617
938
  .map((r) => r.hash)
@@ -694,187 +1015,7 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
694
1015
  classParts.push(...dynamicClassParts);
695
1016
  return { classParts, isOptimizable, baseStyle };
696
1017
  };
697
- const registerStyle = (node, declSpan, isExported) => {
698
- let propName;
699
- const init = node.init;
700
- if (t.isIdentifier(node.id) &&
701
- init &&
702
- t.isCallExpression(init) &&
703
- init.arguments.length >= 1) {
704
- const callee = init.callee;
705
- if (t.isMemberExpression(callee) &&
706
- t.isIdentifier(callee.object) &&
707
- t.isIdentifier(callee.property)) {
708
- const objectName = callee.object.value;
709
- const propertyName = callee.property.value;
710
- const alias = plumeriaAliases[objectName];
711
- if (alias === 'NAMESPACE')
712
- propName = propertyName;
713
- }
714
- else if (t.isIdentifier(callee)) {
715
- const calleeName = callee.value;
716
- const originalName = plumeriaAliases[calleeName];
717
- if (originalName)
718
- propName = originalName;
719
- }
720
- }
721
- if (propName && init && t.isCallExpression(init)) {
722
- if (propName === 'create' &&
723
- t.isObjectExpression(init.arguments[0].expression)) {
724
- const obj = objectExpressionToObject(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
725
- if (obj) {
726
- const hashMap = {};
727
- Object.entries(obj).forEach(([key, style]) => {
728
- if (typeof style !== 'object' || style === null)
729
- return;
730
- const records = getStyleRecords(style);
731
- const atomMap = {};
732
- records.forEach((r) => (atomMap[r.key] = r.hash));
733
- hashMap[key] = atomMap;
734
- });
735
- const styleFunctions = {};
736
- const objExpr = init.arguments[0].expression;
737
- objExpr.properties.forEach((prop) => {
738
- if (prop.type !== 'KeyValueProperty' ||
739
- prop.key.type !== 'Identifier')
740
- return;
741
- const func = prop.value;
742
- if (func.type !== 'ArrowFunctionExpression' &&
743
- func.type !== 'FunctionExpression')
744
- return;
745
- const params = func.params.map((p) => {
746
- if (t.isIdentifier(p))
747
- return p.value;
748
- if (typeof p === 'object' &&
749
- p !== null &&
750
- 'pat' in p &&
751
- t.isIdentifier(p.pat))
752
- return p.pat.value;
753
- return 'arg';
754
- });
755
- let actualBody = func.body;
756
- if (actualBody?.type === 'ParenthesisExpression')
757
- actualBody = actualBody.expression;
758
- if (actualBody?.type === 'BlockStatement') {
759
- const first = actualBody.stmts?.[0];
760
- if (first?.type === 'ReturnStatement')
761
- actualBody = first.argument;
762
- if (actualBody?.type === 'ParenthesisExpression')
763
- actualBody = actualBody.expression;
764
- }
765
- if (actualBody && actualBody.type === 'ObjectExpression') {
766
- styleFunctions[prop.key.value] = {
767
- params,
768
- body: actualBody,
769
- };
770
- }
771
- });
772
- if (t.isIdentifier(node.id)) {
773
- idSpans.add(node.id.span.start);
774
- localCreateStyles[node.id.value] = {
775
- name: node.id.value,
776
- type: 'create',
777
- obj,
778
- hashMap,
779
- isExported,
780
- initSpan: {
781
- start: init.span.start - baseByteOffset,
782
- end: init.span.end - baseByteOffset,
783
- },
784
- declSpan: {
785
- start: declSpan.start - baseByteOffset,
786
- end: declSpan.end - baseByteOffset,
787
- },
788
- functions: styleFunctions,
789
- };
790
- }
791
- }
792
- }
793
- else if (propName === 'variants' &&
794
- t.isObjectExpression(init.arguments[0].expression)) {
795
- if (t.isIdentifier(node.id))
796
- idSpans.add(node.id.span.start);
797
- const obj = objectExpressionToObject(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable, (name) => {
798
- if (localCreateStyles[name])
799
- return localCreateStyles[name].obj;
800
- if (mergedCreateTable[name]) {
801
- const hash = mergedCreateTable[name];
802
- if (scannedTables.createObjectTable[hash])
803
- return scannedTables.createObjectTable[hash];
804
- }
805
- return undefined;
806
- });
807
- const { hashMap } = processVariants(obj);
808
- if (t.isIdentifier(node.id)) {
809
- localCreateStyles[node.id.value] = {
810
- name: node.id.value,
811
- type: 'variant',
812
- obj,
813
- hashMap,
814
- isExported,
815
- initSpan: {
816
- start: init.span.start - baseByteOffset,
817
- end: init.span.end - baseByteOffset,
818
- },
819
- declSpan: {
820
- start: declSpan.start - baseByteOffset,
821
- end: declSpan.end - baseByteOffset,
822
- },
823
- };
824
- }
825
- }
826
- else if (propName === 'createTheme' &&
827
- t.isObjectExpression(init.arguments[0].expression)) {
828
- if (t.isIdentifier(node.id))
829
- idSpans.add(node.id.span.start);
830
- const obj = objectExpressionToObject(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
831
- const hash = genBase36Hash(obj, 1, 8);
832
- if (t.isIdentifier(node.id)) {
833
- const uniqueKey = `${resourcePath}-${node.id.value}`;
834
- scannedTables.createThemeHashTable[uniqueKey] = hash;
835
- scannedTables.createThemeObjectTable[hash] = obj;
836
- localCreateStyles[node.id.value] = {
837
- name: node.id.value,
838
- type: 'constant',
839
- obj,
840
- hashMap: scannedTables.createAtomicMapTable[hash],
841
- isExported,
842
- initSpan: {
843
- start: init.span.start - baseByteOffset,
844
- end: init.span.end - baseByteOffset,
845
- },
846
- declSpan: {
847
- start: declSpan.start - baseByteOffset,
848
- end: declSpan.end - baseByteOffset,
849
- },
850
- };
851
- }
852
- }
853
- }
854
- };
855
- const jsxOpeningElementMap = new Map();
856
1018
  traverse(ast, {
857
- ImportDeclaration({ node }) {
858
- if (node.specifiers) {
859
- node.specifiers.forEach((specifier) => {
860
- if (specifier.local)
861
- excludedSpans.add(specifier.local.span.start);
862
- if (specifier.type === 'ImportSpecifier' && specifier.imported)
863
- excludedSpans.add(specifier.imported.span.start);
864
- });
865
- }
866
- },
867
- ExportDeclaration({ node }) {
868
- if (t.isVariableDeclaration(node.declaration)) {
869
- processedDecls.add(node.declaration);
870
- node.declaration.declarations.forEach((decl) => registerStyle(decl, node.span, true));
871
- }
872
- },
873
- VariableDeclaration({ node }) {
874
- if (processedDecls.has(node))
875
- return;
876
- node.declarations.forEach((decl) => registerStyle(decl, node.span, false));
877
- },
878
1019
  JSXOpeningElement({ node }) {
879
1020
  jsxOpeningElementMap.set(node.span.start, node.attributes);
880
1021
  },
@@ -884,42 +1025,49 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
884
1025
  const propName = node.property.value;
885
1026
  const uniqueKey = `${resourcePath}-${varName}`;
886
1027
  let hash = scannedTables.createHashTable[uniqueKey];
887
- if (!hash)
1028
+ if (!hash) {
888
1029
  hash = mergedCreateTable[varName];
1030
+ }
889
1031
  if (hash) {
890
1032
  let atomMap;
891
- if (scannedTables.createAtomicMapTable[hash])
1033
+ if (scannedTables.createAtomicMapTable[hash]) {
892
1034
  atomMap = scannedTables.createAtomicMapTable[hash][propName];
893
- if (atomMap)
1035
+ }
1036
+ if (atomMap) {
894
1037
  replacements.push({
895
1038
  start: node.span.start - baseByteOffset,
896
1039
  end: node.span.end - baseByteOffset,
897
1040
  content: `(${JSON.stringify(atomMap)})`,
898
1041
  });
1042
+ }
899
1043
  }
900
1044
  let themeHash = scannedTables.createThemeHashTable[uniqueKey];
901
- if (!themeHash)
1045
+ if (!themeHash) {
902
1046
  themeHash = mergedCreateThemeHashTable[varName];
1047
+ }
903
1048
  if (themeHash) {
904
1049
  const atomicMap = scannedTables.createAtomicMapTable[themeHash];
905
- if (atomicMap && atomicMap[propName])
1050
+ if (atomicMap && atomicMap && atomicMap[propName]) {
906
1051
  replacements.push({
907
1052
  start: node.span.start - baseByteOffset,
908
1053
  end: node.span.end - baseByteOffset,
909
1054
  content: `(${JSON.stringify(atomicMap[propName])})`,
910
1055
  });
1056
+ }
911
1057
  }
912
1058
  let staticHash = scannedTables.createStaticHashTable[uniqueKey];
913
- if (!staticHash)
1059
+ if (!staticHash) {
914
1060
  staticHash = mergedCreateStaticHashTable[varName];
1061
+ }
915
1062
  if (staticHash) {
916
1063
  const staticObj = scannedTables.createStaticObjectTable[staticHash];
917
- if (staticObj && staticObj[propName] !== undefined)
1064
+ if (staticObj && staticObj[propName] !== undefined) {
918
1065
  replacements.push({
919
1066
  start: node.span.start - baseByteOffset,
920
1067
  end: node.span.end - baseByteOffset,
921
1068
  content: `(${JSON.stringify(staticObj[propName])})`,
922
1069
  });
1070
+ }
923
1071
  }
924
1072
  }
925
1073
  },
@@ -940,16 +1088,18 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
940
1088
  const varName = node.value;
941
1089
  const uniqueKey = `${resourcePath}-${varName}`;
942
1090
  let hash = scannedTables.createHashTable[uniqueKey];
943
- if (!hash)
1091
+ if (!hash) {
944
1092
  hash = mergedCreateTable[varName];
1093
+ }
945
1094
  if (hash) {
946
1095
  const obj = scannedTables.createObjectTable[hash];
947
1096
  const atomicMap = scannedTables.createAtomicMapTable[hash];
948
1097
  if (obj && atomicMap) {
949
1098
  const hashMap = {};
950
1099
  Object.keys(obj).forEach((key) => {
951
- if (atomicMap[key])
1100
+ if (atomicMap[key]) {
952
1101
  hashMap[key] = atomicMap[key];
1102
+ }
953
1103
  });
954
1104
  replacements.push({
955
1105
  start: node.span.start - baseByteOffset,
@@ -959,29 +1109,33 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
959
1109
  }
960
1110
  }
961
1111
  let themeHash = scannedTables.createThemeHashTable[uniqueKey];
962
- if (!themeHash)
1112
+ if (!themeHash) {
963
1113
  themeHash = mergedCreateThemeHashTable[varName];
1114
+ }
964
1115
  if (themeHash) {
965
1116
  const atomicMap = scannedTables.createAtomicMapTable[themeHash];
966
- if (atomicMap)
1117
+ if (atomicMap) {
967
1118
  replacements.push({
968
1119
  start: node.span.start - baseByteOffset,
969
1120
  end: node.span.end - baseByteOffset,
970
1121
  content: `(${JSON.stringify(atomicMap)})`,
971
1122
  });
972
- return;
1123
+ return;
1124
+ }
973
1125
  }
974
1126
  let staticHash = scannedTables.createStaticHashTable[uniqueKey];
975
- if (!staticHash)
1127
+ if (!staticHash) {
976
1128
  staticHash = mergedCreateStaticHashTable[varName];
1129
+ }
977
1130
  if (staticHash) {
978
1131
  const staticObj = scannedTables.createStaticObjectTable[staticHash];
979
- if (staticObj)
1132
+ if (staticObj) {
980
1133
  replacements.push({
981
1134
  start: node.span.start - baseByteOffset,
982
1135
  end: node.span.end - baseByteOffset,
983
1136
  content: `(${JSON.stringify(staticObj)})`,
984
1137
  });
1138
+ }
985
1139
  }
986
1140
  },
987
1141
  JSXAttribute({ node }) {
@@ -1044,11 +1198,10 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1044
1198
  }
1045
1199
  }
1046
1200
  args = args.filter((arg) => {
1047
- const innerExpr = arg.expression;
1048
- if (!t.isCallExpression(innerExpr) ||
1049
- !t.isMemberExpression(innerExpr.callee))
1201
+ const expr = arg.expression;
1202
+ if (!t.isCallExpression(expr) || !t.isMemberExpression(expr.callee))
1050
1203
  return true;
1051
- const callee = innerExpr.callee;
1204
+ const callee = expr.callee;
1052
1205
  if (!t.isIdentifier(callee.object) ||
1053
1206
  !t.isIdentifier(callee.property))
1054
1207
  return true;
@@ -1057,7 +1210,7 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1057
1210
  const styleInfo = localCreateStyles[varName];
1058
1211
  if (styleInfo?.functions?.[propKey]) {
1059
1212
  const func = styleInfo.functions[propKey];
1060
- const callArgs = innerExpr.arguments;
1213
+ const callArgs = expr.arguments;
1061
1214
  const hasSpread = callArgs.some((a) => a.spread);
1062
1215
  if (!hasSpread && callArgs.length >= 1) {
1063
1216
  const tempStaticTable = { ...mergedStaticTable };
@@ -1158,32 +1311,33 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1158
1311
  const objectName = callee.object.value;
1159
1312
  const propertyName = callee.property.value;
1160
1313
  const alias = plumeriaAliases[objectName];
1161
- if (alias === 'NAMESPACE' && propertyName === 'use')
1314
+ if (alias === 'NAMESPACE' && propertyName === 'use') {
1162
1315
  isUseCall = true;
1316
+ }
1163
1317
  }
1164
1318
  else if (t.isIdentifier(callee)) {
1165
1319
  const calleeName = callee.value;
1166
1320
  const originalName = plumeriaAliases[calleeName];
1167
- if (originalName === 'use')
1321
+ if (originalName === 'use') {
1168
1322
  isUseCall = true;
1323
+ }
1169
1324
  }
1170
1325
  if (!isUseCall)
1171
1326
  return;
1172
1327
  const args = node.arguments;
1173
1328
  for (const arg of args) {
1174
- const innerExpr = arg.expression;
1175
- if (!t.isCallExpression(innerExpr) ||
1176
- !t.isMemberExpression(innerExpr.callee))
1329
+ const expr = arg.expression;
1330
+ if (!t.isCallExpression(expr) || !t.isMemberExpression(expr.callee))
1177
1331
  continue;
1178
- const innerCallee = innerExpr.callee;
1179
- if (!t.isIdentifier(innerCallee.object) ||
1180
- !t.isIdentifier(innerCallee.property))
1332
+ const callee = expr.callee;
1333
+ if (!t.isIdentifier(callee.object) ||
1334
+ !t.isIdentifier(callee.property))
1181
1335
  continue;
1182
- const varName = innerCallee.object.value;
1183
- const propKey = innerCallee.property.value;
1336
+ const varName = callee.object.value;
1337
+ const propKey = callee.property.value;
1184
1338
  const styleInfo = localCreateStyles[varName];
1185
1339
  if (styleInfo?.functions?.[propKey]) {
1186
- throw new Error(`Plumeria: css.use(${getSource(innerExpr)}) does not support dynamic function keys.\n`);
1340
+ throw new Error(`Plumeria: css.use(${getSource(expr)}) does not support dynamic function keys.\n`);
1187
1341
  }
1188
1342
  }
1189
1343
  const { classParts, isOptimizable, baseStyle } = buildClassParts(args);
@@ -1199,8 +1353,9 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1199
1353
  },
1200
1354
  });
1201
1355
  Object.values(localCreateStyles).forEach((info) => {
1202
- if (info.type === 'constant')
1356
+ if (info.type === 'constant') {
1203
1357
  return;
1358
+ }
1204
1359
  if (info.isExported) {
1205
1360
  replacements.push({
1206
1361
  start: info.declSpan.start,
@@ -1216,7 +1371,6 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1216
1371
  });
1217
1372
  }
1218
1373
  });
1219
- const optInCSS = await optimizer(extractedSheets.join(''));
1220
1374
  const buffer = Buffer.from(source);
1221
1375
  let offset = 0;
1222
1376
  const parts = [];
@@ -1231,6 +1385,7 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1231
1385
  });
1232
1386
  parts.push(buffer.subarray(offset));
1233
1387
  const transformedSource = Buffer.concat(parts).toString();
1388
+ const optInCSS = await optimizer(extractedSheets.join(''));
1234
1389
  if (extractedSheets.length > 0) {
1235
1390
  const baseId = id.replace(EXTENSION_PATTERN, '');
1236
1391
  const cssFilename = `${baseId}.zero.css`;