-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
javascript-esm.ts
17 lines (15 loc) · 863 Bytes
/
javascript-esm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import StyleDictionary from 'style-dictionary';
import type { Format } from 'style-dictionary/types';
// Currently, Style-Dictionary does not yet support async formats, and prettier has changed to async now from v3 onwards
import syncPrettier from '@prettier/sync'
import { jsonToNestedValue } from '../utilities/jsonToNestedValue.js'
const { fileHeader } = StyleDictionary.formatHelpers
export const javascriptEsm: Format['formatter'] = async ({ dictionary, file, options, platform = {} }) => {
const { prefix } = platform
const tokens = prefix ? { [prefix]: dictionary.tokens } : dictionary.tokens
//
const output = await fileHeader({ file }) +
`export default \n${JSON.stringify(jsonToNestedValue(tokens), null, 2)}\n`
// return prettified
return syncPrettier.format(output, { parser: 'typescript', printWidth: 500, ...options?.prettier })
}