@ama-styling/style-dictionary

中文文档
15.2.4发布于 2026/11/05BSD-3-ClauseTypeScriptES Module构建来源已验证近 30 天下载 2.4万

Package exposing modules and hooks for Style Dictionary

原始使用文档
文档目录15 节
  1. Description
  2. Get Started
  3. Enhancement
  4. Design Token Extensions
  5. Available Otter Extensions
  6. Modules
  7. Parsers
  8. Pre-processor
  9. Transforms
  10. Transform-groups
  11. Formats
  12. Registration process
  13. Helpers
  14. Advanced
  15. Basic Node Configuration Example

Otter Style Dictionary

Super cute Otter!

This package is an Otter Framework Module.

Description

Stable Version Bundle Size

This package exposes a set of Hooks and Modules for the Style Dictionary to enhance the capabilities of Design Tokens.

Get Started

Set up your Style Dictionary in your project thanks to the command:

ng add @ama-styling/style-dictionary

By default, the command will do the following updates:

  • Add a dev dependency to the Style Dictionary package.
  • Create a minimal config.mjs configuration file with the required setup for CSS and Metadata generation.
  • Add the generate:theme and generate:metadata scripts in the project package.json.

You will then be able to customize this setup with your project's specifications:

Enhancement

Design Token Extensions

The Design Tokens format allows to provide the $extensions property to enhance the Token it is applied to.
The property can be applied to the Token directly as follows:

// colors.token.json
{
  "colors": {
    "primary": {
      "$value": "#000000",
      "$extensions": {
        "o3rPrivate": true
      }
    }
  }
}

or in a dedicated .extensions.json file (when the ama/json-parser/extensions parser is loaded):

// color.token.json
{
  "colors": {
    "primary": {
      "$value": "#000000"
    }
  }
}
// custom.extensions.json
{
  "colors.primary": {
    "$description": "Primary private color",
    "$extensions": {
      "o3rPrivate": true
    }
  }
}

Available Otter Extensions

ExtensionsTypeDescriptionRequired hooks
o3rPrivatebooleanDetermine if the token is flagged as privatepre-processor: ama/pre-processor/extensions
formatter: ama/css/variable
o3rImportantbooleanDetermine if the token should be flagged as important when generatedpre-processor: ama/pre-processor/extensions
formatter: ama/css/variable
o3rScopestringScope to apply to the generated variablepre-processor: ama/pre-processor/extensions
formatter: ama/css/variable
o3rMetadataCMS MetadataAdditional information to provide to the metadata if generatedpre-processor: ama/pre-processor/extensions
formatter: ama/json/metadata
o3rUnitstringConvert a numeric value from the specified unit to the new unit. It will add a unit to the tokens of type "number" for which the unit is not specified.
In the case of complex types (such as shadow, transition, etc...), the unit will be applied to all numeric types they contain.
pre-processor: ama/pre-processor/extensions
transforms: ama/transform/unit
o3rRationumberRatio to apply to the previous value. The ratio will only be applied to tokens of type "number" or to the first numbers determined in "string" like types.
In the case of complex types (such as shadow, transition, etc...), the ratio will be applied to all numeric types they contain.
pre-processor: ama/pre-processor/extensions
transforms: ama/transform/ratio
o3rExpectOverridebooleanIndicate that the token is expected to be overridden by external rulespre-processor: ama/pre-processor/extensions

[!WARNING] The required hooks need to be registered to the Style Dictionary configuration to fully support the extension.

Modules

Parsers

NameMatching filesDescription
ama/json-parser/one-line-token**/*.jsonAllow dot notation Token in JSON file
ama/json-parser/extensions**/*.extensions.jsonParse file containing $extensions instructions to apply on top of Design Token

Pre-processor

NameDescription
ama/pre-processor/extensionsPre-processor to add the support of the $extensions instructions in the Token (or dedicated extensions.json file). This pre-processor is mandatory for any Otter hooks.

Transforms

NameTypeCriteriaDescription
ama/transform/ratiovalueo3rRatio extension is providedApply the given o3rRatio to the numeric values of the Token(s) it refers to.
ama/transform/unitvalueo3rUnit extension is providedReplace the unit of the values of the Token(s) it refers to, by the provided o3rUnit.

Transform-groups

NameDescription
ama/css/recommendedExtend the official CSS transform groups by adding ama/transform/ratio and ama/transform/unit

Formats

NameOptionsDescription
ama/css/variableSee css/variables optionsRender CSS variable block (based on the built-in format) supporting additional Otter Extensions features.
ama/json/metadataSee css/variables options (applied to defaultValue)
- keepPrivate: include private variables
Render CMS style metadata (CSS variables and their associated customization properties for CMS integration).

Registration process

The different hooks/modules can be registered individually via the different register...() functions from StyleDictionary or all at once with the register function as follows:

import { register, baseConfig } from '@ama-styling/style-dictionary';

const sd = new StyleDictionary({
  ...baseConfig,
  // custom configs ...
});

