@plumeria/compiler 19.1.6 → 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 +46 -9
  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;
@@ -292,6 +293,13 @@ function compileCSS(options) {
292
293
  const actualPath = (0, utils_1.resolveImportPath)(sourcePath, resourcePath);
293
294
  if (actualPath) {
294
295
  node.specifiers.forEach((specifier) => {
296
+ if (specifier.type === 'ImportNamespaceSpecifier') {
297
+ localImports[specifier.local.value] = {
298
+ actualPath,
299
+ importedName: '*',
300
+ };
301
+ return;
302
+ }
295
303
  if (specifier.type === 'ImportSpecifier' ||
296
304
  specifier.type === 'ImportDefaultSpecifier') {
297
305
  const importedName = specifier.type === 'ImportDefaultSpecifier'
@@ -507,7 +515,7 @@ function compileCSS(options) {
507
515
  const records = (0, utils_1.getStyleRecords)(style);
508
516
  records.forEach((r) => extractedSheets.push(r.sheet));
509
517
  };
510
- const extractAndProcessConditionals = (args) => {
518
+ const extractAndProcessConditionals = (args, isStyleProp) => {
511
519
  args.forEach((arg) => {
512
520
  arg.expression = resolveLocalStyleAlias(localStyleAliases, arg.expression);
513
521
  });
@@ -591,10 +599,7 @@ function compileCSS(options) {
591
599
  (ctx.localCreateStyles[rootId] !== undefined ||
592
600
  ctx.mergedCreateTable[rootId] !== undefined);
593
601
  if (!isPlumeriaStyle) {
594
- const origin = rootId ? localImports[rootId] : undefined;
595
- const failure = origin
596
- ? (0, utils_1.resolveFileError)(origin.actualPath, origin.importedName)
597
- : undefined;
602
+ const failure = (0, utils_1.resolveOriginError)(node, rootId, localImports);
598
603
  throw new Error(failure
599
604
  ? `[plumeria] ${failure.message} (${path.basename(failure.filePath)})`
600
605
  : `[plumeria] Dynamic or unresolvable style object "${getSource(node)}" is not supported. (${path.basename(resourcePath)})`);
@@ -764,6 +769,14 @@ function compileCSS(options) {
764
769
  return true;
765
770
  return Boolean(isPropsMember || (owner && declaredProps.get(owner)?.has(propName)));
766
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
+ };
767
780
  const collectConditions = (node, currentTestStrings = []) => {
768
781
  node = resolveLocalStyleAlias(localStyleAliases, (0, utils_1.unwrapExpression)(node));
769
782
  if (node.type === 'ArrayExpression') {
@@ -814,7 +827,7 @@ function compileCSS(options) {
814
827
  }
815
828
  if (collectPropStyles(node))
816
829
  return true;
817
- const staticStyle = resolveStyleObject(node) ?? resolveDynamicCall(node);
830
+ const staticStyle = resolveStyleObject(node) ?? resolveDynamicStyleHere(node);
818
831
  if (staticStyle) {
819
832
  if (currentTestStrings.length === 0) {
820
833
  baseStyle = (0, utils_1.deepMerge)(baseStyle, staticStyle);
@@ -850,7 +863,7 @@ function compileCSS(options) {
850
863
  const expr = arg.expression;
851
864
  if (collectPropStyles(expr))
852
865
  continue;
853
- const dynamicStyle = resolveDynamicCall(expr);
866
+ const dynamicStyle = resolveDynamicStyleHere(expr);
854
867
  if (dynamicStyle) {
855
868
  baseStyle = (0, utils_1.deepMerge)(baseStyle, dynamicStyle);
856
869
  continue;
@@ -891,9 +904,32 @@ function compileCSS(options) {
891
904
  propName = originalName;
892
905
  }
893
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
+ }
894
913
  const args = node.arguments;
895
914
  if (propName === 'use') {
896
- 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);
897
933
  }
898
934
  else if (propName === 'keyframes' &&
899
935
  args.length > 0 &&
@@ -976,6 +1012,7 @@ function compileCSS(options) {
976
1012
  utils_1.t.isObjectExpression(unwrappedDefinitionArg) &&
977
1013
  ((!isTheme && init.arguments.length === 1) ||
978
1014
  (isTheme && init.arguments.length >= 2))) {
1015
+ registeredStyleCalls.add(init.span.start);
979
1016
  const arg = unwrappedDefinitionArg;
980
1017
  const resolveVariable = (name) => ctx.localCreateStyles[name]?.obj ||
981
1018
  (ctx.mergedCreateThemeHashTable[name]
@@ -1116,7 +1153,7 @@ function compileCSS(options) {
1116
1153
  }
1117
1154
  };
1118
1155
  addArgs(expr);
1119
- extractAndProcessConditionals(args);
1156
+ extractAndProcessConditionals(args, true);
1120
1157
  },
1121
1158
  });
1122
1159
  return extractedSheets;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "19.1.6",
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.6"
24
+ "@plumeria/utils": "^19.1.8"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@rust-gear/glob": "1.2.1",