From b8709d74651a6cddd8aac0b9fc58790d0d6241cb Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 4 Aug 2020 15:17:49 +0300 Subject: [PATCH] fix: use transforms for vars with mapping --- src/core/build.ts | 4 ++-- src/core/replace-alias-to-variable.ts | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/core/build.ts b/src/core/build.ts index 00df3c4..cf2e030 100644 --- a/src/core/build.ts +++ b/src/core/build.ts @@ -30,7 +30,7 @@ StyleDictionaryApi.registerFormat({ const transformers = config.transforms.filter((transform) => transform.type === 'name') const props = options.useAliasVariables - ? replaceAliasToVariable(dictionary.allProperties, transformers[0].transformer) + ? replaceAliasToVariable(dictionary.allProperties, transformers) : dictionary.allProperties return `${selector} {\n${variablesWithPrefix(' --', props)}\n}\n` @@ -47,7 +47,7 @@ StyleDictionaryApi.registerFormat({ const transformers = config.transforms.filter((transform) => transform.type === 'name') const props = options.useAliasVariables - ? replaceAliasToVariable(dictionary.allProperties, transformers[0].transformer) + ? replaceAliasToVariable(dictionary.allProperties, transformers) : dictionary.allProperties return `${options.selector} {\n${variablesWithPrefix(' --', props)}\n}\n` diff --git a/src/core/replace-alias-to-variable.ts b/src/core/replace-alias-to-variable.ts index ce7560b..75d1031 100644 --- a/src/core/replace-alias-to-variable.ts +++ b/src/core/replace-alias-to-variable.ts @@ -1,4 +1,6 @@ import merge from 'deepmerge' +import { InjectedTransformer } from 'style-dictionary' + import { isAlias } from './utils' /** @@ -6,12 +8,20 @@ import { isAlias } from './utils' * @param props Props list * @param transformer Name transformer */ -export function replaceAliasToVariable>(props: T, transformer: any): T { +export function replaceAliasToVariable>( + props: T, + transformers: InjectedTransformer[], +): T { const nextProps = merge([], props) for (const prop of nextProps) { if (isAlias(prop.original.value)) { - const variableName = transformer({ path: prop.original.value.replace(/value/, '') }, {}) - prop.value = `var(--${variableName})` + const nextProp = transformers.reduce( + (prevProp, value) => { + return { path: prevProp.path, name: value.transformer(prevProp, {}) } + }, + { ...prop, path: prop.original.value.replace(/value/, '') }, + ) + prop.value = `var(--${nextProp.name})` } } return nextProps as T