LoginSignup
1
0

Figma tokens + style dictionary でCSSを生成

Posted at

背景

figmaで作成したtokenをGitHubにpushしてCSSファイルに書き出したい。
figma tokens → GitHub → style dictionary → CSSファイル

この記事の続き

なんでfigma tokensなのか

本当はfigamaの機能のvariablesでやりたかった。
→ variablesからtokenを書き出す方法が分からなかった。
(APIはエンタープライズプランから使える)
プラグインがあるかも。

tokens.json をtransformする

GitHubにpushされたtoken.json

スクリーンショット 2024-04-25 15.56.59.png

style dictionaryで使えるようにするためリポジトリにpushしたtokens.json をtransformする。

// token-transformerをインストール
npm install -D token-transformer

package.jsonにスクリプトを追加する
(./data/tokens.json をtransformして output.jsonに書き出す)

  "scripts": {
    "transform": "token-transformer ./data/tokens.json output.json"
  }

スクリプトを実行

npm transform

transformされたoutput.json
スクリーンショット 2024-04-25 16.00.13.png

style dictionaryでCSSを生成する

// style-dictionaryをインストール
npm install -D style-dictionary

package.jsonにスクリプトを追加する

  "scripts": {
    "start": "npm run transform && npm run build",
    "build": "style-dictionary build",
  }

config.jsonを作成する

{
  "source": ["output.json"],
  "platforms": {
    "css": {
      "transformGroup": "css",
      "buildPath": "build/css/",
      "files": [{
        "destination": "variables.css",
        "format": "css/variables",
        "options": {
          "outputReferences": true
        }
      }]
    }
  }
}

スクリプトを実行

npm build

CSSファイルが生成される

スクリーンショット 2024-04-25 16.04.55.png

最後に

今度はnpmパッケージにする記事を書きたい。
パッケージのバージョン管理もいい感じにしたい。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0