forked from Joystream/atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd.config.js
106 lines (100 loc) · 3.38 KB
/
sd.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* eslint-disable @typescript-eslint/no-var-requires */
const { template, camelCase, kebabCase } = require('lodash')
const { basename } = require('path')
const variablesTemplate = template(`import { css } from '@emotion/react'
export const variables = css\`
:root {
<%= cssVariables %>
}\`
export const theme = {
<%= themeVariables %>
}
export const cVar = (key: keyof typeof theme) => {
return theme[key]
}
`)
const createTokenKey = (token) => {
const baseFileName = basename(token.filePath).replace('.token.json', '')
// singularize string
const prefix = baseFileName.substr(-1) === 's' ? baseFileName.slice(0, -1) : baseFileName
return `${prefix}-${token.name.replace(/-default/g, '')}`
}
module.exports = {
source: [`./src/styles/tokens/**/*.json`],
transform: {
referencedValueTransform: {
// style dictionary requires adding ".value" suffix to all referenced value, e.g. "value": "{core.neutral.default.900}" should be e.g. "value": "{core.neutral.default.900.value}"
// this transform should fix it, although it just a workaround
// we could remove this when they do something about it https://github.com/amzn/style-dictionary/issues/721
type: 'value',
transitive: true,
matcher: (token) => token.value.value && token.type !== 'typedef',
transformer: (token) => token.value.value,
},
easingTransform: {
type: 'value',
matcher: (token) => token.attributes.category === 'easing',
// [1, 2, 3, 4] will become 1, 2, 3, 4
transformer: (token) => `cubic-bezier(${token.value.toString().replace(/\[|\]/g, '')})`,
},
transitionTransform: {
type: 'value',
transitive: true,
matcher: (token) => token.attributes.category === 'transition' && token.value.timing && token.value.easing,
transformer: (token) => `${token.value.timing.value} ${token.value.easing.value}`,
},
},
format: {
customFormat: ({ dictionary }) => {
return variablesTemplate({
cssVariables: dictionary.allTokens
.map((token) => {
if (token.type === 'typedef') {
return
}
let keyValuePair = `--${createTokenKey(token)}: ${token.value};`
if (dictionary.usesReference(token.original.value)) {
const refs = dictionary.getReferences(token.original.value)
refs.forEach((ref) => {
keyValuePair = keyValuePair.replace(ref.value, ` var(--${createTokenKey(ref)})`)
})
}
return keyValuePair
})
.join('\n'),
themeVariables: dictionary.allTokens
.map((token) => {
if (token.type === 'typedef') {
return
}
const variableName = createTokenKey(token)
const key = camelCase(variableName)
const value = `'var(--${kebabCase(variableName)})'`
return `${key}: ${value},`
})
.join('\n'),
})
},
},
platforms: {
ts: {
transforms: [
`attribute/cti`,
`name/cti/kebab`,
'referencedValueTransform',
'easingTransform',
'transitionTransform',
],
buildPath: 'src/styles/generated/',
files: [
{
destination: 'variables.ts',
format: 'customFormat',
options: {
outputReferences: true,
},
},
],
},
},
}