@plumeria/unplugin 16.4.2 → 16.5.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.
Files changed (3) hide show
  1. package/dist/core.js +49 -46
  2. package/dist/core.mjs +50 -47
  3. package/package.json +2 -2
package/dist/core.js CHANGED
@@ -382,17 +382,7 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
382
382
  const processedDecls = new Set();
383
383
  const idSpans = new Set();
384
384
  const excludedSpans = new Set();
385
- const checkVariantAssignment = (decl) => {
386
- const init = decl.init;
387
- if (init && utils_1.t.isCallExpression(init) && utils_1.t.isIdentifier(init.callee)) {
388
- const varName = init.callee.value;
389
- if ((localCreateStyles[varName] &&
390
- localCreateStyles[varName].type === 'variant') ||
391
- mergedVariantsTable[varName]) {
392
- throwCompilationError(`Plumeria: Assigning the return value of css.variants() to a variable is not supported.\nPlease pass the variant function directly to styleName or css.use(). Found assignment to: ${utils_1.t.isIdentifier(decl.id) ? decl.id.value : 'unknown'}`, init);
393
- }
394
- }
395
- };
385
+ const referenceIdents = (0, utils_1.collectReferenceIdentifiers)(ast);
396
386
  const registerStyle = (node, declSpan, isExported) => {
397
387
  let propName;
398
388
  const init = node.init;
@@ -618,7 +608,6 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
618
608
  if (utils_1.t.isVariableDeclaration(node.declaration)) {
619
609
  processedDecls.add(node.declaration);
620
610
  node.declaration.declarations.forEach((decl) => {
621
- checkVariantAssignment(decl);
622
611
  registerStyle(decl, node.span, true);
623
612
  checkStyleAliasAssignment(decl);
624
613
  });
@@ -628,7 +617,6 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
628
617
  if (processedDecls.has(node))
629
618
  return;
630
619
  node.declarations.forEach((decl) => {
631
- checkVariantAssignment(decl);
632
620
  registerStyle(decl, node.span, false);
633
621
  checkStyleAliasAssignment(decl);
634
622
  });
@@ -764,10 +752,17 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
764
752
  else if (utils_1.t.isMemberExpression(expr) &&
765
753
  utils_1.t.isIdentifier(expr.object) &&
766
754
  (utils_1.t.isIdentifier(expr.property) || expr.property.type === 'Computed')) {
767
- if (expr.property.type === 'Computed')
768
- return null;
769
755
  const varName = expr.object.value;
770
- const propName = expr.property.value;
756
+ let propName;
757
+ if (expr.property.type === 'Computed') {
758
+ const keyExpr = expr.property.expression;
759
+ if (!utils_1.t.isStringLiteral(keyExpr))
760
+ return null;
761
+ propName = keyExpr.value;
762
+ }
763
+ else {
764
+ propName = expr.property.value;
765
+ }
771
766
  const styleInfo = localCreateStyles[varName];
772
767
  if (styleInfo?.obj[propName]) {
773
768
  const style = styleInfo.obj[propName];
@@ -877,6 +872,14 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
877
872
  else if (node.type === 'ParenthesisExpression') {
878
873
  return collectConditions(node.expression, currentTestStrings);
879
874
  }
875
+ if (currentTestStrings.length > 0 &&
876
+ utils_1.t.isMemberExpression(node) &&
877
+ utils_1.t.isIdentifier(node.object) &&
878
+ node.property.type === 'Computed' &&
879
+ resolveCreateObject(node.object.value)) {
880
+ const varName = node.object.value;
881
+ throwCompilationError(`Plumeria: "${getSource(node)}" cannot be used inside a condition, because its bracket key is not a literal.\nMove the condition into the brackets instead, e.g. ${varName}[cond ? 'a' : 'b'].`, node);
882
+ }
880
883
  assertResolvable(node);
881
884
  return false;
882
885
  };
@@ -1360,17 +1363,6 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
1360
1363
  return;
1361
1364
  }
1362
1365
  }
1363
- if (localStyle && localStyle.type === 'variant') {
1364
- const variantMap = localStyle.hashMap[propName];
1365
- if (variantMap) {
1366
- replacements.push({
1367
- start: node.span.start - baseByteOffset,
1368
- end: node.span.end - baseByteOffset,
1369
- content: `(${JSON.stringify(variantMap)})`,
1370
- });
1371
- return;
1372
- }
1373
- }
1374
1366
  let hash = scannedTables.createHashTable[uniqueKey];
1375
1367
  if (!hash) {
1376
1368
  hash = mergedCreateTable[varName];
@@ -1431,13 +1423,21 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
1431
1423
  return;
1432
1424
  if (idSpans.has(node.span.start))
1433
1425
  return;
1434
- const styleInfo = localCreateStyles[node.value];
1435
- if (styleInfo) {
1426
+ if (!referenceIdents.references.has(node.span.start))
1427
+ return;
1428
+ const prefix = referenceIdents.shorthands.has(node.span.start)
1429
+ ? `${node.value}: `
1430
+ : '';
1431
+ const pushReplacement = (content) => {
1436
1432
  replacements.push({
1437
1433
  start: node.span.start - baseByteOffset,
1438
1434
  end: node.span.end - baseByteOffset,
1439
- content: `(${JSON.stringify(styleInfo.hashMap)})`,
1435
+ content: `${prefix}${content}`,
1440
1436
  });
1437
+ };
1438
+ const styleInfo = localCreateStyles[node.value];
1439
+ if (styleInfo) {
1440
+ pushReplacement(`(${JSON.stringify(styleInfo.hashMap)})`);
1441
1441
  return;
1442
1442
  }
1443
1443
  const varName = node.value;
@@ -1456,11 +1456,7 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
1456
1456
  hashMap[key] = atomicMap[key];
1457
1457
  }
1458
1458
  });
