@plumeria/compiler 19.1.7 → 19.1.8

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 (2) hide show
  1. package/dist/index.js +38 -5
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -270,6 +270,7 @@ function compileCSS(options) {
270
270
  const plumeriaAliases = {};
271
271
  const localImports = {};
272
272
  const localStyleAliases = {};
273
+ const registeredStyleCalls = new Set();
273
274
  (0, utils_1.traverse)(ast, {
274
275
  ImportDeclaration({ node }) {
275
276
  const sourcePath = node.source.value;
@@ -514,7 +515,7 @@ function compileCSS(options) {
514
515
  const records = (0, utils_1.getStyleRecords)(style);
515
516
  records.forEach((r) => extractedSheets.push(r.sheet));
516
517
  };
517
- const extractAndProcessConditionals = (args) => {
518
+ const extractAndProcessConditionals = (args, isStyleProp) => {
518
519
  args.forEach((arg) => {
519
520
  arg.expression = resolveLocalStyleAlias(localStyleAliases, arg.expression);
520
521
  });
@@ -768,6 +769,14 @@ function compileCSS(options) {
768
769
  return true;
769
770
  return Boolean(isPropsMember || (owner && declaredProps.get(owner)?.has(propName)));
770
771
  };
772
+ const resolveDynamicStyleHere = (node) => {
773
+ const dynamic = resolveDynamicCall(node);
774
+ if (dynamic && !isStyleProp) {
775
+ throw new Error(`[plumeria] css.use(${getSource(node)}) does not support dynamic function keys. ` +
776
+ `(${path.basename(resourcePath)})`);
777
+ }
778
+ return dynamic;
779
+ };
771
780
  const collectConditions = (node, currentTestStrings = []) => {
772
781
  node = resolveLocalStyleAlias(localStyleAliases, (0, utils_1.unwrapExpression)(node));
773
782
  if (node.type === 'ArrayExpression') {
@@ -818,7 +827,7 @@ function compileCSS(options) {
818
827
  }
819
828
  if (collectPropStyles(node))
820
829
  return true;
821
- const staticStyle = resolveStyleObject(node) ?? resolveDynamicCall(node);
830
+ const staticStyle = resolveStyleObject(node) ?? resolveDynamicStyleHere(node);
822
831
  if (staticStyle) {
823
832
  if (currentTestStrings.length === 0) {
824
833
  baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
@@ -854,7 +863,7 @@ function compileCSS(options) {
854
863
  const expr = arg.expression;
855
864
  if (collectPropStyles(expr))
856
865
  continue;
857
- const dynamicStyle = resolveDynamicCall(expr);
866
+ const dynamicStyle = resolveDynamicStyleHere(expr);
858
867
  if (dynamicStyle) {
859
868
  baseStyle = (0, utils_1.deepMerge)(baseStyle, dynamicStyle);
860
869
  continue;
@@ -895,9 +904,32 @@ function compileCSS(options) {
895
904
  propName = originalName;
896
905
  }
897
906
  if (propName) {
907
+ if (['create', 'createTheme', 'createStatic'].includes(propName) &&
908
+ !registeredStyleCalls.has(node.span.start)) {
909
+ throw new Error(`[plumeria] css.${propName} must be assigned to a named top-level variable. ` +
910
+ `Destructuring, assignment statements, and default-exported calls cannot be compiled. ` +
911
+ `(${path.basename(resourcePath)})`);
912
+ }
898
913
  const args = node.arguments;
899
914
  if (propName === 'use') {
900
- extractAndProcessConditionals(args);
915
+ const namesStyleFunction = (expr) => {
916
+ const node = (0, utils_1.unwrapExpression)(expr);
917
+ if (node.type === 'ArrayExpression') {
918
+ return (node.elements ?? []).some((element) => !!element && namesStyleFunction(element.expression));
919
+ }
920
+ if (!utils_1.t.isMemberExpression(node) ||
921
+ !utils_1.t.isIdentifier(node.object) ||
922
+ !utils_1.t.isIdentifier(node.property)) {
923
+ return false;
924
+ }
925
+ return Boolean(ctx.localCreateStyles[node.object.value]?.functions?.[node.property.value] ??
926
+ ctx.createFunctionImportMap[node.object.value]?.[node.property.value]);
927
+ };
928
+ if (args.some((arg) => namesStyleFunction(arg.expression))) {
929
+ throw new Error(`[plumeria] Dynamic or unresolvable style object "${sourceOf(node, sourceBuffer, baseByteOffset)}" is not supported. ` +
930
+ `(${path.basename(resourcePath)})`);
931
+ }
932
+ extractAndProcessConditionals(args, false);
901
933
  }
902
934
  else if (propName === 'keyframes' &&
903
935
  args.length > 0 &&
@@ -980,6 +1012,7 @@ function compileCSS(options) {
980
1012
  utils_1.t.isObjectExpression(unwrappedDefinitionArg) &&
981
1013
  ((!isTheme && init.arguments.length === 1) ||
982
1014
  (isTheme && init.arguments.length >= 2))) {
1015
+ registeredStyleCalls.add(init.span.start);
983
1016
  const arg = unwrappedDefinitionArg;
984
1017
  const resolveVariable = (name) => ctx.localCreateStyles[name]?.obj ||
985
1018
  (ctx.mergedCreateThemeHashTable[name]
@@ -1120,7 +1153,7 @@ function compileCSS(options) {
1120
1153
  }
1121
1154
  };
1122
1155
  addArgs(expr);
1123
- extractAndProcessConditionals(args);
1156
+ extractAndProcessConditionals(args, true);
1124
1157
  },
1125
1158
  });
1126
1159
  return extractedSheets;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "19.1.7",
3
+ "version": "19.1.8",
4
4
  "description": "Plumeria swc based compiler for statically extracting css",
5
5
  "author": "Refirst 11",
6
6
  "license": "MIT",
@@ -21,7 +21,7 @@
21
21
  "dist/"
22
22
  ],
23
23
  "dependencies": {
24
- "@plumeria/utils": "^19.1.7"
24
+ "@plumeria/utils": "^19.1.8"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@rust-gear/glob": "1.2.1",