Skip to content

Commit f9b8bd3

Browse files
working for borders
1 parent 3e5c407 commit f9b8bd3

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

scripts/buildTokens.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {Config} from 'style-dictionary/types'
1+
import type {Config, LogConfig} from 'style-dictionary/types'
22
import {PrimerStyleDictionary} from '../src/primerStyleDictionary.js'
33
import {copyFromDir} from '../src/utilities/index.js'
44
import {deprecatedJson, css, docJson, fallbacks, styleLint} from '../src/platforms/index.js'
@@ -12,6 +12,14 @@ import {themes} from './themes.config.js'
1212
import fs from 'fs'
1313
import {getFallbackTheme} from './utilities/getFallbackTheme.js'
1414

15+
const log: LogConfig = {
16+
warnings: 'disabled', // 'warn' | 'error' | 'disabled'
17+
verbosity: 'silent', // 'default' | 'silent' | 'verbose'
18+
errors: {
19+
brokenReferences: 'throw', // 'throw' | 'console'
20+
},
21+
}
22+
1523
/**
1624
* getStyleDictionaryConfig
1725
* @param filename output file name without extension
@@ -29,13 +37,7 @@ const getStyleDictionaryConfig: StyleDictionaryConfigGenerator = (
2937
): Config => ({
3038
source, // build the special formats
3139
include,
32-
log: {
33-
warnings: 'disabled', // 'warn' | 'error' | 'disabled'
34-
verbosity: 'silent', // 'default' | 'silent' | 'verbose'
35-
errors: {
36-
brokenReferences: 'throw', // 'throw' | 'console'
37-
},
38-
},
40+
log,
3941
platforms: Object.fromEntries(
4042
Object.entries({
4143
css: css(`css/${filename}.css`, options.prefix, options.buildPath, {
@@ -64,6 +66,7 @@ export const buildDesignTokens = async (buildOptions: ConfigGeneratorOptions): P
6466
const extendedSD = await PrimerStyleDictionary.extend({
6567
source: [...source, ...include], // build the special formats
6668
include,
69+
log,
6770
platforms: {
6871
css: css(`internalCss/${filename}.css`, buildOptions.prefix, buildOptions.buildPath, {
6972
themed: true,

src/formats/utilities/createPropertyFormatterWithRef.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,7 @@ export default function createPropertyFormatterWithRef({
192192
})
193193
.join(' ')
194194
}
195-
// if (token.attributes.referenceMap) {
196-
// // console.log(token)
197-
// // console.log('original is object', token.attributes || '')
198-
// value = token.attributes.referenceMap
199-
// // console.log('referenceMap', token.attributes.referenceMap)
200-
// for (const [key, val] of Object.entries(originalValue)) {
201-
// const regex = new RegExp(`\\[${key}\\]`)
202-
// value = value.replace(regex, val)
203-
// }
204-
// }
205195
}
206-
// console.log('originalValue:', originalValue, JSON.stringify(originalIsObject))
207-
// if (ref.$type === 'border') {
208-
// console.log('BORDER:', ref.original)
209-
// }
210-
// console.log('refs: ', refs)
211196

212197
refs.forEach(ref => {
213198
// value should be a string that contains the resolved reference

src/platforms/css.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {isFromFile, isSource} from '../filters/index.js'
22
import type {PlatformInitializer} from '../types/platformInitializer.js'
33
import type {PlatformConfig, TransformedToken} from 'style-dictionary/types'
44
import {outputReferencesTransformed, outputReferencesFilter} from 'style-dictionary/utils'
5+
import {outputReferencesTransformedWithObject} from './utilities/outputReferencesTransformedWithObject.js'
56

67
const getCssSelectors = (outputFile: string) => {
78
// check for dark in the beginning of the output filename
@@ -58,9 +59,10 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
5859
]),
5960
options: {
6061
showFileHeader: false,
61-
outputReferences: true,
62-
// outputReferences: (token, platformOptions) =>
63-
// outputReferencesFilter(token, platformOptions) && outputReferencesTransformed(token, platformOptions),
62+
// outputReferences: true,
63+
outputReferences: (token, platformOptions) =>
64+
outputReferencesFilter(token, platformOptions) &&
65+
outputReferencesTransformedWithObject(token, platformOptions),
6466
descriptions: false,
6567
queries: getCssSelectors(outputFile),
6668
...options?.options,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type {Dictionary, TransformedToken} from 'style-dictionary/types'
2+
import {resolveReferences} from 'style-dictionary/utils'
3+
4+
export function outputReferencesTransformedWithObject(
5+
token: TransformedToken,
6+
{dictionary, usesDtcg}: {dictionary: Dictionary; usesDtcg?: boolean},
7+
): boolean {
8+
const originalValue = usesDtcg ? token.original.$value : token.original.value
9+
const value = usesDtcg ? token.$value : token.value
10+
11+
// double check if this is a string, technically speaking the token could also be an object
12+
// and pass the usesReferences check
13+
if (typeof originalValue === 'string') {
14+
// Check if the token's value is the same as if we were resolve references on the original value
15+
// This checks whether the token's value has been transformed e.g. transitive transforms.
16+
// If it has been, that means we should not be outputting refs because this would undo the work of those transforms.
17+
return (
18+
value ===
19+
resolveReferences(originalValue, dictionary.unfilteredTokens ?? dictionary.tokens, {
20+
usesDtcg,
21+
warnImmediately: false,
22+
})
23+
)
24+
}
25+
if (typeof originalValue === 'object') {
26+
const values = Object.values(originalValue).filter(val => typeof val === 'string')
27+
return values.every(val =>
28+
values.includes(
29+
resolveReferences(val, dictionary.unfilteredTokens ?? dictionary.tokens, {
30+
usesDtcg,
31+
warnImmediately: false,
32+
}),
33+
),
34+
)
35+
}
36+
return false
37+
}

0 commit comments

Comments
 (0)