1459
- replacements.push({
1460
- start: node.span.start - baseByteOffset,
1461
- end: node.span.end - baseByteOffset,
1462
- content: `(${JSON.stringify(hashMap)})`,
1463
- });
1459
+ pushReplacement(`(${JSON.stringify(hashMap)})`);
1464
1460
  }
1465
1461
  }
1466
1462
  let themeHash = mergedCreateThemeHashTable[varName];
@@ -1470,11 +1466,7 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
1470
1466
  if (themeHash) {
1471
1467
  const atomicMap = scannedTables.createAtomicMapTable[themeHash];
1472
1468
  if (atomicMap) {
1473
- replacements.push({
1474
- start: node.span.start - baseByteOffset,
1475
- end: node.span.end - baseByteOffset,
1476
- content: `(${JSON.stringify(atomicMap)})`,
1477
- });
1469
+ pushReplacement(`(${JSON.stringify(atomicMap)})`);
1478
1470
  return;
1479
1471
  }
1480
1472
  }
@@ -1485,11 +1477,7 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
1485
1477
  if (staticHash) {
1486
1478
  const staticObj = scannedTables.createStaticObjectTable[staticHash];
1487
1479
  if (staticObj) {
1488
- replacements.push({
1489
- start: node.span.start - baseByteOffset,
1490
- end: node.span.end - baseByteOffset,
1491
- content: `(${JSON.stringify(staticObj)})`,
1492
- });
1480
+ pushReplacement(`(${JSON.stringify(staticObj)})`);
1493
1481
  }
1494
1482
  }
1495
1483
  },
@@ -1782,12 +1770,27 @@ const unpluginFactory = (options = {}, unpluginMeta) => {
1782
1770
  }
1783
1771
  },
1784
1772
  });
