Skip to content

Commit

Permalink
BREAKING: use preprocessors if the passed SD is prerelease.2 or higher
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Jan 9, 2024
1 parent fd9710a commit e879b66
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-kiwis-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tokens-studio/sd-transforms': minor
---

Will now use preprocessors instead of parsers when user Style-Dictionary is v4.0.0-prerelease.2 or higher. Fixes an issue with multi-file references not being resolvable when running composite token expansion or add font style utilities.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ registerTransforms(StyleDictionary, {
expand: {
composition: false,
typography: true,
// Note: when using Style-Dictionary v4.0.0-prerelease.2 or higher, filePath no longer gets passed
// as an argument, because preprocessors work on the full dictionary rather than per file (parsers)
border: (token, filePath) =>
token.value.width !== 0 && filePath.startsWith(path.resolve('tokens/core')),
shadow: false,
Expand Down
38 changes: 15 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"expr-eval": "^2.0.2",
"is-mergeable-object": "^1.1.1",
"postcss-calc-ast-parser": "^0.1.4",
"style-dictionary": "github:amzn/style-dictionary#types"
"style-dictionary": "4.0.0-prerelease.7"
},
"devDependencies": {
"@changesets/cli": "^2.26.0",
Expand Down
10 changes: 9 additions & 1 deletion src/parsers/add-font-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ function recurse(
}
let fontWeight = value.fontWeight;
if (usesReferences(fontWeight)) {
fontWeight = `${resolveReferences(fontWeight, copy) ?? fontWeight}`;
try {
const resolved = resolveReferences(fontWeight, copy);
if (resolved) {
fontWeight = `${resolved}`;
}
} catch (e) {
// we don't want to throw a fatal error, we'll just keep the ref as is
console.error(e);
}
}
// cast because fontStyle is a prop we will add ourselves
const tokenValue = value as TokenTypographyValue & { fontStyle: string };
Expand Down
10 changes: 9 additions & 1 deletion src/parsers/expand-composites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ function recurse(
if (expand) {
// if token uses a reference, attempt to resolve it
if (typeof token.value === 'string' && usesReferences(token.value)) {
token.value = resolveReferences(token.value, copy as DesignTokens) ?? token.value;
try {
const resolved = resolveReferences(token.value, copy as DesignTokens);
if (resolved) {
token.value = resolved;
}
} catch (e) {
// we don't want to throw a fatal error, expansion can still occur, just with the reference kept as is
console.error(e);
}
// If every key of the result (object) is a number, the ref value is a multi-value, which means TokenBoxshadowValue[]
if (
typeof token.value === 'object' &&
Expand Down
29 changes: 22 additions & 7 deletions src/registerTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,32 @@ export async function registerTransforms(
sd: typeof StyleDictionary,
transformOpts?: TransformOptions,
) {
// >= 4.0.0-prelease.2, once SD reaches full release: 4.0.0, sd-transforms will get
// a breaking release where we always use preprocessors
// and are no longer backwards compatible with sd 3 or lower
const supportsPreprocessors =
sd.VERSION.match(/4\.0\.0(-prerelease\.([2-9]|[0-9]{2}))/g) !== null;
// Allow completely disabling the registering of this parser
// in case people want to combine the expandComposites() utility with their own parser and prevent conflicts
if (transformOpts?.expand !== false) {
// expand composition tokens, typography, border, shadow (latter 3 conditionally, as opt-in)
sd.registerParser({
pattern: /\.json$/,
parse: ({ filePath, contents }) => {
const tokens = JSON.parse(contents);
return parseTokens(tokens, transformOpts, filePath) as DesignTokens;
},
});
if (supportsPreprocessors) {
sd.registerPreprocessor({
name: 'sd-transforms-preprocessors',
preprocessor: dictionary => {
console.log('halllelujah');
return parseTokens(dictionary, transformOpts) as DesignTokens;
},
});
} else {
sd.registerParser({
pattern: /\.json$/,
parse: ({ filePath, contents }) => {
const tokens = JSON.parse(contents);
return parseTokens(tokens, transformOpts, filePath) as DesignTokens;
},
});
}
}

sd.registerTransform({
Expand Down
51 changes: 51 additions & 0 deletions test/integration/cross-file-refs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type StyleDictionary from 'style-dictionary';
import { expect } from '@esm-bundle/chai';
import { promises } from 'fs';
import path from 'path';
import { cleanup, init } from './utils.js';

const outputDir = 'test/integration/tokens/';
const outputFileName = 'vars.css';
const outputFilePath = path.resolve(outputDir, outputFileName);

const cfg = {
source: ['test/integration/tokens/cross-file-refs-*.tokens.json'],
platforms: {
css: {
transformGroup: 'tokens-studio',
prefix: 'sd',
buildPath: outputDir,
files: [
{
destination: outputFileName,
format: 'css/variables',
},
],
},
},
};

let dict: StyleDictionary | undefined;

describe('cross file references', () => {
beforeEach(async () => {
cleanup(dict);
dict = await init(cfg, { expand: { typography: true } });
});

afterEach(async () => {
await cleanup(dict);
});

it('supports cross file references e.g. expanding typography', async () => {
const file = await promises.readFile(outputFilePath, 'utf-8');
console.log(file);
expect(file).to.include(`
--sdTypoFontWeight: 400;
--sdTypoFontStyle: italic;
--sdWeight: 400 italic;
--sdTypoAliasFontWeight: 400;
--sdTypoAliasFontStyle: italic;
`);
});
});
8 changes: 8 additions & 0 deletions test/integration/tokens/cross-file-refs-1.tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"typo": {
"value": {
"fontWeight": "{weight}"
},
"type": "typography"
}
}
6 changes: 6 additions & 0 deletions test/integration/tokens/cross-file-refs-2.tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"weight": {
"value": "Regular italic",
"type": "fontWeights"
}
}
6 changes: 6 additions & 0 deletions test/integration/tokens/cross-file-refs-3.tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"typo-alias": {
"value": "{typo}",
"type": "typography"
}
}

0 comments on commit e879b66

Please sign in to comment.