Skip to content

Commit

Permalink
add custom transformers for px
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann committed Jul 22, 2021
1 parent 5c8494d commit 2a1d7bc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
6 changes: 3 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
"source": ["tokens/*.json"],
"platforms": {
"scss": {
"transformGroup": "scss",
"transformGroup": "custom/scss",
"buildPath": "build/scss/",
"files": [{
"destination": "_variables.scss",
"format": "scss/variables"
}]
},
"less": {
"transformGroup": "less",
"transformGroup": "custom/less",
"buildPath": "build/less/",
"files": [{
"destination": "_variables.less",
"format": "less/variables"
}]
},
"css": {
"transformGroup": "css",
"transformGroup": "custom/css",
"buildPath": "build/css/",
"files": [{
"destination": "_variables.css",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"scripts": {
"start": "npm run transform-tokens",
"transform-tokens": "style-dictionary build",
"transform-tokens": "node ./transformTokens.js",
"test": "jest tests --coverage=false"
},
"repository": {
Expand Down
53 changes: 53 additions & 0 deletions transformTokens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const StyleDictionary = require('style-dictionary')
const baseConfig = require('./config.json')


StyleDictionary.registerTransform({
name: 'size/px',
type: 'value',
matcher: token => {
return token.unit === 'pixel' && token.value !== 0
},
transformer: token => {
return `${token.value}px`
},
})

StyleDictionary.registerTransform({
name: 'size/percent',
type: 'value',
matcher: token => {
return token.unit === 'percent' && token.value !== 0
},
transformer: token => {
return `${token.value}%`
},
})

StyleDictionary.registerTransformGroup({
name: 'custom/css',
transforms: StyleDictionary.transformGroup['css'].concat([
'size/px',
'size/percent',
]),
})

StyleDictionary.registerTransformGroup({
name: 'custom/less',
transforms: StyleDictionary.transformGroup['less'].concat([
'size/px',
'size/percent',
]),
})

StyleDictionary.registerTransformGroup({
name: 'custom/scss',
transforms: StyleDictionary.transformGroup['less'].concat([
'size/px',
'size/percent',
]),
})

const StyleDictionaryExtended = StyleDictionary.extend(baseConfig)

StyleDictionaryExtended.buildAllPlatforms()

0 comments on commit 2a1d7bc

Please sign in to comment.