Skip to content

Commit ead2954

Browse files
committed
feat: upgrade to v5 and optimize preprocessor fontWeight/styles
1 parent 3b1b5c3 commit ead2954

File tree

10 files changed

+68
-43
lines changed

10 files changed

+68
-43
lines changed

.changeset/orange-grapes-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tokens-studio/sd-transforms': major
3+
---
4+
5+
Upgrade to latest Style Dictionary v5.0.0. The only breaking change is that this version sd-transforms is no longer compatible with v4, due to the preprocessors making direct use of the performance improvements that came with Style Dictionary v5.

package-lock.json

Lines changed: 17 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"is-mergeable-object": "^1.1.1"
4242
},
4343
"peerDependencies": {
44-
"style-dictionary": "^4.3.0 || ^5.0.0-rc.0"
44+
"style-dictionary": "^5.0.0"
4545
},
4646
"devDependencies": {
4747
"@changesets/cli": "^2.27.6",

src/preprocessors/add-font-styles.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,30 @@ import {
44
SingleToken,
55
SingleFontWeightsToken,
66
} from '@tokens-studio/types';
7-
import { usesReferences, resolveReferences } from 'style-dictionary/utils';
7+
import { usesReferences, resolveReferences, convertTokenData } from 'style-dictionary/utils';
8+
import type { TransformedToken } from 'style-dictionary/types';
89
import { fontWeightReg, fontStyles } from '../transformFontWeight.js';
910
import { TransformOptions } from '../TransformOptions.js';
1011

1112
function resolveFontWeight(fontWeight: string, copy: DeepKeyTokenMap<false>, usesDtcg: boolean) {
13+
const tokenMap = convertTokenData(copy, { output: 'map' });
1214
let resolved = fontWeight;
1315
if (usesReferences(fontWeight)) {
1416
try {
15-
resolved = `${resolveReferences(fontWeight, copy, { usesDtcg })}`;
17+
resolved = `${resolveReferences(fontWeight, tokenMap as Map<string, TransformedToken>, { usesDtcg })}`;
1618
} catch (e) {
17-
// dont throw fatal, see: https://github.com/tokens-studio/sd-transforms/issues/217
18-
// we throw once we only support SD v4, for v3 we need to be more permissive
19-
console.error(e);
19+
if (e instanceof Error) {
20+
// create an extended error
21+
const err = new Error(
22+
`tokens-studio preprocessor -> addFontStyles: Failing to resolve references within fontWeight -> ${fontWeight}.\n\n${e.message}`,
23+
);
24+
err.stack = e.stack;
25+
// dont throw fatal, see: https://github.com/tokens-studio/sd-transforms/issues/217
26+
// we throw once we only support SD v4, for v3 we need to be more permissive
27+
// Update: maybe sd-transforms should also be (re-)using the new logger approach that's coming to SD v5 in the future
28+
// that way it's up to users to decide whether they want these to be thrown, error'd or something else.
29+
console.error(err);
30+
}
2031
}
2132
}
2233
return resolved;

test/integration/cross-file-refs.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ describe('cross file references', () => {
4444
it('supports cross file references e.g. expanding typography', async () => {
4545
const file = await promises.readFile(outputFilePath, 'utf-8');
4646
const content = excerpt(file, { start: ':root {', end: '}' });
47-
const expectedOutput = `--sdTypoFontWeight: 400;
48-
--sdTypoFontStyle: italic;
49-
--sdPrimaryFont: Inter;
47+
const expectedOutput = `--sdPrimaryFont: Inter;
5048
--sdFontWeight: 800;
5149
--sdLineHeight: 1.5;
50+
--sdDimensionScale: 2;
51+
--sdDimensionXs: 4px;
52+
--sdWeightWeight: 400;
53+
--sdWeightStyle: italic;
54+
--sdTypoFontWeight: 400;
55+
--sdTypoFontStyle: italic;
5256
--sdTypo2FontFamily: Inter;
5357
--sdTypo2FontWeight: 800;
5458
--sdTypo2LineHeight: 1.5;
5559
--sdTypo2FontSize: 8px;
56-
--sdDimensionScale: 2;
57-
--sdDimensionXs: 4px;
5860
--sdTestCompositeFancyCardColor: #fff;
5961
--sdTestCompositeFancyCardBorderRadius: 18px;
6062
--sdTestCompositeFancyCardBorderColor: #999;
@@ -69,8 +71,6 @@ describe('cross file references', () => {
6971
--sdTestTypographyTextFontSize: 25px;
7072
--sdTestTypographyTextLineHeight: 32px;
7173
--sdTestTypographyTextFontWeight: 700;
72-
--sdWeightWeight: 400;
73-
--sdWeightStyle: italic;
7474
--sdTypoAliasFontWeight: 400;
7575
--sdTypoAliasFontStyle: italic;
7676
--sdTypo3FontFamily: Inter;

test/integration/expand-composition.test.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,27 @@ describe('expand composition tokens', () => {
6565
const file = await promises.readFile(outputFilePath, 'utf-8');
6666
const content = excerpt(file, {
6767
start: ':root {',
68-
end: '--sdDeepRef: italic 800 26px/1.25 Arial;',
68+
end: '\n}',
6969
});
7070
const expectedOutput = `--sdCompositionSize: 24px;
7171
--sdCompositionOpacity: 0.5;
7272
--sdCompositionFontSize: 96px;
7373
--sdCompositionFontFamily: Roboto;
7474
--sdCompositionFontWeight: 700;
75-
--sdCompositionHeaderFontFamily: Roboto;
76-
--sdCompositionHeaderFontSize: 96px;
77-
--sdCompositionHeaderFontWeight: 700;
78-
--sdCompositionHeaderLineHeight: 1.25;
79-
--sdCompositionHeaderLetterSpacing: 1.25em;
8075
--sdTypography: italic 800 26px/1.25 Arial;
8176
--sdFontWeightRefWeight: 800;
8277
--sdFontWeightRefStyle: italic;
8378
--sdBorder: 4px solid #FFFF00;
8479
--sdShadowSingle: inset 0 4px 10px 0 rgba(0,0,0,0.4);
8580
--sdShadowDouble: inset 0 4px 10px 0 rgba(0,0,0,0.4), 0 8px 12px 5px rgba(0,0,0,0.4);
86-
--sdRef: italic 800 26px/1.25 Arial;`;
81+
--sdRef: italic 800 26px/1.25 Arial;
82+
--sdDeepRef: italic 800 26px/1.25 Arial;
83+
--sdDeepRefShadowMulti: inset 0 4px 10px 0 rgba(0,0,0,0.4), 0 8px 12px 5px rgba(0,0,0,0.4);
84+
--sdCompositionHeaderFontFamily: Roboto;
85+
--sdCompositionHeaderFontSize: 96px;
86+
--sdCompositionHeaderFontWeight: 700;
87+
--sdCompositionHeaderLineHeight: 1.25;
88+
--sdCompositionHeaderLetterSpacing: 1.25em;`;
8789
expect(content).to.equal(expectedOutput);
8890
});
8991

@@ -95,6 +97,8 @@ describe('expand composition tokens', () => {
9597
--sdCompositionFontSize: 96px;
9698
--sdCompositionFontFamily: Roboto;
9799
--sdCompositionFontWeight: 700;
100+
--sdFontWeightRefWeight: 800;
101+
--sdFontWeightRefStyle: italic;
98102
--sdCompositionHeaderFontFamily: Roboto;
99103
--sdCompositionHeaderFontSize: 96px;
100104
--sdCompositionHeaderFontWeight: 700;
@@ -110,8 +114,6 @@ describe('expand composition tokens', () => {
110114
--sdTypographyTextDecoration: none;
111115
--sdTypographyTextCase: none;
112116
--sdTypographyFontStyle: italic;
113-
--sdFontWeightRefWeight: 800;
114-
--sdFontWeightRefStyle: italic;
115117
--sdBorderColor: #ffff00;
116118
--sdBorderWidth: 4px;
117119
--sdBorderStyle: solid;

test/integration/sd-transforms.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('sd-transforms smoke tests', () => {
5454
--sdOpacity: 0.25;
5555
--sdSpacingSm: 8px;
5656
--sdSpacingXl: 64px;
57-
--sdSpacingMultiValue: 8px 64px; /* You can have multiple values in a single spacing token. Read more on these: https://docs.tokens.studio/available-tokens/spacing-tokens#multi-value-spacing-tokens */
57+
--sdSpacingMultiValue: 8px 64px; /** You can have multiple values in a single spacing token. Read more on these: https://docs.tokens.studio/available-tokens/spacing-tokens#multi-value-spacing-tokens */
5858
--sdColorsBlack: #000000;
5959
--sdColorsWhite: #ffffff;
6060
--sdColorsBlue: #0000ff;
@@ -111,7 +111,7 @@ describe('sd-transforms smoke tests', () => {
111111
--sd-opacity: 0.25;
112112
--sd-spacing-sm: 8px;
113113
--sd-spacing-xl: 64px;
114-
--sd-spacing-multi-value: 8px 64px; /* You can have multiple values in a single spacing token. Read more on these: https://docs.tokens.studio/available-tokens/spacing-tokens#multi-value-spacing-tokens */
114+
--sd-spacing-multi-value: 8px 64px; /** You can have multiple values in a single spacing token. Read more on these: https://docs.tokens.studio/available-tokens/spacing-tokens#multi-value-spacing-tokens */
115115
--sd-colors-black: #000000;
116116
--sd-colors-white: #ffffff;
117117
--sd-colors-blue: #0000ff;

test/integration/w3c-spec-compliance.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('w3c spec compliance smoke test', () => {
5858
--sdOpacity: 0.25;
5959
--sdSpacingSm: 8px;
6060
--sdSpacingXl: 64px;
61-
--sdSpacingMultiValue: 8px 64px; /* You can have multiple values in a single spacing token. Read more on these: https://docs.tokens.studio/available-tokens/spacing-tokens#multi-$value-spacing-tokens */
61+
--sdSpacingMultiValue: 8px 64px; /** You can have multiple values in a single spacing token. Read more on these: https://docs.tokens.studio/available-tokens/spacing-tokens#multi-$value-spacing-tokens */
6262
--sdColorsBlack: #000000;
6363
--sdColorsWhite: #ffffff;
6464
--sdColorsBlue: #0000FF;

test/spec/preprocessors/add-font-styles.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ describe('add font style', () => {
151151

152152
expect(stub.calls.size).to.equal(1);
153153
expect(stub.firstCall?.args[0].message).to.equal(
154-
`tries to reference fwRef, which is not defined.`,
154+
`tokens-studio preprocessor -> addFontStyles: Failing to resolve references within fontWeight -> {fwRef}.\n\ntries to reference {fwRef}, which is not defined.`,
155155
);
156156
});
157157

@@ -291,8 +291,14 @@ describe('add font style', () => {
291291
},
292292
} as DeepKeyTokenMap<false>;
293293

294+
const stub = stubMethod(console, 'error');
294295
const processed = addFontStyles(tokens);
296+
restore();
295297

298+
expect(stub.calls.size).to.equal(1);
299+
expect(stub.firstCall?.args[0].message).to.equal(
300+
`tokens-studio preprocessor -> addFontStyles: Failing to resolve references within fontWeight -> {foo}.\n\ntries to reference {foo}, which is not defined.`,
301+
);
296302
expect(processed).to.eql({
297303
foo: {
298304
$type: 'fontWeight',

test/spec/register.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ describe('register', () => {
317317
--opacity: 0.25;
318318
--spacingSm: 8px;
319319
--spacingXl: 64px;
320-
--spacingMultiValue: 8px 64px; /* You can have multiple values in a single spacing token. Read more on these: https://docs.tokens.studio/available-tokens/spacing-tokens#multi-value-spacing-tokens */
320+
--spacingMultiValue: 8px 64px; /** You can have multiple values in a single spacing token. Read more on these: https://docs.tokens.studio/available-tokens/spacing-tokens#multi-value-spacing-tokens */
321321
--colorsBlack: #000000;
322322
--colorsWhite: #ffffff;
323323
--colorsBlue: #0000ff;

0 commit comments

Comments
 (0)