1773
+ const buildExportedInit = (info) => {
1774
+ const keys = Object.keys(info.obj);
1775
+ if (!isDev || keys.length === 0) {
1776
+ return JSON.stringify('');
1777
+ }
1778
+ const head = JSON.stringify(`Plumeria: "${info.name}.`);
1779
+ const tail = JSON.stringify(`" was read at runtime. The file that read it was not compiled ` +
1780
+ `because it does not reference "@plumeria/core" — add ` +
1781
+ `import '@plumeria/core'; to it (see the stack below). Defined in ` +
1782
+ `${path.relative(viteRoot, baseId)}.`);
1783
+ return (`(()=>{const k=new Set(${JSON.stringify(keys)});` +
1784
+ `return new Proxy({},{get(t,p){` +
1785
+ `if(typeof p==="string"&&k.has(p))throw new Error(${head}+p+${tail});` +
1786
+ `return t[p];}});})()`);
1787
+ };
1785
1788
  Object.values(localCreateStyles).forEach((info) => {
1786
1789
  if (info.isExported) {
1787
1790
  replacements.push({
1788
1791
  start: info.initSpan.start,
1789
1792
  end: info.initSpan.end,
1790
- content: JSON.stringify(''),
1793
+ content: buildExportedInit(info),
1791
1794
  });
1792
1795
  }
1793
1796
  else {
package/dist/core.mjs CHANGED
@@ -2,7 +2,7 @@ import { createFilter } from '@rollup/pluginutils';
2
2
  import { parseSync } from '@swc/core';
3
3
  import * as path from 'path';
4
4
  import { applyCssValue, genBase36Hash, exceptionCamelCase, camelToKebabCase, isAtRule, } from 'zss-engine';
5
- import { traverse, getStyleRecords, collectLocalConsts, objectExpressionToObject, t, getRootIdentifier, extractOndemandStyles, deepMerge, scanAll, resolveImportPath, getLeadingCommentLength, optimizer, getFileDependencies, resolveExport, } from '@plumeria/utils';
5
+ import { traverse, collectReferenceIdentifiers, getStyleRecords, collectLocalConsts, objectExpressionToObject, t, getRootIdentifier, extractOndemandStyles, deepMerge, scanAll, resolveImportPath, getLeadingCommentLength, optimizer, getFileDependencies, resolveExport, } from '@plumeria/utils';
6
6
  export const TARGET_EXTENSIONS = ['ts', 'tsx', 'js', 'jsx'];
7
7
  export const EXTENSION_PATTERN = /\.(ts|tsx|js|jsx)$/;
8
8
  export const unpluginFactory = (options = {}, unpluginMeta) => {
@@ -346,17 +346,7 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
346
346
  const processedDecls = new Set();
347
347
  const idSpans = new Set();
348
348
  const excludedSpans = new Set();
349
- const checkVariantAssignment = (decl) => {
350
- const init = decl.init;
351
- if (init && t.isCallExpression(init) && t.isIdentifier(init.callee)) {
352
- const varName = init.callee.value;
353
- if ((localCreateStyles[varName] &&
354
- localCreateStyles[varName].type === 'variant') ||
355
- mergedVariantsTable[varName]) {
356
- throwCompilationError(`Plumeria: Assigning the return value of css.variants() to a variable is not supported.\nPlease pass the variant function directly to styleName or css.use(). Found assignment to: ${t.isIdentifier(decl.id) ? decl.id.value : 'unknown'}`, init);
357
- }
358
- }
359
- };
349
+ const referenceIdents = collectReferenceIdentifiers(ast);
360
350
  const registerStyle = (node, declSpan, isExported) => {
361
351
  let propName;
362
352
  const init = node.init;
@@ -582,7 +572,6 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
582
572
  if (t.isVariableDeclaration(node.declaration)) {
583
573
  processedDecls.add(node.declaration);
584
574
  node.declaration.declarations.forEach((decl) => {
585
- checkVariantAssignment(decl);
586
575
  registerStyle(decl, node.span, true);
587
576
  checkStyleAliasAssignment(decl);
588
577
  });
@@ -592,7 +581,6 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
592
581
  if (processedDecls.has(node))
593
582
  return;
594
583
  node.declarations.forEach((decl) => {
595
- checkVariantAssignment(decl);
596
584
  registerStyle(decl, node.span, false);
597
585
  checkStyleAliasAssignment(decl);
598
586
  });
@@ -728,10 +716,17 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
728
716
  else if (t.isMemberExpression(expr) &&
729
717
  t.isIdentifier(expr.object) &&
730
718
  (t.isIdentifier(expr.property) || expr.property.type === 'Computed')) {
731
- if (expr.property.type === 'Computed')
732
- return null;
733
719
  const varName = expr.object.value;
734
- const propName = expr.property.value;
720
+ let propName;
721
+ if (expr.property.type === 'Computed') {
722
+ const keyExpr = expr.property.expression;
723
+ if (!t.isStringLiteral(keyExpr))
724
+ return null;
725
+ propName = keyExpr.value;
726
+ }
727
+ else {
728
+ propName = expr.property.value;
729
+ }
735
730
  const styleInfo = localCreateStyles[varName];
736
731
  if (styleInfo?.obj[propName]) {
737
732
  const style = styleInfo.obj[propName];
@@ -841,6 +836,14 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
841
836
  else if (node.type === 'ParenthesisExpression') {
842
837
  return collectConditions(node.expression, currentTestStrings);
843
838
  }
839
+ if (currentTestStrings.length > 0 &&
840
+ t.isMemberExpression(node) &&
841
+ t.isIdentifier(node.object) &&
842
+ node.property.type === 'Computed' &&
843
+ resolveCreateObject(node.object.value)) {
844
+ const varName = node.object.value;
845
+ throwCompilationError(`Plumeria: "${getSource(node)}" cannot be used inside a condition, because its bracket key is not a literal.\nMove the condition into the brackets instead, e.g. ${varName}[cond ? 'a' : 'b'].`, node);
846
+ }
844
847
  assertResolvable(node);
845
848
  return false;
846
849
  };
@@ -1324,17 +1327,6 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1324
1327
  return;
1325
1328
  }
1326
1329
  }
1327
- if (localStyle && localStyle.type === 'variant') {
1328
- const variantMap = localStyle.hashMap[propName];
1329
- if (variantMap) {
1330
- replacements.push({
1331
- start: node.span.start - baseByteOffset,
1332
- end: node.span.end - baseByteOffset,
1333
- content: `(${JSON.stringify(variantMap)})`,
1334
- });
1335
- return;
1336
- }
1337
- }
1338
1330
  let hash = scannedTables.createHashTable[uniqueKey];
1339
1331
  if (!hash) {
1340
1332
  hash = mergedCreateTable[varName];
@@ -1395,13 +1387,21 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1395
1387
  return;
1396
1388
  if (idSpans.has(node.span.start))
1397
1389
  return;
1398
- const styleInfo = localCreateStyles[node.value];
1399
- if (styleInfo) {
1390
+ if (!referenceIdents.references.has(node.span.start))
1391
+ return;
1392
+ const prefix = referenceIdents.shorthands.has(node.span.start)
1393
+ ? `${node.value}: `
1394
+ : '';
1395
+ const pushReplacement = (content) => {
1400
1396
  replacements.push({
1401
1397
  start: node.span.start - baseByteOffset,
1402
1398
  end: node.span.end - baseByteOffset,
1403
- content: `(${JSON.stringify(styleInfo.hashMap)})`,
1399
+ content: `${prefix}${content}`,
1404
1400
  });
1401
+ };
1402
+ const styleInfo = localCreateStyles[node.value];
1403
+ if (styleInfo) {
1404
+ pushReplacement(`(${JSON.stringify(styleInfo.hashMap)})`);
1405
1405
  return;
1406
1406
  }
1407
1407
  const varName = node.value;
@@ -1420,11 +1420,7 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1420
1420
  hashMap[key] = atomicMap[key];
1421
1421
  }
1422
1422
  });
1423
- replacements.push({
1424
- start: node.span.start - baseByteOffset,
1425
- end: node.span.end - baseByteOffset,
1426
- content: `(${JSON.stringify(hashMap)})`,
1427
- });
1423
+ pushReplacement(`(${JSON.stringify(hashMap)})`);
1428
1424
  }
1429
1425
  }
1430
1426
  let themeHash = mergedCreateThemeHashTable[varName];
@@ -1434,11 +1430,7 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1434
1430
  if (themeHash) {
1435
1431
  const atomicMap = scannedTables.createAtomicMapTable[themeHash];
1436
1432
  if (atomicMap) {
1437
- replacements.push({
1438
- start: node.span.start - baseByteOffset,
1439
- end: node.span.end - baseByteOffset,
1440
- content: `(${JSON.stringify(atomicMap)})`,
1441
- });
1433
+ pushReplacement(`(${JSON.stringify(atomicMap)})`);
1442
1434
  return;
1443
1435
  }
1444
1436
  }
@@ -1449,11 +1441,7 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1449
1441
  if (staticHash) {
1450
1442
  const staticObj = scannedTables.createStaticObjectTable[staticHash];
1451
1443
  if (staticObj) {
1452
- replacements.push({
1453
- start: node.span.start - baseByteOffset,
1454
- end: node.span.end - baseByteOffset,
1455
- content: `(${JSON.stringify(staticObj)})`,
1456
- });
1444
+ pushReplacement(`(${JSON.stringify(staticObj)})`);
1457
1445
  }
1458
1446
  }
1459
1447
  },
@@ -1746,12 +1734,27 @@ export const unpluginFactory = (options = {}, unpluginMeta) => {
1746
1734
  }
1747
1735
  },
