import { fileHeader } from 'style-dictionary/utils'; import { type FormatFn, type Dictionary } from 'style-dictionary/types'; import { tailwindTokenFilesMapper } from './tailwindTokenFilesMapper.js'; const PREFIX = 'mfui'; /** * Creates a token object from the dictionary for the Tailwind theme. * * @param dictionary - The dictionary containing all the tokens. * @param fileMatchPattern - The file match pattern to filter the tokens. * @returns The Tailwind theme object with the tokens. */ const createTokenObject = (dictionary: Dictionary, fileMatchPattern: string | RegExp) => { const tokens: { [key: string]: string | { [key: string]: string } } = {}; dictionary.allTokens .filter((token) => token.filePath.match(fileMatchPattern)) .forEach((token) => { const name = `${PREFIX}-${token.name}`; tokens[name] = String(token.$value); }); return tokens; }; /** * Creates a shadow token object from the dictionary for the Tailwind theme. * * @param dictionary - The dictionary containing all the tokens. * @param fileMatchPattern - The file match pattern to filter the tokens. * @returns The Tailwind theme object with the shadow tokens. */ const createShadowTokenObject = (dictionary: Dictionary, fileMatchPattern: string | RegExp) => { const shadowTokens: { [key: string]: string } = {}; dictionary.allTokens .filter((token) => token.filePath.match(fileMatchPattern)) .forEach((token) => { const tokenName = `${PREFIX}-${token.name}`; const { offsetX, offsetY, blur, spread, color } = token.$value; shadowTokens[tokenName] = `${offsetX} ${offsetY} ${blur} ${spread} ${color}`; }); return shadowTokens; }; export const tailwindV3PresetFormat: FormatFn = async ({ dictionary, file }) => { const theme: { [key: string]: any } = {}; tailwindTokenFilesMapper.forEach(({ themeProperty, fileMatchPattern }) => { // Some tokens require special handling based on the token type const tokenCreator = (() => { if (themeProperty === 'boxShadow') { return createShadowTokenObject; } return createTokenObject; })(); const newObject = { ...theme[themeProperty], ...tokenCreator(dictionary, fileMatchPattern), }; theme[themeProperty] = newObject; }); return (await fileHeader({ file })) + `module.exports = ${JSON.stringify({ theme }, null, 2)};`; };