Skip to content

Commit 93522aa

Browse files
authored
Merge pull request #369 from mrajaeim/main
Support Style Dictionary v4 and add preprocessors config
2 parents 12f1c01 + 3336ffc commit 93522aa

File tree

5 files changed

+601
-281
lines changed

5 files changed

+601
-281
lines changed

README.md

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ Generate `tailwind.config.js` by setting type to `all`.
2424
See [Creating each theme file](https://github.com/nado1001/sd-tailwindcss-transformer#creating-each-theme-file) if you wish to customize the configuration file with [plugin functions](https://tailwindcss.com/docs/plugins), etc.
2525

2626
```js
27-
const StyleDictionaryModule = require('style-dictionary')
28-
const { makeSdTailwindConfig } = require('sd-tailwindcss-transformer')
29-
30-
const StyleDictionary = StyleDictionaryModule.extend(
31-
makeSdTailwindConfig({ type: 'all' })
32-
)
33-
34-
StyleDictionary.buildAllPlatforms()
27+
import StyleDictionary from 'style-dictionary';
28+
import { makeSdTailwindConfig } from 'sd-tailwindcss-transformer';
29+
30+
const styleDictionaryTailwind = new StyleDictionary(
31+
makeSdTailwindConfig({ type: 'all' }),
32+
);
33+
await styleDictionaryTailwind.hasInitialized;
34+
await styleDictionaryTailwind.buildAllPlatforms();
3535
```
3636

3737
Output:
@@ -68,18 +68,21 @@ Create an object for each theme, assuming that various customizations will be ma
6868
Import and use the created files in `tailwind.config.js`.
6969

7070
```js
71-
const StyleDictionaryModule = require('style-dictionary')
72-
const { makeSdTailwindConfig } = require('sd-tailwindcss-transformer')
71+
import StyleDictionary from 'style-dictionary';
72+
import { makeSdTailwindConfig } from 'sd-tailwindcss-transformer';
7373

74-
const types = ['colors', 'fontSize']
74+
const types = ['colors', 'fontSize'];
7575

76-
types.map((type) => {
77-
const StyleDictionary = StyleDictionaryModule.extend(
78-
makeSdTailwindConfig({ type })
79-
)
76+
for (const type of types) {
77+
let tailwindConfig = makeSdTailwindConfig({
78+
type,
79+
});
8080

81-
StyleDictionary.buildAllPlatforms()
82-
})
81+
const styleDictionaryTailwind = new StyleDictionary(tailwindConfig);
82+
83+
await styleDictionaryTailwind.hasInitialized;
84+
await styleDictionaryTailwind.buildAllPlatforms();
85+
}
8386
```
8487

8588
Output:
@@ -110,30 +113,33 @@ CSS custom variables can be used by setting isVariables to `true`.
110113
In this case, a CSS file must also be generated.
111114

112115
```js
113-
const StyleDictionaryModule = require('style-dictionary')
114-
const { makeSdTailwindConfig } = require('sd-tailwindcss-transformer')
116+
import StyleDictionary from 'style-dictionary';
117+
import { makeSdTailwindConfig } from 'sd-tailwindcss-transformer';
115118

116119
const sdConfig = makeSdTailwindConfig({
117-
type: 'all',
118-
isVariables: true
119-
})
120+
type: 'all',
121+
isVariables: true,
122+
});
120123

121124
sdConfig.platforms['css'] = {
122-
transformGroup: 'css',
123-
buildPath: './styles/',
124-
files: [
125-
{
126-
destination: 'tailwind.css',
127-
format: 'css/variables',
128-
options: {
129-
outputReferences: true
130-
}
131-
}
132-
]
133-
}
134-
135-
const StyleDictionary = StyleDictionaryModule.extend(sdConfig)
136-
StyleDictionary.buildAllPlatforms()
125+
transformGroup: 'css',
126+
buildPath: './styles/',
127+
files: [
128+
{
129+
destination: 'tailwind.css',
130+
format: 'css/variables',
131+
options: {
132+
outputReferences: true,
133+
},
134+
},
135+
],
136+
};
137+
138+
const styleDictionaryTailwind = new StyleDictionary(
139+
makeSdTailwindConfig({ type: 'all' }),
140+
);
141+
await styleDictionaryTailwind.hasInitialized;
142+
await styleDictionaryTailwind.buildAllPlatforms();
137143
```
138144

139145
Output:

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@
5050
"prettier": "2.8.8",
5151
"rimraf": "3.0.2",
5252
"semantic-release": "21.1.2",
53-
"style-dictionary": "3.9.2",
53+
"style-dictionary": "^4.1.1",
5454
"tailwindcss": "3.4.4",
5555
"ts-node": "10.9.2",
5656
"tsup": "6.7.0",
5757
"typescript": "4.9.5",
5858
"vitest": "0.33.0"
5959
},
6060
"peerDependencies": {
61-
"style-dictionary": ">=3.7.0"
61+
"style-dictionary": ">=4.0.0"
6262
}
6363
}

