diff --git a/src/core-v2/build.ts b/src/core-v2/build.ts index fbe9ac5..396a104 100644 --- a/src/core-v2/build.ts +++ b/src/core-v2/build.ts @@ -8,6 +8,7 @@ import { createWhitepaperConfig } from './whitepaper-config' import { variablesWithPrefix } from './variable-with-prefix' import { loadMappers } from './mappers' import { loadThemes } from './themes' +import { dedupeProps } from './dedupe-props' const store = new Map() @@ -69,6 +70,7 @@ export async function build(config: any): Promise { }) } const StyleDictionary = StyleDictionaryApi.extend(styleDictionaryConfig) + StyleDictionary.properties = dedupeProps(StyleDictionary.properties) StyleDictionary.buildPlatform('css') } } diff --git a/src/core-v2/dedupe-props.ts b/src/core-v2/dedupe-props.ts new file mode 100644 index 0000000..9702898 --- /dev/null +++ b/src/core-v2/dedupe-props.ts @@ -0,0 +1,24 @@ +import { Property } from 'style-dictionary' +import merge from 'deepmerge' + +function toDeepToken(path: string, prop: Property): any { + const chunks = path.split('-').reverse() + let result: any = prop + for (let i = 0; i < chunks.length; i++) { + result = { [chunks[i]]: result } + } + return result +} + +export function dedupeProps>(props: T): T { + let source = {} + let overrides = {} + for (const propKey in props) { + if (propKey.match(/-/)) { + overrides = merge(overrides, toDeepToken(propKey, props[propKey])) + } else { + source = merge(source, { [propKey]: props[propKey] }) + } + } + return merge(source, overrides) +}