All files / packages/design-tokens/src/style-dictionary build.js

100% Statements 36/36
66.66% Branches 4/6
100% Functions 5/5
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287                                      1x           1x     1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x   1x       1x       1x       1x         1x     1x                                     1x                                 1x   1x   2x                                                                                                                                                                                                                                         2x   2x                         2x 36x   56x       2x     4x         36x                               2x 2x     2x                       2x      
import StyleDictionary from 'style-dictionary';
 
import { resolvePackagePath } from './utils/path-resolution.js';
import { lightDarkCSS } from './transforms/css/light-dark-css.js';
import { cssVarTransformer } from './transforms/css/dynamic-vars.js';
import { valueUnitStringTransform } from './transforms/common/value-unit-to-string.js';
import { aliasFilter } from './filters/alias-filter.js';
import { scopeFilter } from './filters/scope-filter.js';
import { deprecatedFilter } from './filters/deprecated-filter.js';
import { configFilter } from './filters/config-filter.js';
import { registerMobileFilters } from './filters/mobile-filter.js';
import { oklchToHex } from './transforms/common/oklch-to-hex.js';
import { jsonFlatMetadataFormat } from './formats/json-flat-metadata.js';
import { jsonRawFormat } from './formats/json-raw.js';
import { androidFormat, androidTokensAllFormat } from './formats/android-tokens-split.js';
import { iosFormat, iosTokensAllFormat } from './formats/ios-tokens-split.js';
import { cssConfigFormat } from './formats/css-config.js';
 
// List the themes for Style Dictionary to build
const themes = ['cosmos', 'slds'];
 
// Themes that have mobile platform outputs (Android/iOS)
// Can be a subset of `themes` when a theme doesn't yet support mobile
// or uses a different token structure. Mobile-enabled themes also get
// alias-palettes.json included in their source for token resolution.
const MOBILE_ENABLED_THEMES = ['cosmos', 'slds'];
 
// Register transforms
lightDarkCSS(StyleDictionary);
cssVarTransformer(StyleDictionary);
valueUnitStringTransform(StyleDictionary);
oklchToHex(StyleDictionary);
 
// register filters
aliasFilter(StyleDictionary);
scopeFilter(StyleDictionary);
deprecatedFilter(StyleDictionary);
configFilter(StyleDictionary);
registerMobileFilters(StyleDictionary);
 
// register formats
jsonFlatMetadataFormat(StyleDictionary);
jsonRawFormat(StyleDictionary);
// Unified mobile formats
StyleDictionary.registerFormat({
  name: 'android/format',
  format: androidFormat,
});
StyleDictionary.registerFormat({
  name: 'android/tokens-all',
  format: androidTokensAllFormat,
});
StyleDictionary.registerFormat({
  name: 'ios/format',
  format: iosFormat,
});
StyleDictionary.registerFormat({
  name: 'ios/tokens-all',
  format: iosTokensAllFormat,
});
 
cssConfigFormat(StyleDictionary);
 
// Configure warnings
StyleDictionary.warnings = {
  filteredTokenReferences: {
    enabled: false,
  },
};
 
/**
 * Build Configuration CSS
 *
 * Config tokens are built separately from theme tokens because they serve a different purpose:
 * - Theme tokens: Design values (colors, spacing, fonts) specific to visual themes
 * - Config tokens: Configuration flags used by components to determine behavior
 *
 * The config.css file is theme-independent and provides system-wide configuration values
 * like version detection (e.g., --slds-is-v2-enabled) that components use to adapt their
 * behavior across different design system versions.
 *
 * Output: dist/config/config.css (shared across all themes)
 */
const configSD = new StyleDictionary({
  source: [resolvePackagePath('src/tokens/common/config.json')],
  platforms: {
    config: {
      buildPath: resolvePackagePath('dist/config/'),
      transforms: ['name/kebab'],
      files: [
        {
          destination: 'config.css',
          format: 'css/config',
          filter: 'filter/config',
        },
      ],
    },
  },
});
 
await configSD.buildAllPlatforms();
 