src/index.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { Dictionary, Config } from 'style-dictionary/types'
1+
import type { Dictionary } from 'style-dictionary/types/DesignToken'
2+
import type { Config } from 'style-dictionary/types/Config'
23
import type { SdTailwindConfigType, TailwindFormatObjType } from './types'
34
import {
45
addHyphen,
@@ -29,7 +30,7 @@ const formatTokens = (
2930
? `var(--${addHyphen(prefix) + cur.name})`
3031
: `var(--${cur.name})`
3132
} else {
32-
acc[Object.values(cur.attributes).join('.')] = cur.value
33+
acc[Object.values(cur.attributes).join('.')] = cur["$value"] || cur["value"]
3334
}
3435
}
3536

@@ -96,7 +97,8 @@ export const makeSdTailwindConfig = ({
9697
transforms,
9798
buildPath,
9899
prefix,
99-
tailwind
100+
tailwind,
101+
preprocessors
100102
}: SdTailwindConfigType): Config => {
101103
if (type === undefined) {
102104
throw new Error('type is required')
@@ -106,34 +108,39 @@ export const makeSdTailwindConfig = ({
106108
throw new Error('formatType must be "js" or "cjs"')
107109
}
108110

111+
const destination =
112+
type !== 'all'
113+
? `${type}.tailwind.${formatType}`
114+
: `tailwind.config.${formatType}`
115+
109116
return {
117+
preprocessors,
110118
source: getConfigValue(source, ['tokens/**/*.json']),
111-
format: {
112-
tailwindFormat: ({ dictionary }: { dictionary: Dictionary }) => {
113-
return getTailwindFormat({
114-
dictionary,
115-
formatType,
116-
isVariables,
117-
extend,
118-
prefix,
119-
type,
120-
tailwind
121-
})
119+
hooks: {
120+
formats: {
121+
tailwindFormat: ({ dictionary }: { dictionary: Dictionary }) => {
122+
return getTailwindFormat({
123+
dictionary,
124+
formatType,
125+
isVariables,
126+
extend,
127+
prefix,
128+
type,
129+
tailwind
130+
})
131+
}
122132
}
123133
},
124134
platforms: {
125135
[type !== 'all' ? `tailwind/${type}` : 'tailwind']: {
126136
transforms: getConfigValue(transforms, [
127137
'attribute/cti',
128-
'name/cti/kebab'
138+
'name/kebab'
129139
]),
130140
buildPath: getConfigValue(buildPath, 'build/web/'),
131141
files: [
132142
{
133-
destination:
134-
type !== 'all'
135-
? `${type}.tailwind.js`
136-
: `tailwind.config.${formatType}`,
143+
destination,
137144
format: 'tailwindFormat'
138145
}
139146
]

src/types.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { Dictionary } from 'style-dictionary/types/Dictionary'
2-
import type { Platform } from 'style-dictionary/types/Platform'
3-
import type { Config } from 'style-dictionary/types/Config'
1+
import { Dictionary } from 'style-dictionary/types/DesignToken'
2+
import type { Config, PlatformConfig } from 'style-dictionary/types/Config'
43
import type { Config as TailwindConfig } from 'tailwindcss/types'
54

65
export type SdObjType<T extends Record<string, any>> = {
@@ -9,7 +8,13 @@ export type SdObjType<T extends Record<string, any>> = {
98

109
export type TailwindOptions = Pick<TailwindConfig, 'content' | 'darkMode'> & {
1110
plugins: Array<
12-
'typography' | ['typography', { className?: string, target?: 'modern' | 'legacy' }] | 'forms' | ['forms', { strategy?: 'base' | 'class' }] | 'aspect-ratio' | 'line-clamp' | 'container-queries'
11+
| 'typography'
12+
| ['typography', { className?: string; target?: 'modern' | 'legacy' }]
13+
| 'forms'
14+
| ['forms', { strategy?: 'base' | 'class' }]
15+
| 'aspect-ratio'
16+
| 'line-clamp'
17+
| 'container-queries'
1318
>
1419
}
1520
export type TailwindFormatType = 'js' | 'cjs'
@@ -19,9 +24,10 @@ export type SdTailwindConfigType = {
1924
formatType?: TailwindFormatType
2025
isVariables?: boolean
2126
source?: Config['source']
22-
transforms?: Platform['transforms']
23-
buildPath?: Platform['buildPath']
24-
prefix?: Platform['prefix']
27+
preprocessors?: Config['preprocessors']
28+
transforms?: PlatformConfig['transforms']
29+
buildPath?: PlatformConfig['buildPath']
30+
prefix?: PlatformConfig['prefix']
2531
tailwind?: Partial<TailwindOptions>
2632
extend?: boolean
2733
}

0 commit comments

Comments
 (0)