figma-tokens-flattener 1.0.16 → 1.0.18

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/consts.js +3 -0
  2. package/index.js +30 -3
  3. package/package.json +1 -1
package/consts.js ADDED
@@ -0,0 +1,3 @@
1
+ const REUSED_KEYS = ["lineType", "fontWeightItalic", "boxShadow", "boxShadowSecondary", "boxShadowTertiary", "focusPrimary", "focusError", "focusWarning", "colorMenuDarkBg", "colorMenuSubItemBg"];
2
+
3
+ module.exports = REUSED_KEYS;
package/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const fs = require('fs');
4
4
  const path = require('path');
5
+ const REUSED_KEYS = require('./consts.js');
5
6
 
6
7
  /* Color */
7
8
  function flattenColorGroups(colorGroups) {
@@ -393,10 +394,16 @@ function flattenComponentsTokens(componentsTokens, contextTokens) {
393
394
  Object.keys(componentTokens).forEach(tokenName => {
394
395
  const tokenDefinition = componentTokens[tokenName];
395
396
 
396
- if (tokenDefinition && typeof tokenDefinition === 'object' && Object.hasOwn(tokenDefinition, 'value')) {
397
- const rawValue = tokenDefinition.value;
397
+ if (tokenDefinition && typeof tokenDefinition === 'object') {
398
+ let rawValueToProcess;
398
399
 
399
- const processedValue = flattenMapTokens(rawValue, contextTokens);
400
+ if (Object.hasOwn(tokenDefinition, 'value') && typeof tokenDefinition.value === 'object' && tokenDefinition.value !== null && Object.hasOwn(tokenDefinition.value, 'style')) {
401
+ rawValueToProcess = tokenDefinition.value.style;
402
+ }
403
+ else if (Object.hasOwn(tokenDefinition, 'value')) {
404
+ rawValueToProcess = tokenDefinition.value;
405
+ }
406
+ const processedValue = flattenMapTokens(rawValueToProcess, contextTokens);
400
407
  processedComponentTokens[tokenName] = processedValue;
401
408
  } else {
402
409
  console.warn(`Unsupported token structure ${componentName}.${tokenName}:`, tokenDefinition);
@@ -410,6 +417,22 @@ function flattenComponentsTokens(componentsTokens, contextTokens) {
410
417
  return flattened;
411
418
  }
412
419
 
420
+ function addReusedTokens(lightTokens, darkTokens) {
421
+ const reusedCollection = {};
422
+
423
+ Object.keys(lightTokens).forEach((key) => {
424
+ const value = lightTokens[key];
425
+
426
+ if (typeof value === 'number' || REUSED_KEYS.includes(key)) {
427
+ reusedCollection[key] = value;
428
+ }
429
+
430
+ });
431
+
432
+ // darkTokens will take precedence in case of a key match.
433
+ return { ...darkTokens, ...reusedCollection };
434
+ }
435
+
413
436
  function flatten() {
414
437
  const configFilePath = path.resolve('./figma-tokens-flattener-config.json');
415
438
  let config = {};
@@ -503,6 +526,10 @@ function flatten() {
503
526
  const flattenDefaultValues = flattenDefaultValueTokens(allTokensData[darkDefaultKey]);
504
527
  darkTokens = { ...flattenDefaultValues, ...darkTokens };
505
528
 
529
+ // The tokens of the light theme contain numeric values, while the dark theme does not contain them to avoid duplication.
530
+ // Need to add these values, and some lines (shadows, focus, etc.) because only the light theme has them too.
531
+ darkTokens = addReusedTokens(lightTokens, darkTokens);
532
+
506
533
  const flattenedComponents = flattenComponentsTokens(allTokensData[darkFullKey], darkTokens);
507
534
  darkTokens = { ...darkTokens, components: flattenedComponents };
508
535
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "figma-tokens-flattener",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "A tool for transforming Ant Design tokens from Tokens Studio for Figma (Single file) into flat style mappings for light and dark themes.",
5
5
  "main": "index.js",
6
6
  "bin": {