config.cjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const StyleDictionary = require('style-dictionary');
  2. const fs = require('fs');
  3. const path = require('path');
  4. // Add comment header to generated files
  5. StyleDictionary.registerFormat({
  6. name: 'css/variables',
  7. formatter: function(dictionary, config) {
  8. const header = `/**\n` + '* Do not edit directly\n' + `* Generated on ${new Date().toUTCString()}\n` + `* Generated from $pnpm css:variables \n` + `*/\n\n`;
  9. const allProperties = dictionary.allProperties;
  10. const properties = allProperties.map(prop => {
  11. const { name, value } = prop;
  12. return ` --${name}: ${value};`
  13. }).join('\n');
  14. // generate tailwind config
  15. generateTailwindConfig(allProperties);
  16. return header + `:root${this.selector} {\n${properties}\n}`
  17. }
  18. });
  19. // expand shadow tokens into a single string
  20. StyleDictionary.registerTransform({
  21. name: 'shadow/spreadShadow',
  22. type: 'value',
  23. matcher: function (prop) {
  24. return prop.type === 'boxShadow';
  25. },
  26. transformer: function (prop) {
  27. // destructure shadow values from original token value
  28. const { x, y, blur, spread, color } = prop.original.value;
  29. return `${x}px ${y}px ${blur}px ${spread}px ${color}`;
  30. },
  31. });
  32. const transforms = ['attribute/cti', 'name/cti/kebab', 'shadow/spreadShadow'];
  33. // Generate Light CSS variables
  34. StyleDictionary.extend({
  35. source: ['./style-dictionary/tokens/base.json', './style-dictionary/tokens/light.json'],
  36. platforms: {
  37. css: {
  38. transformGroup: 'css',
  39. buildPath: './src/styles/variables/',
  40. files: [
  41. {
  42. format: 'css/variables',
  43. destination: 'light.variables.css',
  44. selector: '',
  45. options: {
  46. outputReferences: true
  47. }
  48. },
  49. ],
  50. transforms,
  51. },
  52. },
  53. }).buildAllPlatforms();
  54. // Generate Dark CSS variables
  55. StyleDictionary.extend({
  56. source: ['./style-dictionary/tokens/base.json', './style-dictionary/tokens/dark.json'],
  57. platforms: {
  58. css: {
  59. transformGroup: 'css',
  60. buildPath: './src/styles/variables/',
  61. files: [
  62. {
  63. format: 'css/variables',
  64. destination: 'dark.variables.css',
  65. selector: '[data-dark-mode=true]',
  66. },
  67. ],
  68. transforms,
  69. },
  70. },
  71. }).buildAllPlatforms();
  72. function set(obj, path, value) {
  73. const lastKey = path.pop();
  74. const lastObj = path.reduce((obj, key) =>
  75. obj[key] = obj[key] || {},
  76. obj);
  77. lastObj[lastKey] = value;
  78. }
  79. function writeFile (file, data) {
  80. const header = `/**\n` + '* Do not edit directly\n' + `* Generated on ${new Date().toUTCString()}\n` + `* Generated from $pnpm css:variables \n` + `*/\n\n`;
  81. const exportString = `module.exports = ${JSON.stringify(data, null, 2)}`;
  82. fs.writeFileSync(path.join(__dirname, file), header + exportString);
  83. }
  84. function generateTailwindConfig(allProperties) {
  85. const tailwindColors = {};
  86. const tailwindBoxShadow = {};
  87. allProperties.forEach(prop => {
  88. const { path, type, name, value } = prop;
  89. if (path[0] === 'Base') {
  90. return;
  91. }
  92. if (type === 'color') {
  93. if (name.includes('fill')) {
  94. console.log(prop);
  95. }
  96. set(tailwindColors, path, `var(--${name})`);
  97. }
  98. if (type === 'boxShadow') {
  99. set(tailwindBoxShadow, ['md'], `var(--${name})`);
  100. }
  101. });
  102. writeFile('./tailwind/colors.cjs', tailwindColors);
  103. writeFile('./tailwind/box-shadow.cjs', tailwindBoxShadow);
  104. }