3
3
*/
4
4
5
5
const glob = require ( 'fast-glob' )
6
- const StyleDictionary = require ( 'style-dictionary' )
6
+ const { PrimerStyleDictionary} = require ( '@primer/primitives/dist/build/PrimerStyleDictionary.js' )
7
+
8
+ const StyleDictionary = PrimerStyleDictionary
7
9
8
10
const { fileHeader, formattedVariables} = StyleDictionary . formatHelpers
9
11
10
12
//-----
11
13
// functions to be extracted
12
14
// TODO: extract to a separate files
13
15
14
- const pathToKebabCase = token => token . path . join ( '-' )
15
-
16
- const pathToDotNotation = token => token . path . join ( '.' )
17
-
18
- const capitalize = string => string [ 0 ] . toUpperCase ( ) + string . slice ( 1 )
19
-
20
- const pathToPascalCase = token => token . path . map ( tokenPathItems => capitalize ( tokenPathItems ) ) . join ( '' )
21
-
22
16
// REGISTER THE CUSTOM TRANFORMS
23
17
24
- /**
25
- * transform: scss variable names
26
- * example: `$namespace-item-variant-property-modifier`
27
- */
28
- StyleDictionary . registerTransform ( {
29
- name : 'name/scss' ,
30
- type : 'name' ,
31
- transformer : pathToKebabCase ,
32
- } )
33
-
34
- /**
35
- * transform: css variable names
36
- * example: `--namespace-item-variant-property-modifier`
37
- */
38
- StyleDictionary . registerTransform ( {
39
- name : 'name/css' ,
40
- type : 'name' ,
41
- transformer : pathToKebabCase ,
42
- } )
43
-
44
- /**
45
- * transform: js variable names
46
- * example: `namespace.item.variant.property.modifier`
47
- */
48
- StyleDictionary . registerTransform ( {
49
- name : 'name/js' ,
50
- type : 'name' ,
51
- transformer : pathToDotNotation ,
52
- } )
53
-
54
- /**
55
- * transform: js es6 variable names
56
- * example: `NamespaceItemVariantPropertyModifier`
57
- */
58
- StyleDictionary . registerTransform ( {
59
- name : 'name/js/es6' ,
60
- type : 'name' ,
61
- transformer : pathToPascalCase ,
62
- } )
63
-
64
18
// find values with px unit
65
19
function isPx ( value ) {
66
20
return / [ \d . ] + p x $ / . test ( value )
@@ -93,68 +47,16 @@ StyleDictionary.registerTransform({
93
47
} ,
94
48
} )
95
49
96
- //-----
97
-
98
- // ts output
99
- StyleDictionary . registerTransform ( {
100
- name : 'attribute/typescript' ,
101
- type : 'attribute' ,
102
- transformer : token => {
103
- return {
104
- typescript : {
105
- // these transforms will need to match the ones you use for typescript
106
- // or you can "chain" the transforms and use token.name and token.value
107
- // like scss and less transforms do.
108
- name : token . path . slice ( 1 ) . join ( '.' ) ,
109
- value : token . value ,
110
- } ,
111
- }
112
- } ,
113
- } )
114
-
115
- // css output
116
- StyleDictionary . registerTransform ( {
117
- name : 'attribute/css' ,
118
- type : 'attribute' ,
119
- transformer : token => {
120
- const tokenName = token . path . slice ( 1 ) . join ( '-' )
121
- return {
122
- css : {
123
- name : `--${ tokenName } ` ,
124
- value : token . value ,
125
- } ,
126
- }
127
- } ,
128
- } )
129
-
130
- // transform: composite typography to shorthands
131
- StyleDictionary . registerTransform ( {
132
- name : 'typography/shorthand' ,
133
- type : 'value' ,
134
- transitive : true ,
135
- matcher : token => token . type === 'typography' ,
136
- transformer : token => {
137
- const { value} = token
138
-
139
- // if lineHeight has value, include in shorthand
140
- if ( value . lineHeight ) {
141
- return `${ value . fontWeight } ${ value . fontSize } /${ value . lineHeight } ${ value . fontFamily } `
142
- }
143
-
144
- return `${ value . fontWeight } ${ value . fontSize } ${ value . fontFamily } `
145
- } ,
146
- } )
147
-
148
50
// REGISTER THE CUSTOM TRANFORM GROUPS
149
51
150
52
StyleDictionary . registerTransformGroup ( {
151
53
name : 'css' ,
152
- transforms : [ 'name/css ' , 'pxToRem' , 'typography/shorthand ' ] ,
54
+ transforms : [ 'name/pathToKebabCase ' , 'pxToRem' , 'typography/css ' ] ,
153
55
} )
154
56
155
57
StyleDictionary . registerTransformGroup ( {
156
58
name : 'scss' ,
157
- transforms : [ 'name/scss ' , 'pxToRem' , 'typography/shorthand ' ] ,
59
+ transforms : [ 'name/pathToKebabCase ' , 'pxToRem' , 'typography/css ' ] ,
158
60
} )
159
61
160
62
// REGISTER A CUSTOM FORMAT
@@ -187,19 +89,6 @@ StyleDictionary.registerFormat({
187
89
} ,
188
90
} )
189
91
190
- StyleDictionary . registerFormat ( {
191
- name : 'custom/format/custom-media' ,
192
- formatter ( { dictionary} ) {
193
- return dictionary . allProperties
194
- . map ( prop => {
195
- const { value, name} = prop
196
-
197
- return `@custom-media --${ name } -viewport ${ value } ;`
198
- } )
199
- . join ( '\n' )
200
- } ,
201
- } )
202
-
203
92
// format docs
204
93
StyleDictionary . registerFormat ( {
205
94
name : 'json/docs' ,
@@ -398,7 +287,7 @@ function buildPrimitives(
398
287
files : [
399
288
{
400
289
filter : token => token . filePath . includes ( 'viewport' ) ,
401
- format : 'custom/format/custom-media ' ,
290
+ format : 'css/customMedia ' ,
402
291
destination : 'viewport.css' ,
403
292
} ,
404
293
] ,
@@ -420,7 +309,7 @@ function buildPrimitives(
420
309
} ,
421
310
js : {
422
311
buildPath : `${ outputPath } /js/` ,
423
- transforms : [ 'name/js/es6 ' , 'pxToRem' ] ,
312
+ transforms : [ 'name/pathToPascalCase ' , 'pxToRem' ] ,
424
313
// map the array of token file paths to style dictionary output files
425
314
files : files . map ( filePath => {
426
315
return {
0 commit comments