@plumeria/compiler 0.26.0 → 0.27.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 (2) hide show
  1. package/dist/extract.js +51 -5
  2. package/package.json +1 -1
package/dist/extract.js CHANGED
@@ -158,7 +158,42 @@ function expressionToString(expr) {
158
158
  console.warn(`css.props: Argument unsupported ${expr.type}: Use css.create instead.`);
159
159
  return '';
160
160
  }
161
- async function extractCssProps(ast, code) {
161
+ async function extractCreatedStyleKeys(ast) {
162
+ const createdStylesMap = new Map();
163
+ await visit(ast, {
164
+ VariableDeclarator: (node) => {
165
+ if (node.id.type === 'Identifier' &&
166
+ node.init &&
167
+ node.init.type === 'CallExpression' &&
168
+ node.init.callee.type === 'MemberExpression' &&
169
+ node.init.callee.object.type === 'Identifier' &&
170
+ node.init.callee.object.value === 'css' &&
171
+ node.init.callee.property.type === 'Identifier' &&
172
+ node.init.callee.property.value === 'create' &&
173
+ node.init.arguments.length > 0) {
174
+ const variableName = node.id.value;
175
+ const createArg = node.init.arguments[0].expression;
176
+ if (createArg && createArg.type === 'ObjectExpression') {
177
+ const keys = [];
178
+ for (const prop of createArg.properties) {
179
+ if (prop.type === 'KeyValueProperty' &&
180
+ prop.key.type === 'Identifier') {
181
+ keys.push(prop.key.value);
182
+ }
183
+ else if (prop.type === 'Identifier') {
184
+ keys.push(prop.value);
185
+ }
186
+ }
187
+ if (keys.length > 0) {
188
+ createdStylesMap.set(variableName, keys);
189
+ }
190
+ }
191
+ }
192
+ },
193
+ });
194
+ return createdStylesMap;
195
+ }
196
+ async function extractCssProps(ast, code, createdStylesMap) {
162
197
  const propsMatches = [];
163
198
  if (code && !code.includes('css.props')) {
164
199
  return propsMatches;
@@ -178,9 +213,18 @@ async function extractCssProps(ast, code) {
178
213
  if (arg.expression.type === 'ConditionalExpression' ||
179
214
  (arg.expression.type === 'BinaryExpression' &&
180
215
  arg.expression.operator === '&&')) {
181
- const styles = await extractStyleObjectsFromExpression(arg.expression);
216
+ const styles = extractStyleObjectsFromExpression(arg.expression);
182
217
  conditionalStyleObjects.push(...styles);
183
218
  }
219
+ else if (arg.expression.type === 'MemberExpression' &&
220
+ arg.expression.property.type === 'Computed' &&
221
+ arg.expression.object.type === 'Identifier') {
222
+ const styleVarName = arg.expression.object.value;
223
+ const styleKeys = createdStylesMap.get(styleVarName);
224
+ if (styleKeys) {
225
+ propsMatches.push(...styleKeys.map((key) => `css.props(${styleVarName}.${key})`));
226
+ }
227
+ }
184
228
  else {
185
229
  const argStr = expressionToString(arg.expression);
186
230
  if (argStr) {
@@ -207,7 +251,7 @@ async function extractCssProps(ast, code) {
207
251
  catch (e) {
208
252
  console.error(`Failed to parse code to extract css.props: ${e}`);
209
253
  }
210
- return propsMatches;
254
+ return [...new Set(propsMatches)];
211
255
  }
212
256
  function extractStyleObjectsFromExpression(expression) {
213
257
  switch (expression.type) {
@@ -415,7 +459,8 @@ async function extractVueAndSvelte(filePath) {
415
459
  syntax: 'typescript',
416
460
  tsx: true,
417
461
  });
418
- const propsFromScript = await extractCssProps(ast, code);
462
+ const createdStylesMap = await extractCreatedStyleKeys(ast);
463
+ const propsFromScript = await extractCssProps(ast, code, createdStylesMap);
419
464
  const propsFromTemplate = extractCssPropsFromTemplate(code);
420
465
  const propsMatches = [...new Set([...propsFromScript, ...propsFromTemplate])];
421
466
  const calls = propsMatches
@@ -455,6 +500,7 @@ async function extractTSFile(filePath) {
455
500
  syntax: 'typescript',
456
501
  tsx: true,
457
502
  });
503
+ const createdStylesMap = await extractCreatedStyleKeys(ast);
458
504
  const importSection = await extractImportDeclarations(ast);
459
505
  const staticVariableSection = await extractStaticStringLiteralVariable(ast);
460
506
  const cssCreateSection = await extractCssMethod(ast, 'create', code);
@@ -462,7 +508,7 @@ async function extractTSFile(filePath) {
462
508
  const cssViewTransitionSection = await extractCssMethod(ast, 'viewTransition', code);
463
509
  const cssDefineConstsSection = await extractCssMethod(ast, 'defineConsts', code);
464
510
  const cssDefineTokensSection = await extractCssMethod(ast, 'defineTokens', code);
465
- const propsMatches = await extractCssProps(ast, code);
511
+ const propsMatches = await extractCssProps(ast, code, createdStylesMap);
466
512
  const calls = propsMatches
467
513
  .filter(Boolean)
468
514
  .map((call) => `${call};`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plumeria/compiler",
3
- "version": "0.26.0",
3
+ "version": "0.27.0",
4
4
  "description": "Plumeria Rust-based compiler",
5
5
  "repository": {
6
6
  "type": "git",