1748
1736
  });
1737
+ const buildExportedInit = (info) => {
1738
+ const keys = Object.keys(info.obj);
1739
+ if (!isDev || keys.length === 0) {
1740
+ return JSON.stringify('');
1741
+ }
1742
+ const head = JSON.stringify(`Plumeria: "${info.name}.`);
1743
+ const tail = JSON.stringify(`" was read at runtime. The file that read it was not compiled ` +
1744
+ `because it does not reference "@plumeria/core" — add ` +
1745
+ `import '@plumeria/core'; to it (see the stack below). Defined in ` +
1746
+ `${path.relative(viteRoot, baseId)}.`);
1747
+ return (`(()=>{const k=new Set(${JSON.stringify(keys)});` +
1748
+ `return new Proxy({},{get(t,p){` +
1749
+ `if(typeof p==="string"&&k.has(p))throw new Error(${head}+p+${tail});` +
1750
+ `return t[p];}});})()`);
1751
+ };
1749
1752
  Object.values(localCreateStyles).forEach((info) => {
1750
1753
  if (info.isExported) {
1751
1754
  replacements.push({
1752
1755
  start: info.initSpan.start,
1753
1756
  end: info.initSpan.end,
1754
- content: JSON.stringify(''),
1757
+ content: buildExportedInit(info),
1755
1758
  });
1756
1759
  }
1757
1760
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/unplugin",
3
- "version": "16.4.2",
3
+ "version": "16.5.0",
4
4
  "description": "Universal Plumeria plugin for various build tools",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -89,7 +89,7 @@
89
89
  "dependencies": {
90
90
  "@rollup/pluginutils": "^5.4.0",
91
91
  "unplugin": "^3.0.0",
92
- "@plumeria/utils": "^16.4.2"
92
+ "@plumeria/utils": "^16.5.0"
93
93
  },
94
94
  "devDependencies": {
95
95
  "@swc/core": "1.15.43",