Created
March 9, 2021 21:10
-
-
Save dbanksdesign/45d424544a4031b89ad11536fcbeccfa to your computer and use it in GitHub Desktop.
Custom Formats in Style Dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Way 3: calling StyleDictionary.extend().buildAllPlatforms() | |
// you would call this in your npm script like: "node build1.js" | |
// the filename doesn't really matter, I tend to use 'build.js' if this file is running and 'config.js' if it exports a config | |
const StyleDictionary = require('style-dictionary'); | |
StyleDictionary.registerFormat({ | |
name: 'myCustomFormat', | |
formatter: function({ dictionary }) { | |
// custom format code | |
} | |
}); | |
StyleDictionary.extend({ | |
source: ['tokens/**/*.json'], | |
platforms: { | |
css: { | |
transformGroup: 'css', | |
files: [{ | |
destination: 'variables.css', | |
format: 'myCustomFormat' | |
}] | |
} | |
} | |
}).buildAllPlatforms(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Way 4: calling buildAllPlatforms() with inline format | |
// you would call this in your npm script like: "node build.js" | |
const StyleDictionary = require('style-dictionary'); | |
StyleDictionary.extend({ | |
format: { | |
myCustomFormat: function({ dictionary }) { | |
// custom format code | |
} | |
}, | |
source: ['tokens/**/*.json'], | |
platforms: { | |
css: { | |
transformGroup: 'css', | |
files: [{ | |
destination: 'variables.css', | |
format: 'myCustomFormat' | |
}] | |
} | |
} | |
}).buildAllPlatforms(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Way 5: calling extend() and then .registerFormat() | |
// you would call this in your npm script like: "node build.js" | |
const StyleDictionary = require('style-dictionary'); | |
const sd = StyleDictionary.extend({ | |
source: ['tokens/**/*.json'], | |
platforms: { | |
css: { | |
transformGroup: 'css', | |
files: [{ | |
destination: 'variables.css', | |
format: 'myCustomFormat' | |
}] | |
} | |
} | |
}); | |
// You can register the format after .extend() as long as it is before .buildAllPlatforms() | |
sd.registerFormat({ | |
name: 'myCustomFormat', | |
formatter: function({ dictionary }) { | |
// custom format code | |
} | |
}); | |
sd.buildAllPlatforms(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Way 1: Javascript config with inline custom format | |
// you would call this in an npm script like: "style-dictionary build --config ./config1.js" | |
module.exports = { | |
format: { | |
myCustomFormat: function({ dictionary }) { | |
// custom format code | |
} | |
}, | |
source: ['tokens/**/*.json'], | |
platforms: { | |
css: { | |
transformGroup: 'css', | |
files: [{ | |
destination: 'variables.css', | |
format: 'myCustomFormat' | |
}] | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Way 2: Javascript config with registerTransform | |
// you would call this in an npm script the same way: "style-dictionary build --config ./config2.js" | |
const StyleDictionary = require('style-dictionary'); | |
StyleDictionary.registerFormat({ | |
name: 'myCustomFormat', | |
formatter: function({ dictionary }) { | |
// custom format code | |
} | |
}); | |
module.exports = { | |
source: ['tokens/**/*.json'], | |
platforms: { | |
css: { | |
transformGroup: 'css', | |
files: [{ | |
destination: 'variables.css', | |
format: 'myCustomFormat' | |
}] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment