sd-transforms: [Bug]: References across files when expanding composite tokens / addFontStyle

What happened?

I have the following file structure:

  • core.json -> all private/primitive values
  • base.json -> semantic naming layer referrencing core tokens
  • components/**.json -> Sets with component specific tokens referrencing base tokens

We’re using typography tokens to more easily work with typography, but for the code built tokens i want to expand the typography tokens for the component sets, but somehow I’m having trouble resolving some references:

Warning: could not resolve reference {core.font-weight.normal}
Warning: could not resolve reference {base.typography.body}
                                                           
scss                                                       
- dist/scss/tokens.scss                                    
                                                           
scss                                                       
✔︎ dist/scss/tokens.scss     

It manages to expand the typography token in the base layer, but not in the component layer:

$base-typography-body-font-family: 'Open Sans';
$base-typography-body-font-weight: Regular;
$base-typography-body-line-height: 1.5;
$base-typography-body-font-size: 14px;
$base-typography-body-text-decoration: none;
$body: [object Object];

If i toggle the expand option to false in the build config, it builds the tokens correctly:

$base-typography-body: 400 14px/1.5 'Open Sans';
$comp-announcement-typography-body: 400 14px/1.5 'Open Sans';

Reproduction

A reproduction of the issue can be found here

Expected output

I want the typography tokens in the component sets to expand properly aswell

Version

0.11.9

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

Unfortunately, I cannot fix this bug right now without first introducing a new feature to Style-Dictionary.

I have created a PR to style-dictionary for Preprocessors which will allow us to change our Tokens Studio -> Style-Dictionary custom parsers (works on a per file basis) to preprocessors (works on the merged dictionary as a whole) to ensure that this bug gets resolved in sd-transforms. https://github.com/amzn/style-dictionary/pull/1043/

I’ll try to give this feature high prio, as this bug is pretty annoying to get and hard to work around. Only way to work around it for now is to not have cross-file references in composite tokens that get expanded.

import { expandComposites } from '@tokens-studio/sd-transforms';
import StyleDictionary from 'style-dictionary';

const sd = StyleDictionary.extend({ source: ['**/*.json'] })

sd.tokens = expandComposites(sd.tokens, { expand: { typography: true } });
sd.properties = sd.tokens;

this workaround should do it for v3 btw, just to unblock people. It runs the typography expand processing step on the full dictionary after the parsing step.

You can replace expandComposites for addFontStyles instead if you want to run the font styles step on it.

Once you’re on style-dictionary v4.0.0-prerelease.2 minimum, and also the upcoming minor release of sd-transforms (after which I will be able to close this issue), you should be able to delete the workaround.

As long as the StyleDictionary instance that you pass to registerTransforms() function has version v4.0.0-prerelease.2 or higher, this issue is now fixed in sd-transforms v0.13.0 😃

Smallest repro

Seems related to references across multiple files, since in a single file it does work. I suppose the expand typography parser is trying to resolve references inside only the current file, but not across the whole dictionary of combined JSONs.

Affected use cases, when having cross-file references in:

  • Typography tokens (assuming expanding them is enabled)
  • Composition tokens (assuming expanding them is enabled)
  • Border tokens (assuming expanding them is enabled)
  • Shadow tokens (assuming expanding them is enabled)
  • Typography tokens with fontWeight prop that combines fontWeight + fontStyle both inside fontWeight prop, and trying to expand into two separate props. This happens even when not expanding the typography tokens as a whole, and when alwaysAddFontStyle is set to true, fontWeights without a fontStyle inside are also affected, as you would usually see a fontStyle normal prop but not with the cross-file ref.