디자인 토큰을 라이트·다크 CSS 두 파일로 빌드
팔레트와 모드별 의미 토큰 JSON 을 Style Dictionary 로 :root 와 [data-theme="dark"] 두 CSS 파일로 나눠 빌드하고, 별칭이 모드를 따라가는지 확인합니다.
토큰 JSON 하나에 라이트·다크 값을 섞어 두면 CSS 로 나눌 방법이 마땅치 않습니다. 팔레트(원색)는 한 벌, 「배경·글자·강조」 같은 의미 토큰은 모드마다 한 벌씩 두고, 빌드를 모드별로 두 번 돌려 :root 파일과 [data-theme="dark"] 파일을 얻습니다. 토큰 빌드 자체는 디자인 토큰 JSON 을 CSS 변수로 빌드하기 에서 다룬 내용을 전제로 합니다. 예시 JSON 은 직접 쓴 것입니다.
[1] 설치합니다.
$ npm install style-dictionary$ npx style-dictionary --version5.7.0[2] 팔레트를 tokens/core/palette.json 에 둡니다. 모드와 무관한 원색입니다.
{ "palette": { "$type": "color", "white": { "$value": "#ffffff" }, "gray": { "100": { "$value": "#f4f5f7" }, "800": { "$value": "#1f2430" }, "900": { "$value": "#14171f" } }, "blue": { "500": { "$value": "#0b5fff" }, "300": { "$value": "#7aa5ff" } } }}[3] 의미 토큰을 모드마다 파일 하나씩 만듭니다. 두 파일은 같은 이름(color.bg 등)을 갖고, 가리키는 팔레트만 다릅니다.
{ "color": { "$type": "color", "bg": { "$value": "{palette.white}" }, "surface": { "$value": "{palette.gray.100}" }, "text": { "$value": "{palette.gray.900}" }, "accent": { "$value": "{palette.blue.500}" } }}{ "color": { "$type": "color", "bg": { "$value": "{palette.gray.900}" }, "surface": { "$value": "{palette.gray.800}" }, "text": { "$value": "{palette.gray.100}" }, "accent": { "$value": "{palette.blue.300}" } }}[4] 빌드 스크립트 build.mjs 를 씁니다. 모드마다 StyleDictionary 인스턴스를 따로 만들어 source 에 팔레트와 그 모드의 의미 토큰 파일만 넣습니다. 출력 선택자는 css/variables 형식의 selector 옵션으로 정하고, 다크 쪽은 filter 로 의미 토큰만 남깁니다. 팔레트는 라이트 파일(:root)에만 한 번 나오면 됩니다.
import StyleDictionary from 'style-dictionary'; const modes = [ { name: 'light', selector: ':root', keepCore: true }, { name: 'dark', selector: '[data-theme="dark"]', keepCore: false },]; for (const m of modes) { const sd = new StyleDictionary({ source: ['tokens/core/*.json', `tokens/semantic/${m.name}.json`], platforms: { css: { transformGroup: 'css', buildPath: 'build/', files: [{ destination: `${m.name}.css`, format: 'css/variables', filter: (t) => m.keepCore || t.filePath.includes('semantic'), options: { selector: m.selector }, }], }, }, }); await sd.buildAllPlatforms();}[5] 빌드합니다.
$ node build.mjscss✔︎ build/light.css css✔︎ build/dark.css$ cat build/light.css build/dark.css/** * Do not edit directly, this file was auto-generated. */ :root { --palette-white: #ffffff; --palette-gray-100: #f4f5f7; --palette-gray-800: #1f2430; --palette-gray-900: #14171f; --palette-blue-300: #7aa5ff; --palette-blue-500: #0b5fff; --color-bg: #ffffff; --color-surface: #f4f5f7; --color-text: #14171f; --color-accent: #0b5fff;}/** * Do not edit directly, this file was auto-generated. */ [data-theme="dark"] { --color-bg: #14171f; --color-surface: #1f2430; --color-text: #f4f5f7; --color-accent: #7aa5ff;}--color-bg 같은 같은 이름이 두 파일에서 값만 다릅니다. 별칭은 모드별 파일 안에서 각자 풀렸으므로, 의미 토큰이 모드를 따라간 것입니다.
[6] 페이지에서 light.css 다음에 dark.css 를 불러오고 <html> 에 속성을 줍니다. :root 와 [data-theme="dark"] 는 선택자 특이도가 같으므로 뒤에 오는 파일이 이깁니다. 순서를 바꾸면 다크가 적용되지 않습니다.
<html data-theme="dark"><head> <link rel="stylesheet" href="light.css"> <link rel="stylesheet" href="dark.css"></head>[7] 변수를 값이 아니라 팔레트 참조로 내고 싶으면 options 에 outputReferences: true 를 더합니다. 같은 입력에서 이렇게 나왔습니다.
[data-theme="dark"] { --color-bg: var(--palette-gray-900); --color-surface: var(--palette-gray-800); --color-text: var(--palette-gray-100); --color-accent: var(--palette-blue-300);}라이트 파일도 --color-bg: var(--palette-white); 처럼 나왔고 팔레트 변수 정의는 그대로 :root 에 있습니다. 다크 파일이 가리키는 --palette-* 는 :root 에서 오므로 두 파일을 같이 불러야 합니다.
[8] 한 모드에서만 참조를 잘못 쓰면 어떻게 되는지 봅니다. dark.json 의 {palette.blue.300} 을 {palette.blue.350} 으로 바꾸고 다시 돌립니다.
$ node build.mjscss✔︎ build/light.css…Error: Reference Errors:Some token references (1) could not be found.Use log.verbosity "verbose" or use CLI option --verbose for more details.Refer to: https://styledictionary.com/reference/logging/$ echo $?1라이트는 만들어지고 다크에서 멈춥니다. 종료 코드가 1 이라 CI 에서 그대로 실패로 잡힙니다. 모드마다 빌드가 따로 도는 구조라서, 한쪽 파일에만 있는 오타도 그 모드를 빌드할 때 드러납니다.