Skip to content

Commit c9dd760

Browse files
committed
feat: upgrade to latest SD pre 27, preprocessor explicit
1 parent 376cbe6 commit c9dd760

11 files changed

+291
-204
lines changed

.changeset/rude-berries-train.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': minor
3+
---
4+
5+
BREAKING: update to Style Dictionary `v4.0.0-prerelease.27`, set preprocessor name to `'tokens-studio'`, which now has to be applied if you want to exclude parent keys, expand composite types or add font style properties to typography values.

README.md

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Table of contents
66

77
- [Installation](#installation)
8+
- [Compatibility](#compatibility)
89
- [Getting Started](#usage)
910
- [Using the transforms](#using-the-transforms)
1011
- [Custom Transform Group](#custom-transform-group)
@@ -55,23 +56,37 @@ With [NPM](https://www.npmjs.com/):
5556
npm install @tokens-studio/sd-transforms
5657
```
5758

59+
## Compatibility
60+
61+
This package is to be used in combination with [Style Dictionary](https://github.com/amzn/style-dictionary).
62+
63+
There are some caveats however, with regards to which versions of Style Dictionary are compatible with which versions of this package:
64+
65+
| Style Dictionary | sd-transforms |
66+
| ----------------------------------------- | --------------- |
67+
| 3.0.0 - 4.0.0-prerelease.1 | <= 0.12.2 |
68+
| 4.0.0-prerelease.2 - 4.0.0-prerelease.18 | 0.13.0 - 0.14.4 |
69+
| 4.0.0-prerelease.18 - 4.0.0-prerelease.26 | 0.13.0 - 0.15.2 |
70+
| >= 4.0.0-prerelease.27 | >= 0.16.0 |
71+
5872
## Usage
5973

6074
> Note: this library is available both in CJS and ESM
6175
6276
```js
63-
const { registerTransforms } = require('@tokens-studio/sd-transforms');
64-
const StyleDictionary = require('style-dictionary');
77+
import { registerTransforms } from '@tokens-studio/sd-transforms';
78+
import StyleDictionary from 'style-dictionary';
6579

6680
// will register them on StyleDictionary object
6781
// that is installed as a dependency of this package.
6882
registerTransforms(StyleDictionary);
6983

7084
const sd = StyleDictionary.extend({
7185
source: ['**/*.json'], // <-- make sure to have this match your token files!!!
86+
preprocessors: ['tokens-studio'], // <-- since 0.16.0 this must be explicit
7287
platforms: {
7388
css: {
74-
transformGroup: 'tokens-studio',
89+
transformGroup: 'tokens-studio', // <-- apply the tokens-studio transformGroup to apply all transforms
7590
transforms: ['name/kebab'], // <-- add a token name transform for generating token names, default is camel
7691
buildPath: 'build/css/',
7792
files: [
@@ -98,11 +113,25 @@ node build-output.js
98113

99114
> From Style-Dictionary `4.0.0-prerelease.18`, [`transformGroup` and `transforms` can now be combined in a platform inside your config](https://github.com/amzn/style-dictionary/blob/v4/CHANGELOG.md#400-prerelease18).
100115
116+
### Using the preprocessor
117+
118+
If you want to use `excludeParentKeys`, `expand` or allow this package to extract the `fontStyle` from the `fontWeight` e.g. `regular italic`,
119+
you must add the `'tokens-studio'` preprocessor explicitly in the configuration:
120+
121+
```json
122+
{
123+
"source": ["**/*.tokens.json"],
124+
"preprocessors": ["tokens-studio"],
125+
"platforms": {}
126+
}
127+
```
128+
101129
### Using the transforms
102130

103131
```json
104132
{
105133
"source": ["**/*.tokens.json"],
134+
"preprocessors": ["tokens-studio"],
106135
"platforms": {
107136
"css": {
108137
"transformGroup": "tokens-studio",
@@ -130,31 +159,31 @@ node build-output.js
130159
```
131160

132161
More fine-grained control is possible, every transformation is available as a raw JavaScript function
133-
for you to create your own Style-Dictionary transformer out of.
162+
for you to create your own Style-Dictionary transform out of.
134163

135164
```js
136-
const { transformDimension } = require('@tokens-studio/sd-transforms');
137-
const StyleDictionary = require('style-dictionary');
165+
import { transformDimension } from '@tokens-studio/sd-transforms';
166+
import StyleDictionary from 'style-dictionary';
138167

139168
StyleDictionary.registerTransform({
140169
name: 'my/size/px',
141170
type: 'value',
142171
transitive: true,
143-
matcher: token => ['fontSizes', 'dimension', 'borderRadius', 'spacing'].includes(token.type),
144-
transformer: token => transformDimension(token.value),
172+
filter: token => ['fontSizes', 'dimension', 'borderRadius', 'spacing'].includes(token.type),
173+
transform: token => transformDimension(token.value),
145174
});
146175
```
147176

148177
### Custom Transform Group
149178

150179
> From Style-Dictionary `4.0.0-prerelease.18`, [`transformGroup` and `transforms` can now be combined in a platform inside your config](https://github.com/amzn/style-dictionary/blob/v4/CHANGELOG.md#400-prerelease18).
151180
152-
You can create a custom transformGroup that includes the individual transforms from this package.
153-
If you wish to use the transformGroup, but adjust or remove a few transforms, your best option is to create a custom transform group:
181+
You can create a custom `transformGroup` that includes the individual transforms from this package.
182+
If you wish to use the `transformGroup`, but adjust or remove a few transforms, your best option is to create a custom transform group:
154183

155184
```js
156-
const { transforms } = require('@tokens-studio/sd-transforms');
157-
const StyleDictionary = require('style-dictionary');
185+
import { transforms } from '@tokens-studio/sd-transforms';
186+
import StyleDictionary from 'style-dictionary';
158187

159188
// Register custom tokens-studio transform group
160189
// without 'px' being added to numbers without a unit
@@ -215,9 +244,9 @@ Here's a full example of how you can use this in conjunction with Style Dictiona
215244
Run this script:
216245

217246
```cjs
218-
const { registerTransforms } = require('@tokens-studio/sd-transforms');
219-
const StyleDictionary = require('style-dictionary');
220-
const { promises } = require('fs');
247+
import { registerTransforms } from '@tokens-studio/sd-transforms';
248+
import StyleDictionary from 'style-dictionary';
249+
import { promises } from 'fs';
221250

222251
registerTransforms(StyleDictionary, {
223252
/* options here if needed */
@@ -229,6 +258,7 @@ async function run() {
229258
source: Object.entries(theme.selectedTokenSets)
230259
.filter(([, val]) => val !== 'disabled')
231260
.map(([tokenset]) => `${tokenset}.json`),
261+
preprocessors: ['tokens-studio'], // <-- since 0.16.0 this must be explicit
232262
platforms: {
233263
css: {
234264
transformGroup: 'tokens-studio',
@@ -289,8 +319,8 @@ Running `permutateThemes` on these themes will generate 4 theme combinations:
289319
See details below:
290320

291321
```js
292-
const { permutateThemes } = require('@tokens-studio/sd-transforms');
293-
const fs = require('fs');
322+
import { permutateThemes } from '@tokens-studio/sd-transforms';
323+
import fs from 'fs';
294324

295325
/**
296326
* Input:
@@ -347,9 +377,9 @@ Note that it is a best practice to generate standalone output files for each the
347377
Full example with multi-dimensional themes:
348378

349379
```js
350-
const { registerTransforms, permutateThemes } = require('@tokens-studio/sd-transforms');
351-
const StyleDictionary = require('style-dictionary');
352-
const { promises } = require('fs');
380+
import { registerTransforms, permutateThemes } from '@tokens-studio/sd-transforms';
381+
import StyleDictionary from 'style-dictionary';
382+
import { promises } from 'fs';
353383

354384
registerTransforms(StyleDictionary, {
355385
/* options here if needed */
@@ -360,6 +390,7 @@ async function run() {
360390
const themes = permutateThemes($themes, { separator: '_' });
361391
const configs = Object.entries(themes).map(([name, tokensets]) => ({
362392
source: tokensets.map(tokenset => `${tokenset}.json`),
393+
preprocessors: ['tokens-studio'], // <-- since 0.16.0 this must be explicit
363394
platforms: {
364395
css: {
365396
transformGroup: 'tokens-studio',

0 commit comments

Comments
 (0)