await Promise.all(
  themes.map((theme) => {
    const platforms = {
      // CSS Variables
      css: {
        buildPath: resolvePackagePath(`dist/themes/${theme}/`),
        transforms: [
          'name/kebab',
          'value/value-unit-to-string',
          'value/css-vars',
          'value/light-dark-css-var',
          'value/oklch-to-hex',
        ],
        files: [
          // Reference tokens
          {
            destination: `${theme}.reference.hooks.custom-props.css`,
            format: 'css/variables',
            filter: 'filter/reference',
            options: {
              selector: `:root, .slds-theme_${theme}`,
              header:
                '/*\n * SLDS Reference Custom Properties\n * Generated from design tokens\n * DO NOT EDIT DIRECTLY\n */',
            },
          },
          // Shared tokens
          {
            destination: `${theme}.shared.hooks.custom-props.css`,
            format: 'css/variables',
            filter: 'filter/shared',
            options: {
              selector: `:root, .slds-theme_${theme}`,
              header:
                '/*\n * SLDS Shared Custom Properties\n * Generated from design tokens\n * DO NOT EDIT DIRECTLY\n */',
            },
          },
          // Component tokens
          {
            destination: `${theme}.component.hooks.custom-props.css`,
            format: 'css/variables',
            filter: 'filter/component',
            options: {
              selector: `:root, .slds-theme_${theme}`,
              header:
                '/*\n * SLDS Component Custom Properties\n * Generated from design tokens\n * DO NOT EDIT DIRECTLY\n */',
            },
          },
          // Global tokens (matches legacy filename)
          {
            destination: `${theme}.hooks.custom-props.css`,
            format: 'css/variables',
            filter: 'filter/global',
            options: {
              selector: `:root, .slds-theme_${theme}`,
              header:
                '/*\n * SLDS Global Custom Properties\n * Generated from design tokens\n * DO NOT EDIT DIRECTLY\n */',
            },
          },
        ],
      },
      // Raw JSON with extensions
      rawJson: {
        buildPath: resolvePackagePath(`dist/themes/${theme}/`),
        transforms: [],
        files: [
          {
            destination: `${theme}.hooks.raw.json`,
            format: 'json/raw',
            filter: 'filter/aliases',
          },
          {
            destination: `${theme}.reference.hooks.raw.json`,
            format: 'json/raw',
            filter: 'filter/reference',
          },
          {
            destination: `${theme}.shared.hooks.raw.json`,
            format: 'json/raw',
            filter: 'filter/shared',
          },
          {
            destination: `${theme}.component.hooks.raw.json`,
            format: 'json/raw',
            filter: 'filter/component',
          },
          {
            destination: `${theme}.deprecated.hooks.raw.json`,
            format: 'json/raw',
            filter: 'filter/deprecated',
          },
        ],
      },
      // Default style-dictionary JSON format output (for sandbox palette-analysis)
      json: {
        buildPath: resolvePackagePath(`dist/themes/${theme}/`),
        transforms: ['name/camel', 'value/oklch-to-hex'],
        files: [
          {
            destination: `${theme}.hooks.json`,
            format: 'json',
            filter: 'filter/aliases',
          },
        ],
      },
      // Flat JSON (for sandbox parity page and theme-parity validator)
      flatJson: {
        buildPath: resolvePackagePath(`dist/themes/${theme}/`),
        transforms: ['name/kebab', 'value/value-unit-to-string', 'value/oklch-to-hex', 'value/css-vars'],
        files: [
          {
            destination: `${theme}.hooks.flat.json`,
            format: 'json/flat-full',
            filter: 'filter/aliases',
          },
        ],
      },
    };
 
    // Shared configuration for mobile platforms (Android and iOS)
    Eif (MOBILE_ENABLED_THEMES.includes(theme)) {
      // Define token types and their corresponding filter names
      const tokenTypeConfigs = [
        { type: 'spacing', filter: 'filter/mobile-spacing' },
        { type: 'sizing', filter: 'filter/mobile-sizing' },
        { type: 'shapes', filter: 'filter/mobile-radius' },
        { type: 'motion-duration', filter: 'filter/mobile-duration' },
        { type: 'fonts', filter: 'filter/mobile-font' },
        { type: 'system-colors', filter: 'filter/mobile' },
        { type: 'reference-colors', filter: 'filter/mobile' },
        { type: 'semantic-colors', filter: 'filter/mobile' },
        { type: 'palette-colors', filter: 'filter/mobile' },
      ];
 
      // Helper to convert format to PascalCase filename
      const toPascalCase = (str) =>
        str
          .split(/[-/]/)
          .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
          .join('');
 
      // Theme-specific prefix for class/file names
      const themePrefix = theme === 'cosmos' ? 'SalesforceCosmos' : 'SalesforceLightningBlue';
 
      // Helper to create platform configuration (reduces duplication between Android/iOS)
      const createMobilePlatform = (platform, extension) => ({
        buildPath: resolvePackagePath(`dist/themes/${theme}/${platform}/`),
        transforms: ['value/value-unit-to-string', 'value/oklch-to-hex'],
        files: [
          // Generate individual token type files using unified format
          ...tokenTypeConfigs.map(({ type, filter }) => ({
            destination: `${themePrefix}${toPascalCase(type)}.${extension}`,
            format: `${platform}/format`,
            filter,
            options: { tokenType: type, themePrefix },
          })),
          // Combined file with all tokens
          {
            destination: `${themePrefix}TokensAll.${extension}`,
            format: `${platform}/tokens-all`,
            filter: 'filter/mobile',
            options: { themePrefix },
          },
        ],
      });
 
      platforms.android = createMobilePlatform('android', 'kt');
      platforms.ios = createMobilePlatform('ios', 'swift');
    }
 
    const sd = new StyleDictionary({
      include: [resolvePackagePath('src/tokens/common/**/*.json')],
      source: [
        resolvePackagePath(`src/tokens/${theme}/**/*.json`),
        // Include common alias palettes for themes with mobile outputs
        ...(MOBILE_ENABLED_THEMES.includes(theme)
          ? [resolvePackagePath('src/tokens/common/alias-palettes.json')]
          : []),
      ],
      platforms,
    });
 
    return sd.buildAllPlatforms();
  }),
);