@divriots/style-dictionary-to-figma

中文文档
0.4.0发布于 2022/09/15MITTypeScriptESM + CJS零依赖近 30 天下载 5.4万

A utility that transforms a style-dictionary object into something Figma Tokens plugin understands

原始使用文档
文档目录4 节
  1. Features
  2. Usage
  3. Use in Backlight / Style-dictionary
  4. Create a JSON for each tokenset

style dictionary playground logo

Brought to you by
‹div›RIOTS ‹div›RIOTS

Style Dictionary To Figma

A utility that transforms a style-dictionary object into something Figma Tokens plugin understands.

Used by Design Systems in Backlight using design tokens in style-dictionary that can be synced into Figma via the Figma Tokens plugin.

The tool, at the moment, assumes usage of the Sync feature of Figma Tokens Plugin. The JSON output is catered to this as it is a single file containing the tokensets information.

Features

  • Supports marking a token group as a custom tokenset so that it will appear as a separate tokenset in Figma. By default, "global" is used as the tokenset, and your tokens will be placed inside of this, but you can override it. This is useful if you want to combine many base tokens into a "global" set but theme-specific token groups into a "theme-dark" set for example. You can configure it like so:

    {
      "color": {
        "tokenset": "custom",
        "primary": {
          "base": {
            "type": "color",
            "value": "#14b8a6"
          }
        }
      }
    }
    

    color.primary.base token will appear under custom tokenset now in the plugin. You can also configure or turn off this automatic tokenset mapping by passing defaultTokenset: false or configuring a string for it defaultTokenset: 'default'

  • Trims .value from reference values as Figma Tokens plugin does not use this suffix.

  • Trims the name properties from tokens since Figma Tokens plugin uses this property to name its tokens, however, without a name property it creates its own naming/nesting by the object structure which is way nicer.

  • Use the reference values rather than its resolved values. Put ignoreUseRefValue: true as a sibling property to the value prop if you want to make an exception and keep it as a resolved value.

  • Allow passing some optional options to adjust the object conversion:

    • cleanMeta, if true, will clean up some of the meta info that style-dictionary creates, which Figma Tokens plugin doesn't care about. Can also pass a string[] if you want to configure a blacklist of meta props that you want to filter out yourself
    transform(obj, { cleanMeta: ['foo', 'bar'] });
    

Usage

npm i @divriots/style-dictionary-to-figma
import { transform } from '@divriots/style-dictionary-to-figma';

const sdObject = { ... };
const figmaObj = transform(sdObject);

In case you want its separate counterparts, you can import them separately.

import {
  trimValue,
  trimName,
  useRefValue,
  markTokenset,
  cleanMeta,
} from '@divriots/style-dictionary-to-figma';

Once you transformed the object to Figma, a recommendation is to push this to GitHub and use the Figma Tokens plugin to sync with it to use the tokens in Figma.

Use in Backlight / Style-dictionary

Import the transform utility and create a style-dictionary formatter:

const { transform } = require('@divriots/style-dictionary-to-figma');
const StyleDictionary = require('style-dictionary');

StyleDictionary.registerFormat({
  name: 'figmaTokensPlugin',
  formatter: ({ dictionary }) => {
    const transformedTokens = transform(dictionary.tokens);
    return JSON.stringify(transformedTokens, null, 2);
  },
});

Or you can also put the formatter directly into the config without registering it imperatively:

const { transform } = require('@divriots/style-dictionary-to-figma');

module.exports = {
  source: ['**/*.tokens.json'],
  format: {
    figmaTokensPlugin: ({ dictionary }) => {
      const transformedTokens = transform(dictionary.tokens);
      return JSON.stringify(transformedTokens, null, 2);
    },
  },
  platforms: {
    json: {
      transformGroup: 'js',
      buildPath: '/tokens/',
      files: [
        {
          destination: 'tokens.json',
          format: 'figmaTokensPlugin',
        },
      ],
    },
  },
};

This spits out a file /tokens/tokens.json which Figma Tokens plugin can import (e.g. through GitHub).

Since Backlight has GitHub and Style-Dictionary integration out of the box, this process is very simple.

Create a JSON for each tokenset

Perhaps you'd like to use this tool to generate a separate JSON file for each tokenset, which you can then manually paste into the Figma Tokens Plugin JSON view. For example, when you're not using the Figma Tokens Plugin Sync feature.

For this, refer to this code snippet from this issue.

版本与发布记录

@divriots/style-dictionary-to-figma 共发布 11 个版本,首次发布于 2022/02/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.0latest2022/09/154 年前
0.3.32022/09/154 年前
0.3.22022/09/124 年前
0.3.12022/09/094 年前
0.3.02022/07/204 年前
0.2.12022/07/144 年前

关于 @divriots/style-dictionary-to-figma 的常见问题

@divriots/style-dictionary-to-figma 是什么?

A utility that transforms a style-dictionary object into something Figma Tokens plugin understands

如何安装 @divriots/style-dictionary-to-figma?

在项目目录运行 npm install @divriots/style-dictionary-to-figma,或使用 pnpm add / yarn add / bun add。

@divriots/style-dictionary-to-figma 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@divriots/style-dictionary-to-figma 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@divriots/style-dictionary-to-figma 可以商用吗?

@divriots/style-dictionary-to-figma 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@divriots/style-dictionary-to-figma 的下载量有多少?

根据 npm 官方下载统计,@divriots/style-dictionary-to-figma 在 2026/10/13 – 2026/11/11 这 30 天内被下载 54,418 次,日均约 1,814 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@divriots/style-dictionary-to-figma 还在维护吗?

最新版本 0.4.0 发布于 2022/09/15(4 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/02/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。