@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.js +378 -223
- package/dist/core.mjs +379 -224
- package/dist/vite.js +13 -3
- package/dist/vite.mjs +13 -3
- package/package.json +2 -2
package/dist/core.js
CHANGED
|
@@ -290,6 +290,325 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
290
290
|
const processedDecls = new Set();
|
|
291
291
|
const idSpans = new Set();
|
|
292
292
|
const excludedSpans = new Set();
|
|
293
|
+
const checkVariantAssignment = (decl) => {
|
|
294
|
+
const init = decl.init;
|
|
295
|
+
if (init && utils_1.t.isCallExpression(init) && utils_1.t.isIdentifier(init.callee)) {
|
|
296
|
+
const varName = init.callee.value;
|
|
297
|
+
if ((localCreateStyles[varName] &&
|
|
298
|
+
localCreateStyles[varName].type === 'variant') ||
|
|
299
|
+
mergedVariantsTable[varName]) {
|
|
300
|
+
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: ${utils_1.t.isIdentifier(decl.id) ? decl.id.value : 'unknown'}`);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
const registerStyle = (node, declSpan, isExported) => {
|
|
305
|
+
let propName;
|
|
306
|
+
const init = node.init;
|
|
307
|
+
if (utils_1.t.isIdentifier(node.id) &&
|
|
308
|
+
init &&
|
|
309
|
+
utils_1.t.isCallExpression(init) &&
|
|
310
|
+
init.arguments.length >= 1) {
|
|
311
|
+
const callee = init.callee;
|
|
312
|
+
if (utils_1.t.isMemberExpression(callee) &&
|
|
313
|
+
utils_1.t.isIdentifier(callee.object) &&
|
|
314
|
+
utils_1.t.isIdentifier(callee.property)) {
|
|
315
|
+
const objectName = callee.object.value;
|
|
316
|
+
const propertyName = callee.property.value;
|
|
317
|
+
const alias = plumeriaAliases[objectName];
|
|
318
|
+
if (alias === 'NAMESPACE') {
|
|
319
|
+
propName = propertyName;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
else if (utils_1.t.isIdentifier(callee)) {
|
|
323
|
+
const calleeName = callee.value;
|
|
324
|
+
const originalName = plumeriaAliases[calleeName];
|
|
325
|
+
if (originalName) {
|
|
326
|
+
propName = originalName;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (propName && init && utils_1.t.isCallExpression(init)) {
|
|
331
|
+
if (propName === 'create' &&
|
|
332
|
+
utils_1.t.isObjectExpression(init.arguments[0].expression)) {
|
|
333
|
+
const obj = (0, utils_1.objectExpressionToObject)(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
334
|
+
if (obj) {
|
|
335
|
+
const hashMap = {};
|
|
336
|
+
Object.entries(obj).forEach(([key, style]) => {
|
|
337
|
+
if (typeof style !== 'object' || style === null)
|
|
338
|
+
return;
|
|
339
|
+
const records = (0, utils_1.getStyleRecords)(style);
|
|
340
|
+
const atomMap = {};
|
|
341
|
+
records.forEach((r) => (atomMap[r.key] = r.hash));
|
|
342
|
+
hashMap[key] = atomMap;
|
|
343
|
+
});
|
|
344
|
+
const styleFunctions = {};
|
|
345
|
+
const objExpr = init.arguments[0].expression;
|
|
346
|
+
objExpr.properties.forEach((prop) => {
|
|
347
|
+
if (prop.type !== 'KeyValueProperty' ||
|
|
348
|
+
prop.key.type !== 'Identifier')
|
|
349
|
+
return;
|
|
350
|
+
const func = prop.value;
|
|
351
|
+
if (func.type !== 'ArrowFunctionExpression' &&
|
|
352
|
+
func.type !== 'FunctionExpression')
|
|
353
|
+
return;
|
|
354
|
+
const params = func.params.map((p) => {
|
|
355
|
+
if (utils_1.t.isIdentifier(p))
|
|
356
|
+
return p.value;
|
|
357
|
+
if (typeof p === 'object' &&
|
|
358
|
+
p !== null &&
|
|
359
|
+
'pat' in p &&
|
|
360
|
+
utils_1.t.isIdentifier(p.pat))
|
|
361
|
+
return p.pat.value;
|
|
362
|
+
return 'arg';
|
|
363
|
+
});
|
|
364
|
+
let actualBody = func.body;
|
|
365
|
+
if (actualBody?.type === 'ParenthesisExpression')
|
|
366
|
+
actualBody = actualBody.expression;
|
|
367
|
+
if (actualBody?.type === 'BlockStatement') {
|
|
368
|
+
const first = actualBody.stmts?.[0];
|
|
369
|
+
if (first?.type === 'ReturnStatement')
|
|
370
|
+
actualBody = first.argument;
|
|
371
|
+
if (actualBody?.type === 'ParenthesisExpression')
|
|
372
|
+
actualBody = actualBody.expression;
|
|
373
|
+
}
|
|
374
|
+
if (actualBody && actualBody.type === 'ObjectExpression') {
|
|
375
|
+
styleFunctions[prop.key.value] = {
|
|
376
|
+
params,
|
|
377
|
+
body: actualBody,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
if (utils_1.t.isIdentifier(node.id)) {
|
|
382
|
+
idSpans.add(node.id.span.start);
|
|
383
|
+
}
|
|
384
|
+
if (utils_1.t.isIdentifier(node.id)) {
|
|
385
|
+
localCreateStyles[node.id.value] = {
|
|
386
|
+
name: node.id.value,
|
|
387
|
+
type: 'create',
|
|
388
|
+
obj,
|
|
389
|
+
hashMap,
|
|
390
|
+
isExported,
|
|
391
|
+
initSpan: {
|
|
392
|
+
start: init.span.start - baseByteOffset,
|
|
393
|
+
end: init.span.end - baseByteOffset,
|
|
394
|
+
},
|
|
395
|
+
declSpan: {
|
|
396
|
+
start: declSpan.start - baseByteOffset,
|
|
397
|
+
end: declSpan.end - baseByteOffset,
|
|
398
|
+
},
|
|
399
|
+
functions: styleFunctions,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
else if (propName === 'variants' &&
|
|
405
|
+
utils_1.t.isObjectExpression(init.arguments[0].expression)) {
|
|
406
|
+
if (utils_1.t.isIdentifier(node.id)) {
|
|
407
|
+
idSpans.add(node.id.span.start);
|
|
408
|
+
}
|
|
409
|
+
const obj = (0, utils_1.objectExpressionToObject)(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable, (name) => {
|
|
410
|
+
if (localCreateStyles[name]) {
|
|
411
|
+
return localCreateStyles[name].obj;
|
|
412
|
+
}
|
|
413
|
+
if (mergedCreateTable[name]) {
|
|
414
|
+
const hash = mergedCreateTable[name];
|
|
415
|
+
if (scannedTables.createObjectTable[hash]) {
|
|
416
|
+
return scannedTables.createObjectTable[hash];
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return undefined;
|
|
420
|
+
});
|
|
421
|
+
const { hashMap } = (0, utils_1.processVariants)(obj);
|
|
422
|
+
if (utils_1.t.isIdentifier(node.id)) {
|
|
423
|
+
localCreateStyles[node.id.value] = {
|
|
424
|
+
name: node.id.value,
|
|
425
|
+
type: 'variant',
|
|
426
|
+
obj,
|
|
427
|
+
hashMap,
|
|
428
|
+
isExported,
|
|
429
|
+
initSpan: {
|
|
430
|
+
start: init.span.start - baseByteOffset,
|
|
431
|
+
end: init.span.end - baseByteOffset,
|
|
432
|
+
},
|
|
433
|
+
declSpan: {
|
|
434
|
+
start: declSpan.start - baseByteOffset,
|
|
435
|
+
end: declSpan.end - baseByteOffset,
|
|
436
|
+
},
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
else if (propName === 'createTheme' &&
|
|
441
|
+
utils_1.t.isObjectExpression(init.arguments[0].expression)) {
|
|
442
|
+
if (utils_1.t.isIdentifier(node.id)) {
|
|
443
|
+
idSpans.add(node.id.span.start);
|
|
444
|
+
}
|
|
445
|
+
const obj = (0, utils_1.objectExpressionToObject)(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
446
|
+
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
447
|
+
if (utils_1.t.isIdentifier(node.id)) {
|
|
448
|
+
const uniqueKey = `${resourcePath}-${node.id.value}`;
|
|
449
|
+
scannedTables.createThemeHashTable[uniqueKey] = hash;
|
|
450
|
+
scannedTables.createThemeObjectTable[hash] = obj;
|
|
451
|
+
mergedCreateThemeHashTable[node.id.value] = hash;
|
|
452
|
+
const themeHashMap = {};
|
|
453
|
+
for (const [key] of Object.entries(obj)) {
|
|
454
|
+
const cssVarName = (0, zss_engine_1.camelToKebabCase)(key);
|
|
455
|
+
themeHashMap[key] = `var(--${hash}-${cssVarName})`;
|
|
456
|
+
}
|
|
457
|
+
localCreateStyles[node.id.value] = {
|
|
458
|
+
name: node.id.value,
|
|
459
|
+
type: 'constant',
|
|
460
|
+
obj,
|
|
461
|
+
hashMap: themeHashMap,
|
|
462
|
+
isExported,
|
|
463
|
+
initSpan: {
|
|
464
|
+
start: init.span.start - baseByteOffset,
|
|
465
|
+
end: init.span.end - baseByteOffset,
|
|
466
|
+
},
|
|
467
|
+
declSpan: {
|
|
468
|
+
start: declSpan.start - baseByteOffset,
|
|
469
|
+
end: declSpan.end - baseByteOffset,
|
|
470
|
+
},
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
(0, utils_1.traverse)(ast, {
|
|
477
|
+
ImportDeclaration({ node }) {
|
|
478
|
+
if (node.source.value === '@plumeria/core') {
|
|
479
|
+
if (node.typeOnly)
|
|
480
|
+
return;
|
|
481
|
+
const typeOnlySpecs = node.specifiers.filter((s) => s.type === 'ImportSpecifier' && s.isTypeOnly);
|
|
482
|
+
if (typeOnlySpecs.length > 0) {
|
|
483
|
+
const names = typeOnlySpecs
|
|
484
|
+
.map((s) => {
|
|
485
|
+
if (s.type !== 'ImportSpecifier')
|
|
486
|
+
return;
|
|
487
|
+
const imported = s.imported
|
|
488
|
+
? s.imported.value
|
|
489
|
+
: s.local.value;
|
|
490
|
+
const local = s.local.value;
|
|
491
|
+
return imported === local
|
|
492
|
+
? imported
|
|
493
|
+
: `${imported} as ${local}`;
|
|
494
|
+
})
|
|
495
|
+
.join(', ');
|
|
496
|
+
replacements.push({
|
|
497
|
+
start: node.span.start - baseByteOffset,
|
|
498
|
+
end: node.span.end - baseByteOffset,
|
|
499
|
+
content: `import type { ${names} } from '@plumeria/core'`,
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
503
|
+
replacements.push({
|
|
504
|
+
start: node.span.start - baseByteOffset,
|
|
505
|
+
end: node.span.end - baseByteOffset,
|
|
506
|
+
content: '',
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
node.specifiers.forEach((specifier) => {
|
|
511
|
+
if (specifier.local) {
|
|
512
|
+
excludedSpans.add(specifier.local.span.start);
|
|
513
|
+
}
|
|
514
|
+
if (specifier.type === 'ImportSpecifier' && specifier.imported) {
|
|
515
|
+
excludedSpans.add(specifier.imported.span.start);
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
},
|
|
519
|
+
ExportDeclaration({ node }) {
|
|
520
|
+
if (utils_1.t.isVariableDeclaration(node.declaration)) {
|
|
521
|
+
processedDecls.add(node.declaration);
|
|
522
|
+
node.declaration.declarations.forEach((decl) => {
|
|
523
|
+
checkVariantAssignment(decl);
|
|
524
|
+
registerStyle(decl, node.span, true);
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
},
|
|
528
|
+
VariableDeclaration({ node }) {
|
|
529
|
+
if (processedDecls.has(node))
|
|
530
|
+
return;
|
|
531
|
+
node.declarations.forEach((decl) => {
|
|
532
|
+
checkVariantAssignment(decl);
|
|
533
|
+
registerStyle(decl, node.span, false);
|
|
534
|
+
});
|
|
535
|
+
},
|
|
536
|
+
CallExpression({ node }) {
|
|
537
|
+
const callee = node.callee;
|
|
538
|
+
let propName;
|
|
539
|
+
if (utils_1.t.isMemberExpression(callee) &&
|
|
540
|
+
utils_1.t.isIdentifier(callee.object) &&
|
|
541
|
+
utils_1.t.isIdentifier(callee.property)) {
|
|
542
|
+
const objectName = callee.object.value;
|
|
543
|
+
const propertyName = callee.property.value;
|
|
544
|
+
const alias = plumeriaAliases[objectName];
|
|
545
|
+
if (alias === 'NAMESPACE') {
|
|
546
|
+
propName = propertyName;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
else if (utils_1.t.isIdentifier(callee)) {
|
|
550
|
+
const calleeName = callee.value;
|
|
551
|
+
const originalName = plumeriaAliases[calleeName];
|
|
552
|
+
if (originalName) {
|
|
553
|
+
propName = originalName;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
if (propName) {
|
|
557
|
+
const args = node.arguments;
|
|
558
|
+
if (propName === 'keyframes') {
|
|
559
|
+
const expr = args[0].expression;
|
|
560
|
+
if (utils_1.t.isObjectExpression(expr)) {
|
|
561
|
+
const obj = (0, utils_1.objectExpressionToObject)(expr, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
562
|
+
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
563
|
+
scannedTables.keyframesObjectTable[hash] = obj;
|
|
564
|
+
replacements.push({
|
|
565
|
+
start: node.span.start - baseByteOffset,
|
|
566
|
+
end: node.span.end - baseByteOffset,
|
|
567
|
+
content: JSON.stringify(`kf-${hash}`),
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
else if (propName === 'viewTransition' &&
|
|
572
|
+
args.length > 0 &&
|
|
573
|
+
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
574
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
575
|
+
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
576
|
+
scannedTables.viewTransitionObjectTable[hash] = obj;
|
|
577
|
+
replacements.push({
|
|
578
|
+
start: node.span.start - baseByteOffset,
|
|
579
|
+
end: node.span.end - baseByteOffset,
|
|
580
|
+
content: JSON.stringify(`vt-${hash}`),
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
else if ((propName === 'createTheme' || propName === 'createStatic') &&
|
|
584
|
+
args.length > 0 &&
|
|
585
|
+
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
586
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
587
|
+
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
588
|
+
if (propName === 'createTheme') {
|
|
589
|
+
scannedTables.createThemeObjectTable[hash] = obj;
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
scannedTables.createStaticObjectTable[hash] = obj;
|
|
593
|
+
}
|
|
594
|
+
const prefix = propName === 'createTheme' ? 'tm-' : 'st-';
|
|
595
|
+
replacements.push({
|
|
596
|
+
start: node.span.start - baseByteOffset,
|
|
597
|
+
end: node.span.end - baseByteOffset,
|
|
598
|
+
content: JSON.stringify(`${prefix}${hash}`),
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
else if (propName === 'create' &&
|
|
602
|
+
args.length > 0 &&
|
|
603
|
+
utils_1.t.isObjectExpression(args[0].expression)) {
|
|
604
|
+
const obj = (0, utils_1.objectExpressionToObject)(args[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
605
|
+
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
606
|
+
scannedTables.createObjectTable[hash] = obj;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
},
|
|
610
|
+
});
|
|
611
|
+
const jsxOpeningElementMap = new Map();
|
|
293
612
|
const getSource = (node) => {
|
|
294
613
|
const start = node.span.start - baseByteOffset;
|
|
295
614
|
const end = node.span.end - baseByteOffset;
|
|
@@ -416,9 +735,9 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
416
735
|
if (variantObj) {
|
|
417
736
|
const callArgs = expr.arguments;
|
|
418
737
|
if (callArgs.length === 1 && !callArgs[0].spread) {
|
|
419
|
-
const
|
|
420
|
-
if (
|
|
421
|
-
for (const prop of
|
|
738
|
+
const arg = callArgs[0].expression;
|
|
739
|
+
if (arg.type === 'ObjectExpression') {
|
|
740
|
+
for (const prop of arg.properties) {
|
|
422
741
|
let groupName;
|
|
423
742
|
let valExpr;
|
|
424
743
|
if (prop.type === 'KeyValueProperty' &&
|
|
@@ -459,16 +778,16 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
459
778
|
}
|
|
460
779
|
continue;
|
|
461
780
|
}
|
|
462
|
-
const argSource = getSource(
|
|
463
|
-
if (utils_1.t.isStringLiteral(
|
|
464
|
-
if (variantObj[
|
|
465
|
-
baseStyle = (0, utils_1.deepMerge)(baseStyle, variantObj[
|
|
781
|
+
const argSource = getSource(arg);
|
|
782
|
+
if (utils_1.t.isStringLiteral(arg)) {
|
|
783
|
+
if (variantObj[arg.value])
|
|
784
|
+
baseStyle = (0, utils_1.deepMerge)(baseStyle, variantObj[arg.value]);
|
|
466
785
|
continue;
|
|
467
786
|
}
|
|
468
787
|
const currentGroupId = ++groupIdCounter;
|
|
469
788
|
Object.entries(variantObj).forEach(([key, style]) => {
|
|
470
789
|
conditionals.push({
|
|
471
|
-
test:
|
|
790
|
+
test: arg,
|
|
472
791
|
testLHS: argSource,
|
|
473
792
|
testString: `${argSource} === '${key}'`,
|
|
474
793
|
truthy: style,
|
|
@@ -644,10 +963,12 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
644
963
|
indepVarGroups[c.groupId].push(c);
|
|
645
964
|
}
|
|
646
965
|
});
|
|
647
|
-
Object.values(indepVarGroups).forEach((
|
|
648
|
-
const commonTestExpr =
|
|
966
|
+
Object.values(indepVarGroups).forEach((options) => {
|
|
967
|
+
const commonTestExpr = options[0].testLHS ??
|
|
968
|
+
options[0].testString ??
|
|
969
|
+
getSource(options[0].test);
|
|
649
970
|
const lookupMap = {};
|
|
650
|
-
|
|
971
|
+
options.forEach((opt) => {
|
|
651
972
|
if (opt.valueName && opt.truthy) {
|
|
652
973
|
const className = processStyleRecords(opt.truthy)
|
|
653
974
|
.map((r) => r.hash)
|
|
@@ -730,187 +1051,7 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
730
1051
|
classParts.push(...dynamicClassParts);
|
|
731
1052
|
return { classParts, isOptimizable, baseStyle };
|
|
732
1053
|
};
|
|
733
|
-
const registerStyle = (node, declSpan, isExported) => {
|
|
734
|
-
let propName;
|
|
735
|
-
const init = node.init;
|
|
736
|
-
if (utils_1.t.isIdentifier(node.id) &&
|
|
737
|
-
init &&
|
|
738
|
-
utils_1.t.isCallExpression(init) &&
|
|
739
|
-
init.arguments.length >= 1) {
|
|
740
|
-
const callee = init.callee;
|
|
741
|
-
if (utils_1.t.isMemberExpression(callee) &&
|
|
742
|
-
utils_1.t.isIdentifier(callee.object) &&
|
|
743
|
-
utils_1.t.isIdentifier(callee.property)) {
|
|
744
|
-
const objectName = callee.object.value;
|
|
745
|
-
const propertyName = callee.property.value;
|
|
746
|
-
const alias = plumeriaAliases[objectName];
|
|
747
|
-
if (alias === 'NAMESPACE')
|
|
748
|
-
propName = propertyName;
|
|
749
|
-
}
|
|
750
|
-
else if (utils_1.t.isIdentifier(callee)) {
|
|
751
|
-
const calleeName = callee.value;
|
|
752
|
-
const originalName = plumeriaAliases[calleeName];
|
|
753
|
-
if (originalName)
|
|
754
|
-
propName = originalName;
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
if (propName && init && utils_1.t.isCallExpression(init)) {
|
|
758
|
-
if (propName === 'create' &&
|
|
759
|
-
utils_1.t.isObjectExpression(init.arguments[0].expression)) {
|
|
760
|
-
const obj = (0, utils_1.objectExpressionToObject)(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
761
|
-
if (obj) {
|
|
762
|
-
const hashMap = {};
|
|
763
|
-
Object.entries(obj).forEach(([key, style]) => {
|
|
764
|
-
if (typeof style !== 'object' || style === null)
|
|
765
|
-
return;
|
|
766
|
-
const records = (0, utils_1.getStyleRecords)(style);
|
|
767
|
-
const atomMap = {};
|
|
768
|
-
records.forEach((r) => (atomMap[r.key] = r.hash));
|
|
769
|
-
hashMap[key] = atomMap;
|
|
770
|
-
});
|
|
771
|
-
const styleFunctions = {};
|
|
772
|
-
const objExpr = init.arguments[0].expression;
|
|
773
|
-
objExpr.properties.forEach((prop) => {
|
|
774
|
-
if (prop.type !== 'KeyValueProperty' ||
|
|
775
|
-
prop.key.type !== 'Identifier')
|
|
776
|
-
return;
|
|
777
|
-
const func = prop.value;
|
|
778
|
-
if (func.type !== 'ArrowFunctionExpression' &&
|
|
779
|
-
func.type !== 'FunctionExpression')
|
|
780
|
-
return;
|
|
781
|
-
const params = func.params.map((p) => {
|
|
782
|
-
if (utils_1.t.isIdentifier(p))
|
|
783
|
-
return p.value;
|
|
784
|
-
if (typeof p === 'object' &&
|
|
785
|
-
p !== null &&
|
|
786
|
-
'pat' in p &&
|
|
787
|
-
utils_1.t.isIdentifier(p.pat))
|
|
788
|
-
return p.pat.value;
|
|
789
|
-
return 'arg';
|
|
790
|
-
});
|
|
791
|
-
let actualBody = func.body;
|
|
792
|
-
if (actualBody?.type === 'ParenthesisExpression')
|
|
793
|
-
actualBody = actualBody.expression;
|
|
794
|
-
if (actualBody?.type === 'BlockStatement') {
|
|
795
|
-
const first = actualBody.stmts?.[0];
|
|
796
|
-
if (first?.type === 'ReturnStatement')
|
|
797
|
-
actualBody = first.argument;
|
|
798
|
-
if (actualBody?.type === 'ParenthesisExpression')
|
|
799
|
-
actualBody = actualBody.expression;
|
|
800
|
-
}
|
|
801
|
-
if (actualBody && actualBody.type === 'ObjectExpression') {
|
|
802
|
-
styleFunctions[prop.key.value] = {
|
|
803
|
-
params,
|
|
804
|
-
body: actualBody,
|
|
805
|
-
};
|
|
806
|
-
}
|
|
807
|
-
});
|
|
808
|
-
if (utils_1.t.isIdentifier(node.id)) {
|
|
809
|
-
idSpans.add(node.id.span.start);
|
|
810
|
-
localCreateStyles[node.id.value] = {
|
|
811
|
-
name: node.id.value,
|
|
812
|
-
type: 'create',
|
|
813
|
-
obj,
|
|
814
|
-
hashMap,
|
|
815
|
-
isExported,
|
|
816
|
-
initSpan: {
|
|
817
|
-
start: init.span.start - baseByteOffset,
|
|
818
|
-
end: init.span.end - baseByteOffset,
|
|
819
|
-
},
|
|
820
|
-
declSpan: {
|
|
821
|
-
start: declSpan.start - baseByteOffset,
|
|
822
|
-
end: declSpan.end - baseByteOffset,
|
|
823
|
-
},
|
|
824
|
-
functions: styleFunctions,
|
|
825
|
-
};
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
}
|
|
829
|
-
else if (propName === 'variants' &&
|
|
830
|
-
utils_1.t.isObjectExpression(init.arguments[0].expression)) {
|
|
831
|
-
if (utils_1.t.isIdentifier(node.id))
|
|
832
|
-
idSpans.add(node.id.span.start);
|
|
833
|
-
const obj = (0, utils_1.objectExpressionToObject)(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable, (name) => {
|
|
834
|
-
if (localCreateStyles[name])
|
|
835
|
-
return localCreateStyles[name].obj;
|
|
836
|
-
if (mergedCreateTable[name]) {
|
|
837
|
-
const hash = mergedCreateTable[name];
|
|
838
|
-
if (scannedTables.createObjectTable[hash])
|
|
839
|
-
return scannedTables.createObjectTable[hash];
|
|
840
|
-
}
|
|
841
|
-
return undefined;
|
|
842
|
-
});
|
|
843
|
-
const { hashMap } = (0, utils_1.processVariants)(obj);
|
|
844
|
-
if (utils_1.t.isIdentifier(node.id)) {
|
|
845
|
-
localCreateStyles[node.id.value] = {
|
|
846
|
-
name: node.id.value,
|
|
847
|
-
type: 'variant',
|
|
848
|
-
obj,
|
|
849
|
-
hashMap,
|
|
850
|
-
isExported,
|
|
851
|
-
initSpan: {
|
|
852
|
-
start: init.span.start - baseByteOffset,
|
|
853
|
-
end: init.span.end - baseByteOffset,
|
|
854
|
-
},
|
|
855
|
-
declSpan: {
|
|
856
|
-
start: declSpan.start - baseByteOffset,
|
|
857
|
-
end: declSpan.end - baseByteOffset,
|
|
858
|
-
},
|
|
859
|
-
};
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
else if (propName === 'createTheme' &&
|
|
863
|
-
utils_1.t.isObjectExpression(init.arguments[0].expression)) {
|
|
864
|
-
if (utils_1.t.isIdentifier(node.id))
|
|
865
|
-
idSpans.add(node.id.span.start);
|
|
866
|
-
const obj = (0, utils_1.objectExpressionToObject)(init.arguments[0].expression, mergedStaticTable, mergedKeyframesTable, mergedViewTransitionTable, mergedCreateThemeHashTable, scannedTables.createThemeObjectTable, mergedCreateTable, mergedCreateStaticHashTable, scannedTables.createStaticObjectTable, mergedVariantsTable);
|
|
867
|
-
const hash = (0, zss_engine_1.genBase36Hash)(obj, 1, 8);
|
|
868
|
-
if (utils_1.t.isIdentifier(node.id)) {
|
|
869
|
-
const uniqueKey = `${resourcePath}-${node.id.value}`;
|
|
870
|
-
scannedTables.createThemeHashTable[uniqueKey] = hash;
|
|
871
|
-
scannedTables.createThemeObjectTable[hash] = obj;
|
|
872
|
-
localCreateStyles[node.id.value] = {
|
|
873
|
-
name: node.id.value,
|
|
874
|
-
type: 'constant',
|
|
875
|
-
obj,
|
|
876
|
-
hashMap: scannedTables.createAtomicMapTable[hash],
|
|
877
|
-
isExported,
|
|
878
|
-
initSpan: {
|
|
879
|
-
start: init.span.start - baseByteOffset,
|
|
880
|
-
end: init.span.end - baseByteOffset,
|
|
881
|
-
},
|
|
882
|
-
declSpan: {
|
|
883
|
-
start: declSpan.start - baseByteOffset,
|
|
884
|
-
end: declSpan.end - baseByteOffset,
|
|
885
|
-
},
|
|
886
|
-
};
|
|
887
|
-
}
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
};
|
|
891
|
-
const jsxOpeningElementMap = new Map();
|
|
892
1054
|
(0, utils_1.traverse)(ast, {
|
|
893
|
-
ImportDeclaration({ node }) {
|
|
894
|
-
if (node.specifiers) {
|
|
895
|
-
node.specifiers.forEach((specifier) => {
|
|
896
|
-
if (specifier.local)
|
|
897
|
-
excludedSpans.add(specifier.local.span.start);
|
|
898
|
-
if (specifier.type === 'ImportSpecifier' && specifier.imported)
|
|
899
|
-
excludedSpans.add(specifier.imported.span.start);
|
|
900
|
-
});
|
|
901
|
-
}
|
|
902
|
-
},
|
|
903
|
-
ExportDeclaration({ node }) {
|
|
904
|
-
if (utils_1.t.isVariableDeclaration(node.declaration)) {
|
|
905
|
-
processedDecls.add(node.declaration);
|
|
906
|
-
node.declaration.declarations.forEach((decl) => registerStyle(decl, node.span, true));
|
|
907
|
-
}
|
|
908
|
-
},
|
|
909
|
-
VariableDeclaration({ node }) {
|
|
910
|
-
if (processedDecls.has(node))
|
|
911
|
-
return;
|
|
912
|
-
node.declarations.forEach((decl) => registerStyle(decl, node.span, false));
|
|
913
|
-
},
|
|
914
1055
|
JSXOpeningElement({ node }) {
|
|
915
1056
|
jsxOpeningElementMap.set(node.span.start, node.attributes);
|
|
916
1057
|
},
|
|
@@ -920,42 +1061,49 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
920
1061
|
const propName = node.property.value;
|
|
921
1062
|
const uniqueKey = `${resourcePath}-${varName}`;
|
|
922
1063
|
let hash = scannedTables.createHashTable[uniqueKey];
|
|
923
|
-
if (!hash)
|
|
1064
|
+
if (!hash) {
|
|
924
1065
|
hash = mergedCreateTable[varName];
|
|
1066
|
+
}
|
|
925
1067
|
if (hash) {
|
|
926
1068
|
let atomMap;
|
|
927
|
-
if (scannedTables.createAtomicMapTable[hash])
|
|
1069
|
+
if (scannedTables.createAtomicMapTable[hash]) {
|
|
928
1070
|
atomMap = scannedTables.createAtomicMapTable[hash][propName];
|
|
929
|
-
|
|
1071
|
+
}
|
|
1072
|
+
if (atomMap) {
|
|
930
1073
|
replacements.push({
|
|
931
1074
|
start: node.span.start - baseByteOffset,
|
|
932
1075
|
end: node.span.end - baseByteOffset,
|
|
933
1076
|
content: `(${JSON.stringify(atomMap)})`,
|
|
934
1077
|
});
|
|
1078
|
+
}
|
|
935
1079
|
}
|
|
936
1080
|
let themeHash = scannedTables.createThemeHashTable[uniqueKey];
|
|
937
|
-
if (!themeHash)
|
|
1081
|
+
if (!themeHash) {
|
|
938
1082
|
themeHash = mergedCreateThemeHashTable[varName];
|
|
1083
|
+
}
|
|
939
1084
|
if (themeHash) {
|
|
940
1085
|
const atomicMap = scannedTables.createAtomicMapTable[themeHash];
|
|
941
|
-
if (atomicMap && atomicMap[propName])
|
|
1086
|
+
if (atomicMap && atomicMap && atomicMap[propName]) {
|
|
942
1087
|
replacements.push({
|
|
943
1088
|
start: node.span.start - baseByteOffset,
|
|
944
1089
|
end: node.span.end - baseByteOffset,
|
|
945
1090
|
content: `(${JSON.stringify(atomicMap[propName])})`,
|
|
946
1091
|
});
|
|
1092
|
+
}
|
|
947
1093
|
}
|
|
948
1094
|
let staticHash = scannedTables.createStaticHashTable[uniqueKey];
|
|
949
|
-
if (!staticHash)
|
|
1095
|
+
if (!staticHash) {
|
|
950
1096
|
staticHash = mergedCreateStaticHashTable[varName];
|
|
1097
|
+
}
|
|
951
1098
|
if (staticHash) {
|
|
952
1099
|
const staticObj = scannedTables.createStaticObjectTable[staticHash];
|
|
953
|
-
if (staticObj && staticObj[propName] !== undefined)
|
|
1100
|
+
if (staticObj && staticObj[propName] !== undefined) {
|
|
954
1101
|
replacements.push({
|
|
955
1102
|
start: node.span.start - baseByteOffset,
|
|
956
1103
|
end: node.span.end - baseByteOffset,
|
|
957
1104
|
content: `(${JSON.stringify(staticObj[propName])})`,
|
|
958
1105
|
});
|
|
1106
|
+
}
|
|
959
1107
|
}
|
|
960
1108
|
}
|
|
961
1109
|
},
|
|
@@ -976,16 +1124,18 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
976
1124
|
const varName = node.value;
|
|
977
1125
|
const uniqueKey = `${resourcePath}-${varName}`;
|
|
978
1126
|
let hash = scannedTables.createHashTable[uniqueKey];
|
|
979
|
-
if (!hash)
|
|
1127
|
+
if (!hash) {
|
|
980
1128
|
hash = mergedCreateTable[varName];
|
|
1129
|
+
}
|
|
981
1130
|
if (hash) {
|
|
982
1131
|
const obj = scannedTables.createObjectTable[hash];
|
|
983
1132
|
const atomicMap = scannedTables.createAtomicMapTable[hash];
|
|
984
1133
|
if (obj && atomicMap) {
|
|
985
1134
|
const hashMap = {};
|
|
986
1135
|
Object.keys(obj).forEach((key) => {
|
|
987
|
-
if (atomicMap[key])
|
|
1136
|
+
if (atomicMap[key]) {
|
|
988
1137
|
hashMap[key] = atomicMap[key];
|
|
1138
|
+
}
|
|
989
1139
|
});
|
|
990
1140
|
replacements.push({
|
|
991
1141
|
start: node.span.start - baseByteOffset,
|
|
@@ -995,29 +1145,33 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
995
1145
|
}
|
|
996
1146
|
}
|
|
997
1147
|
let themeHash = scannedTables.createThemeHashTable[uniqueKey];
|
|
998
|
-
if (!themeHash)
|
|
1148
|
+
if (!themeHash) {
|
|
999
1149
|
themeHash = mergedCreateThemeHashTable[varName];
|
|
1150
|
+
}
|
|
1000
1151
|
if (themeHash) {
|
|
1001
1152
|
const atomicMap = scannedTables.createAtomicMapTable[themeHash];
|
|
1002
|
-
if (atomicMap)
|
|
1153
|
+
if (atomicMap) {
|
|
1003
1154
|
replacements.push({
|
|
1004
1155
|
start: node.span.start - baseByteOffset,
|
|
1005
1156
|
end: node.span.end - baseByteOffset,
|
|
1006
1157
|
content: `(${JSON.stringify(atomicMap)})`,
|
|
1007
1158
|
});
|
|
1008
|
-
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1009
1161
|
}
|
|
1010
1162
|
let staticHash = scannedTables.createStaticHashTable[uniqueKey];
|
|
1011
|
-
if (!staticHash)
|
|
1163
|
+
if (!staticHash) {
|
|
1012
1164
|
staticHash = mergedCreateStaticHashTable[varName];
|
|
1165
|
+
}
|
|
1013
1166
|
if (staticHash) {
|
|
1014
1167
|
const staticObj = scannedTables.createStaticObjectTable[staticHash];
|
|
1015
|
-
if (staticObj)
|
|
1168
|
+
if (staticObj) {
|
|
1016
1169
|
replacements.push({
|
|
1017
1170
|
start: node.span.start - baseByteOffset,
|
|
1018
1171
|
end: node.span.end - baseByteOffset,
|
|
1019
1172
|
content: `(${JSON.stringify(staticObj)})`,
|
|
1020
1173
|
});
|
|
1174
|
+
}
|
|
1021
1175
|
}
|
|
1022
1176
|
},
|
|
1023
1177
|
JSXAttribute({ node }) {
|
|
@@ -1080,11 +1234,10 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
1080
1234
|
}
|
|
1081
1235
|
}
|
|
1082
1236
|
args = args.filter((arg) => {
|
|
1083
|
-
const
|
|
1084
|
-
if (!utils_1.t.isCallExpression(
|
|
1085
|
-
!utils_1.t.isMemberExpression(innerExpr.callee))
|
|
1237
|
+
const expr = arg.expression;
|
|
1238
|
+
if (!utils_1.t.isCallExpression(expr) || !utils_1.t.isMemberExpression(expr.callee))
|
|
1086
1239
|
return true;
|
|
1087
|
-
const callee =
|
|
1240
|
+
const callee = expr.callee;
|
|
1088
1241
|
if (!utils_1.t.isIdentifier(callee.object) ||
|
|
1089
1242
|
!utils_1.t.isIdentifier(callee.property))
|
|
1090
1243
|
return true;
|
|
@@ -1093,7 +1246,7 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
1093
1246
|
const styleInfo = localCreateStyles[varName];
|
|
1094
1247
|
if (styleInfo?.functions?.[propKey]) {
|
|
1095
1248
|
const func = styleInfo.functions[propKey];
|
|
1096
|
-
const callArgs =
|
|
1249
|
+
const callArgs = expr.arguments;
|
|
1097
1250
|
const hasSpread = callArgs.some((a) => a.spread);
|
|
1098
1251
|
if (!hasSpread && callArgs.length >= 1) {
|
|
1099
1252
|
const tempStaticTable = { ...mergedStaticTable };
|
|
@@ -1194,32 +1347,33 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
1194
1347
|
const objectName = callee.object.value;
|
|
1195
1348
|
const propertyName = callee.property.value;
|
|
1196
1349
|
const alias = plumeriaAliases[objectName];
|
|
1197
|
-
if (alias === 'NAMESPACE' && propertyName === 'use')
|
|
1350
|
+
if (alias === 'NAMESPACE' && propertyName === 'use') {
|
|
1198
1351
|
isUseCall = true;
|
|
1352
|
+
}
|
|
1199
1353
|
}
|
|
1200
1354
|
else if (utils_1.t.isIdentifier(callee)) {
|
|
1201
1355
|
const calleeName = callee.value;
|
|
1202
1356
|
const originalName = plumeriaAliases[calleeName];
|
|
1203
|
-
if (originalName === 'use')
|
|
1357
|
+
if (originalName === 'use') {
|
|
1204
1358
|
isUseCall = true;
|
|
1359
|
+
}
|
|
1205
1360
|
}
|
|
1206
1361
|
if (!isUseCall)
|
|
1207
1362
|
return;
|
|
1208
1363
|
const args = node.arguments;
|
|
1209
1364
|
for (const arg of args) {
|
|
1210
|
-
const
|
|
1211
|
-
if (!utils_1.t.isCallExpression(
|
|
1212
|
-
!utils_1.t.isMemberExpression(innerExpr.callee))
|
|
1365
|
+
const expr = arg.expression;
|
|
1366
|
+
if (!utils_1.t.isCallExpression(expr) || !utils_1.t.isMemberExpression(expr.callee))
|
|
1213
1367
|
continue;
|
|
1214
|
-
const
|
|
1215
|
-
if (!utils_1.t.isIdentifier(
|
|
1216
|
-
!utils_1.t.isIdentifier(
|
|
1368
|
+
const callee = expr.callee;
|
|
1369
|
+
if (!utils_1.t.isIdentifier(callee.object) ||
|
|
1370
|
+
!utils_1.t.isIdentifier(callee.property))
|
|
1217
1371
|
continue;
|
|
1218
|
-
const varName =
|
|
1219
|
-
const propKey =
|
|
1372
|
+
const varName = callee.object.value;
|
|
1373
|
+
const propKey = callee.property.value;
|
|
1220
1374
|
const styleInfo = localCreateStyles[varName];
|
|
1221
1375
|
if (styleInfo?.functions?.[propKey]) {
|
|
1222
|
-
throw new Error(`Plumeria: css.use(${getSource(
|
|
1376
|
+
throw new Error(`Plumeria: css.use(${getSource(expr)}) does not support dynamic function keys.\n`);
|
|
1223
1377
|
}
|
|
1224
1378
|
}
|
|
1225
1379
|
const { classParts, isOptimizable, baseStyle } = buildClassParts(args);
|
|
@@ -1235,8 +1389,9 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
1235
1389
|
},
|
|
1236
1390
|
});
|
|
1237
1391
|
Object.values(localCreateStyles).forEach((info) => {
|
|
1238
|
-
if (info.type === 'constant')
|
|
1392
|
+
if (info.type === 'constant') {
|
|
1239
1393
|
return;
|
|
1394
|
+
}
|
|
1240
1395
|
if (info.isExported) {
|
|
1241
1396
|
replacements.push({
|
|
1242
1397
|
start: info.declSpan.start,
|
|
@@ -1252,7 +1407,6 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
1252
1407
|
});
|
|
1253
1408
|
}
|
|
1254
1409
|
});
|
|
1255
|
-
const optInCSS = await (0, utils_1.optimizer)(extractedSheets.join(''));
|
|
1256
1410
|
const buffer = Buffer.from(source);
|
|
1257
1411
|
let offset = 0;
|
|
1258
1412
|
const parts = [];
|
|
@@ -1267,6 +1421,7 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
|
|
|
1267
1421
|
});
|
|
1268
1422
|
parts.push(buffer.subarray(offset));
|
|
1269
1423
|
const transformedSource = Buffer.concat(parts).toString();
|
|
1424
|
+
const optInCSS = await (0, utils_1.optimizer)(extractedSheets.join(''));
|
|
1270
1425
|
if (extractedSheets.length > 0) {
|
|
1271
1426
|
const baseId = id.replace(exports.EXTENSION_PATTERN, '');
|
|
1272
1427
|
const cssFilename = `${baseId}.zero.css`;
|