register(sd); // Register all Otter modules

[!IMPORTANT] To be able to use any of the listed modules/hooks, they have to be registered to the StyleDefinition instance.

[!TIP] The helper baseConfig will provide the minimal configuration for Otter extension support.

Helpers

To enhance and facilitate the configuration of the StyleDictionary instance, the @ama-styling/style-dictionary exposes a set of helpers.

getTargetFiles

The getTargetFiles function is used to parameterize the generated file based on Design Token (following the same logic as $extensions):

// Example to generate the `color-primary-**` variables in `my-color-primary.css`

import { getTargetFiles } from '@ama-styling/style-dictionary';

const rule = {
  'color.primary': 'my-color-primary.css'
};

// or `const rule = {color: {primary: 'my-color-primary.css'}}`
// or `const rule = {'color.primary': {$extensions: {o3rTargetFile: 'my-color-primary.css'}}}`
// or `const rule = {'color: {primary': {$extensions: {o3rTargetFile: 'my-color-primary.css'}}}}`

const sd = new StyleDictionary({
  //...
  plateforms: {
    css: {
      files: [
        ...getTargetFiles({ rule, { format: 'css', defaultFile: 'default-file.css' } })
      ]
    }
  }
});

register(sd); // Register all Otter modules

[!NOTE] The format option will be applied to all the files provided to the getTargetFiles function (including defaultFile). defaultFile defines the default file where variables not matching any rule will be generated.

The helper function getTargetFiles is registering the filter into StyleDictionary with a unique ID.
This IDs can be retrieved as follows:

import { getTargetFiles } from '@ama-styling/style-dictionary';

const sd = new StyleDictionary();
const files = getTargetFiles({ rules, { styleDictionary: sd } });

/**
 * Filter IDs registered in {@link sd} Style Dictionary instance
 * @type {string[]}
 **/
const filterIds = files.map(({ filter }) => filter);

Advanced

Basic Node Configuration Example

// style-builder.mjs

import { register, getTargetFiles, baseConfiguration } from '@ama-styling/style-dictionary';
import StyleDictionary from 'style-dictionary';

// Rules to generate different CSS files according to Token name
const fileRules = {
  colors: 'style/colors.tokens.css',
  'components.panel': 'components/panel/panel.tokens.css'
}

const sd = new StyleDictionary({
  ...baseConfiguration, // Use basic Otter configuration setup
  usesDtcg: true, // Use Design Token Standard format

  source: ['tokens/*.tokens.json'], // Design Token files
  include: ['token.extensions.json'], // Custom extensions

  platforms: {
    // Generate CSS Files
    css: {
      options: {
        outputReferences: true // to output `--var: var(--other-var)` instead of the value of `--other-var`
      },
      transformGroup: 'ama/css/recommended',
      files: [
        ...getTargetFiles(fileRules, { format: 'css' }),
        // default CSS file where generate variables
        { destination: 'style/default.tokens.css', format: 'css' }
      ]
    },
    cms: {
      options: {
        outputReferences: true
      },
      transformGroup: 'ama/css/recommended',
      files: [
        { destination: 'style.metadata.json', format: 'ama/json/metadata' }
      ]
    }
  }
});

// Register otter hooks/modules
register(sd);

if (process.env.CSS_ONLY){
  sd.buildPlatform('css');
} else {
  sd.buildAllPlatforms();
}

Can be run with the following command:

node ./style-builder.mjs

版本与发布记录

@ama-styling/style-dictionary 共发布 748 个版本,首次发布于 2025/06/20;过去 12 个月发布了 545 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
15.2.4latest2026/11/051 天前
15.4.0-prerelease.5prerelease2026/11/051 天前
15.2.32026/11/041 天前
15.4.0-prerelease.42026/11/041 天前
15.4.0-prerelease.32026/11/023 天前
15.4.0-prerelease.22026/11/023 天前

其他发布通道:next → 14.0.0-next.14,v13-updated → 13.5.13,v14-updated → 14.5.4,v15-updated → 15.1.3,rc → 15.3.0-rc.1,prerelease → 15.4.0-prerelease.5。

关于 @ama-styling/style-dictionary 的常见问题

@ama-styling/style-dictionary 是什么?

Package exposing modules and hooks for Style Dictionary

如何安装 @ama-styling/style-dictionary?

在项目目录运行 npm install @ama-styling/style-dictionary,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:style-dictionary(^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ama-styling/style-dictionary 支持 TypeScript 吗?

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

@ama-styling/style-dictionary 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ama-styling/style-dictionary 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.22.3 || ^24.15.0 || ^26.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ama-styling/style-dictionary 可以商用吗?

@ama-styling/style-dictionary 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@ama-styling/style-dictionary 的下载量有多少?

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

@ama-styling/style-dictionary 还在维护吗?

最新版本 15.2.4 发布于 2026/11/05(1 天前);至今共发布 748 个版本,其中过去 12 个月发布了 545 个;首次发布于 2025/06/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。