@plumeria/compiler 18.4.0 → 18.4.2

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 +18 -198
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -71,133 +71,16 @@ const isStaticArgValue = (node) => node.type === 'StringLiteral' ||
71
71
  (node.type === 'TemplateLiteral' && node.expressions.length === 0) ||
72
72
  utils_1.t.isIdentifier(node) ||
73
73
  utils_1.t.isMemberExpression(node);
74
- const namedParamsOf = (params, defaults) => {
75
- if (params.length !== 1)
76
- return undefined;
77
- const first = params[0];
78
- const pattern = (first?.pat ?? first);
79
- if (pattern?.type !== 'ObjectPattern')
80
- return undefined;
81
- const named = [];
82
- for (const prop of pattern.properties ?? []) {
83
- if (prop.type === 'AssignmentPatternProperty' && utils_1.t.isIdentifier(prop.key)) {
84
- named.push({ key: prop.key.value, local: prop.key.value });
85
- if (prop.value)
86
- defaults[prop.key.value] = prop.value;
87
- }
88
- else if (prop.type === 'KeyValuePatternProperty' &&
89
- utils_1.t.isIdentifier(prop.key) &&
90
- utils_1.t.isIdentifier(prop.value)) {
91
- named.push({ key: prop.key.value, local: prop.value.value });
92
- }
93
- else if (prop.type === 'KeyValuePatternProperty' &&
94
- utils_1.t.isIdentifier(prop.key) &&
95
- prop.value?.type === 'AssignmentPattern' &&
96
- utils_1.t.isIdentifier(prop.value.left)) {
97
- named.push({ key: String(prop.key.value), local: prop.value.left.value });
98
- defaults[prop.value.left.value] = prop.value.right;
99
- }
100
- else {
101
- return undefined;
102
- }
103
- }
104
- return named.length > 0 ? named : undefined;
105
- };
106
- const isUnitlessProp = (prop) => zss_engine_1.exceptionCamelCase.includes(prop) || prop.startsWith('--');
107
- const collectVarProps = (style, cssVar, props) => {
108
- const reference = `var(${cssVar})`;
109
- for (const [prop, value] of Object.entries(style)) {
110
- if (typeof value === 'string' && value.includes(reference))
111
- props.push(prop);
112
- else if (value !== null && typeof value === 'object')
113
- collectVarProps(value, cssVar, props);
114
- }
115
- };
116
- const retargetVar = (style, cssVar, next, unitless) => {
117
- const reference = `var(${cssVar})`;
118
- for (const [prop, value] of Object.entries(style)) {
119
- if (typeof value === 'string' && value.includes(reference)) {
120
- if (isUnitlessProp(prop) === unitless)
121
- style[prop] = value
122
- .split(reference)
123
- .join(`var(${next})`);
124
- }
125
- else if (value !== null && typeof value === 'object') {
126
- retargetVar(value, cssVar, next, unitless);
127
- }
128
- }
129
- };
130
- const splitVarByUnit = (style, cssVar) => {
131
- const props = [];
132
- collectVarProps(style, cssVar, props);
133
- if (props.length === 0)
134
- return [];
135
- const lead = props[0];
136
- const split = props.find((prop) => isUnitlessProp(prop) !== isUnitlessProp(lead));
137
- if (!split)
138
- return [{ cssVar, prop: lead }];
139
- const splitVar = `${cssVar}-${(0, zss_engine_1.camelToKebabCase)(split).replace(/^-+/, '')}`;
140
- retargetVar(style, cssVar, splitVar, isUnitlessProp(split));
141
- return [
142
- { cssVar, prop: lead },
143
- { cssVar: splitVar, prop: split },
144
- ];
145
- };
146
- const applyVarFallback = (style, cssVar, literal) => {
147
- const reference = `var(${cssVar})`;
148
- for (const [prop, value] of Object.entries(style)) {
149
- if (typeof value === 'string' && value.includes(reference)) {
150
- style[prop] = value
151
- .split(reference)
152
- .join(`var(${cssVar}, ${(0, zss_engine_1.applyCssValue)(literal, (0, zss_engine_1.camelToKebabCase)(prop))})`);
153
- }
154
- else if (value !== null && typeof value === 'object') {
155
- applyVarFallback(value, cssVar, literal);
156
- }
157
- }
158
- };
159
- const styleFunctionsOf = (objExpr) => {
160
- const styleFunctions = {};
161
- objExpr.properties.forEach((prop) => {
162
- if (prop.type !== 'KeyValueProperty' || prop.key.type !== 'Identifier')
163
- return;
164
- const func = prop.value;
165
- if (func.type !== 'ArrowFunctionExpression' &&
166
- func.type !== 'FunctionExpression')
167
- return;
168
- const defaults = {};
169
- const params = func.params.map((p) => {
170
- const pattern = (typeof p === 'object' && p !== null && 'pat' in p ? p.pat : p);
171
- if (utils_1.t.isIdentifier(pattern))
172
- return pattern.value;
173
- if (pattern?.type === 'AssignmentPattern' &&
174
- utils_1.t.isIdentifier(pattern.left)) {
175
- defaults[pattern.left.value] = pattern.right;
176
- return pattern.left.value;
177
- }
178
- return 'arg';
179
- });
180
- let actualBody = func.body;
181
- if (actualBody?.type === 'ParenthesisExpression')
182
- actualBody = actualBody.expression;
183
- if (actualBody?.type === 'BlockStatement') {
184
- const first = actualBody.stmts?.[0];
185
- if (first?.type === 'ReturnStatement')
186
- actualBody = first.argument;
187
- if (actualBody?.type === 'ParenthesisExpression')
188
- actualBody = actualBody.expression;
189
- }
190
- if (actualBody && actualBody.type === 'ObjectExpression') {
191
- styleFunctions[prop.key.value] = {
192
- params,
193
- named: namedParamsOf(func.params, defaults),
194
- defaults,
195
- body: actualBody,
196
- };
197
- }
198
- });
199
- return styleFunctions;
200
- };
74
+ const dynamicTablesOf = (ctx) => ({
75
+ keyframesHashTable: ctx.mergedKeyframesTable,
76
+ viewTransitionHashTable: ctx.mergedViewTransitionTable,
77
+ createThemeHashTable: ctx.mergedCreateThemeHashTable,
78
+ createThemeObjectTable: ctx.scannedTables.createThemeObjectTable,
79
+ createHashTable: ctx.mergedCreateTable,
80
+ createStaticHashTable: ctx.mergedCreateStaticHashTable,
81
+ createStaticObjectTable: ctx.scannedTables.createStaticObjectTable,
82
+ variantsHashTable: ctx.mergedVariantsTable,
83
+ });
201
84
  function extractStylesFromExpression(expression, ctx) {
202
85
  let expr = expression;
203
86
  if (ctx.localStyleAliases &&
@@ -379,7 +262,7 @@ function compileCSS(options) {
379
262
  createImportMap[localName] =
380
263
  scannedTables.createHashTable[uniqueKey];
381
264
  if (scannedTables.createFunctionTable[uniqueKey])
382
- createFunctionImportMap[localName] = styleFunctionsOf(scannedTables.createFunctionTable[uniqueKey]);
265
+ createFunctionImportMap[localName] = (0, utils_1.styleFunctionsOf)(scannedTables.createFunctionTable[uniqueKey]);
383
266
  if (scannedTables.variantsHashTable[uniqueKey])
384
267
  variantsImportMap[localName] =
385
268
  scannedTables.variantsHashTable[uniqueKey];
@@ -554,7 +437,7 @@ function compileCSS(options) {
554
437
  const records = (0, utils_1.getStyleRecords)(style);
555
438
  records.forEach((r) => extractedSheets.push(r.sheet));
556
439
  };
557
- const extractAndProcessConditionals = (args, isStyleProp = false) => {
440
+ const extractAndProcessConditionals = (args) => {
558
441
  args.forEach((arg) => {
559
442
  if (utils_1.t.isIdentifier(arg.expression) &&
560
443
  localStyleAliases[arg.expression.value]) {
@@ -726,36 +609,10 @@ function compileCSS(options) {
726
609
  runtime.push(p);
727
610
  });
728
611
  }
729
- const withDefault = Object.entries(func.defaults ?? {}).filter(([param]) => tempStaticTable[param] === undefined);
730
- const cssVars = {};
731
- const varParams = Array.from(new Set([...runtime, ...withDefault.map(([param]) => param)]));
732
- if (varParams.length > 0) {
733
- varParams.forEach((p) => (tempStaticTable[p] = p));
734
- const probe = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
735
- const hash = (0, zss_engine_1.genBase36Hash)(probe ?? {}, 1, 8);
736
- varParams.forEach((p) => {
737
- const cssVar = `--${hash}-${p}`;
738
- tempStaticTable[p] = `var(${cssVar})`;
739
- cssVars[p] = cssVar;
740
- });
741
- }
742
- const style = (0, utils_1.objectExpressionToObject)(func.body, tempStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable);
743
- if (!style)
612
+ const resolved = (0, utils_1.resolveDynamicStyle)(func, runtime, tempStaticTable, dynamicTablesOf(ctx));
613
+ if (!resolved)
744
614
  return null;
745
- const varGroups = new Map();
746
- varParams.forEach((param) => {
747
- const cssVar = cssVars[param];
748
- if (cssVar)
749
- varGroups.set(param, splitVarByUnit(style, cssVar));
750
- });
751
- withDefault.forEach(([param, expr]) => {
752
- const literal = expr.type === 'StringLiteral' || expr.type === 'NumericLiteral'
753
- ? expr.value
754
- : undefined;
755
- if (literal === undefined)
756
- return;
757
- (varGroups.get(param) ?? []).forEach(({ cssVar }) => applyVarFallback(style, cssVar, literal));
758
- });
615
+ const { style } = resolved;
759
616
  return style;
760
617
  };
761
618
  const collectConditions = (node, currentTestStrings = []) => {
@@ -827,44 +684,7 @@ function compileCSS(options) {
827
684
  assertResolvable(node);
828
685
  return false;
829
686
  };
830
- const checkFunctionKey = (node) => {
831
- if (isStyleProp)
832
- return;
833
- if (utils_1.t.isCallExpression(node) &&
834
- utils_1.t.isMemberExpression(node.callee) &&
835
- utils_1.t.isIdentifier(node.callee.object) &&
836
- utils_1.t.isIdentifier(node.callee.property)) {
837
- const varName = node.callee.object.value;
838
- const propKey = node.callee.property.value;
839
- const styleInfo = ctx.localCreateStyles[varName];
840
- const atomMap = styleInfo?.obj[propKey];
841
- if (typeof atomMap === 'object' &&
842
- atomMap !== null &&
843
- '__cssVars__' in atomMap) {
844
- throw new Error(`[plumeria] css.use(${getSource(node)}) cannot handle dynamic style functions. Use ${styleProp} instead.\n`);
845
- }
846
- }
847
- if (node.type === 'ConditionalExpression') {
848
- checkFunctionKey(node.consequent);
849
- checkFunctionKey(node.alternate);
850
- }
851
- else if (node.type === 'BinaryExpression' &&
852
- ['&&', '||', '??'].includes(node.operator)) {
853
- checkFunctionKey(node.left);
854
- checkFunctionKey(node.right);
855
- }
856
- else if (node.type === 'ParenthesisExpression') {
857
- checkFunctionKey(node.expression);
858
- }
859
- else if (node.type === 'ArrayExpression') {
860
- for (const el of node.elements) {
861
- if (el && el.expression)
862
- checkFunctionKey(el.expression);
863
- }
864
- }
865
- };
866
687
  for (const arg of args) {
867
- checkFunctionKey(arg.expression);
868
688
  const expr = arg.expression;
869
689
  if (utils_1.t.isIdentifier(expr) ||
870
690
  (utils_1.t.isMemberExpression(expr) &&
@@ -949,7 +769,7 @@ function compileCSS(options) {
949
769
  if (propName) {
950
770
  const args = node.arguments;
951
771
  if (propName === 'use') {
952
- extractAndProcessConditionals(args, false);
772
+ extractAndProcessConditionals(args);
953
773
  }
954
774
  else if (propName === 'keyframes' &&
955
775
  args.length > 0 &&
@@ -1035,7 +855,7 @@ function compileCSS(options) {
1035
855
  if (pName === 'create') {
1036
856
  const obj = (0, utils_1.objectExpressionToObject)(arg, ctx.mergedStaticTable, ctx.mergedKeyframesTable, ctx.mergedViewTransitionTable, ctx.mergedCreateThemeHashTable, ctx.scannedTables.createThemeObjectTable, ctx.mergedCreateTable, ctx.mergedCreateStaticHashTable, ctx.scannedTables.createStaticObjectTable, ctx.mergedVariantsTable, resolveVariable);
1037
857
  if (obj) {
1038
- const styleFunctions = styleFunctionsOf(arg);
858
+ const styleFunctions = (0, utils_1.styleFunctionsOf)(arg);
1039
859
  ctx.localCreateStyles[node.id.value] = {
1040
860
  type: 'create',
1041
861
  obj,
@@ -1150,7 +970,7 @@ function compileCSS(options) {
1150
970
  .filter((el) => el !== undefined)
1151
971
  .map((el) => ({ expression: el.expression }))
1152
972
  : [{ expression: expr }];
1153
- extractAndProcessConditionals(args, true);
973
+ extractAndProcessConditionals(args);
1154
974
  },
1155
975
  });
1156
976
  return extractedSheets;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "18.4.0",
3
+ "version": "18.4.2",
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": "^18.4.0"
24
+ "@plumeria/utils": "^18.4.2"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@rust-gear/glob": "1.1.1",