diff --git a/src/core/style-dictionary-config.ts b/src/core/style-dictionary-config.ts index e4b4e9e..7fcf77d 100644 --- a/src/core/style-dictionary-config.ts +++ b/src/core/style-dictionary-config.ts @@ -1,5 +1,7 @@ import { Platform, Config } from 'style-dictionary' +import { Api } from '../index' + type Options = { sources: string[] entry: string @@ -12,6 +14,17 @@ export function createStyleDictionaryConfig({ sources, entry, platform, output } // prettier-ignore .reduce>((acc, [key, value]) => { const target = { ...value } + + if (target.preset !== undefined) { + const maybePrese = Api.presets.get(target.preset) + if (maybePrese === undefined) { + throw new Error(`Used unexpected preset "${target.preset}" for "${key}" platform.`) + } else { + target.transforms = maybePrese.transforms || target.transforms + target.actions = maybePrese.actions || target.actions + } + } + // Normalize path for style-dictionary specifics. target.buildPath = target.buildPath.endsWith('/') ? target.buildPath : `${target.buildPath}/` target.files = target.files.map((file) => ({ diff --git a/src/index.h.ts b/src/index.h.ts new file mode 100644 index 0000000..a20beb7 --- /dev/null +++ b/src/index.h.ts @@ -0,0 +1,14 @@ +export type Preset = { + /** + * Preset name + */ + name: string + /** + * Predefined transforms + */ + transforms?: string[] + /** + * Predefined actions + */ + actions?: string[] +} diff --git a/src/index.ts b/src/index.ts index b78bca0..3ffb564 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,28 @@ import StyleDictionary from 'style-dictionary' +import { Preset } from './index.h' + export const Api = { registerFormat: StyleDictionary.registerFormat.bind(StyleDictionary), registerTransform: StyleDictionary.registerTransform.bind(StyleDictionary), registerAction: StyleDictionary.registerAction.bind(StyleDictionary), registerFilter: StyleDictionary.registerFilter.bind(StyleDictionary), + + /** + * Map with registered presets + * + * @internal + */ + presets: new Map(), + + /** + * Add new or override existing preset + * + * @param preset - Preset settings + */ + registerPreset: (preset: Preset): void => { + Api.presets.set(preset.name, preset) + }, } /** diff --git a/src/modules.d.ts b/src/modules.d.ts index 6bf51f0..b06f27e 100644 --- a/src/modules.d.ts +++ b/src/modules.d.ts @@ -13,6 +13,7 @@ declare module 'style-dictionary' { } export type Platform = { + preset?: string buildPath: string transforms?: string[] actions?: string[]