Skip to content

Commit 72f020d

Browse files
tkgrootjorenbroekema
andauthoredMar 11, 2024··
fix: outputReferenceFallbacks for css/variables formatter (#1113)
- outputReferenceFallbacks options are passed through to the createPropertyFormatter function, which was previously not the case Signed-off-by: Tobias Kuppens Groot <tkuppensgroo@uos.de> Co-authored-by: Joren Broekema <joren.broekema@gmail.com>

File tree

5 files changed

+213
-2
lines changed

5 files changed

+213
-2
lines changed
 

‎.changeset/four-fireants-try.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'style-dictionary': patch
3+
---
4+
5+
Pass outputReferencesFallback option to the relevant utilities, so the option actually works.

‎__integration__/__snapshots__/css.test.snap.js

+176
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,179 @@ snapshots["integration css css/variables with selector should match snapshot"] =
529529
`;
530530
/* end snapshot integration css css/variables with selector should match snapshot */
531531

532+
snapshots["integration css css/variables with referenceFallbacks should match snapshot"] =
533+
`/**
534+
* Do not edit directly
535+
* Generated on Sat, 01 Jan 2000 00:00:00 GMT
536+
*/
537+
538+
:root {
539+
--size-padding-xl: 1rem;
540+
--size-padding-large: 1rem;
541+
--size-padding-medium: 1rem;
542+
--size-padding-small: 0.5rem;
543+
--size-font-xl: 2.25rem;
544+
--size-font-large: 1.5rem;
545+
--size-font-medium: 1rem;
546+
--size-font-small: 0.75rem;
547+
--size-border-radius-large: 30rem;
548+
--color-core-yellow-1100: #2d1a05;
549+
--color-core-yellow-1000: #542a00;
550+
--color-core-yellow-900: #944c0c;
551+
--color-core-yellow-800: #ba7506;
552+
--color-core-yellow-700: #dd9903;
553+
--color-core-yellow-600: #ffbc00;
554+
--color-core-yellow-500: #ffcd1c;
555+
--color-core-yellow-400: #ffd943;
556+
--color-core-yellow-300: #ffe16e;
557+
--color-core-yellow-200: #ffe99a;
558+
--color-core-yellow-100: #fdefcd;
559+
--color-core-yellow-0: #fff8e2;
560+
--color-core-neutral-1100: #040404;
561+
--color-core-neutral-1000: #162020;
562+
--color-core-neutral-900: #273333;
563+
--color-core-neutral-800: #364141;
564+
--color-core-neutral-700: #515e5f;
565+
--color-core-neutral-600: #6e797a;
566+
--color-core-neutral-500: #929a9b;
567+
--color-core-neutral-400: #b0b6b7;
568+
--color-core-neutral-300: #c8cccc;
569+
--color-core-neutral-200: #dee1e1;
570+
--color-core-neutral-100: #f3f4f4;
571+
--color-core-neutral-0: #ffffff;
572+
--color-core-orange-1100: #2d130e;
573+
--color-core-orange-1000: #601700;
574+
--color-core-orange-900: #962c0b;
575+
--color-core-orange-800: #ce5511;
576+
--color-core-orange-700: #ed7024;
577+
--color-core-orange-600: #f57d33;
578+
--color-core-orange-500: #fc8943;
579+
--color-core-orange-400: #ff9c5d;
580+
--color-core-orange-300: #ffb180;
581+
--color-core-orange-200: #ffc6a4;
582+
--color-core-orange-100: #fcdccc;
583+
--color-core-orange-0: #ffede3;
584+
--color-core-red-1100: #2b1111;
585+
--color-core-red-1000: #6d1313;
586+
--color-core-red-900: #992222;
587+
--color-core-red-800: #c63434;
588+
--color-core-red-700: #db3e3e;
589+
--color-core-red-600: #ed4c42;
590+
--color-core-red-500: #f76054;
591+
--color-core-red-400: #ff7f6e;
592+
--color-core-red-300: #ff9c8f;
593+
--color-core-red-200: #ffb8b1;
594+
--color-core-red-100: #ffd5d2;
595+
--color-core-red-0: #ffeae9;
596+
--color-core-pink-1100: #2b1721;
597+
--color-core-pink-1000: #561231;
598+
--color-core-pink-900: #931847;
599+
--color-core-pink-800: #b22f5b;
600+
--color-core-pink-700: #ce3665;
601+
--color-core-pink-600: #e0447c;
602+
--color-core-pink-500: #ef588b;
603+
--color-core-pink-400: #ff76ae;
604+
--color-core-pink-300: #ff95c1;
605+
--color-core-pink-200: #ffb5d5;
606+
--color-core-pink-100: #fcdbeb;
607+
--color-core-pink-0: #ffe9f3;
608+
--color-core-magenta-1100: #29192d;
609+
--color-core-magenta-1000: #451551;
610+
--color-core-magenta-900: #6c2277;
611+
--color-core-magenta-800: #8f3896;
612+
--color-core-magenta-700: #ac44a8;
613+
--color-core-magenta-600: #c44eb9;
614+
--color-core-magenta-500: #db61db;
615+
--color-core-magenta-400: #f282f5;
616+
--color-core-magenta-300: #edadf2;
617+
--color-core-magenta-200: #f4c4f7;
618+
--color-core-magenta-100: #f9e3fc;
619+
--color-core-magenta-0: #fef0ff;
620+
--color-core-purple-1100: #1d1d38;
621+
--color-core-purple-1000: #2d246b;
622+
--color-core-purple-900: #483a9c;
623+
--color-core-purple-800: #5e4eba;
624+
--color-core-purple-700: #6f5ed3;
625+
--color-core-purple-600: #816fea;
626+
--color-core-purple-500: #9180f4;
627+
--color-core-purple-400: #a193f2;
628+
--color-core-purple-300: #c1c1f7;
629+
--color-core-purple-200: #d8d7f9;
630+
--color-core-purple-100: #eaeaf9;
631+
--color-core-purple-0: #f2f2f9;
632+
--color-core-blue-1100: #002138;
633+
--color-core-blue-1000: #0a3960;
634+
--color-core-blue-900: #0c5689;
635+
--color-core-blue-800: #116daa;
636+
--color-core-blue-700: #2079c3;
637+
--color-core-blue-600: #2b87d3;
638+
--color-core-blue-500: #3896e3;
639+
--color-core-blue-400: #56adf5;
640+
--color-core-blue-300: #a1d2f8;
641+
--color-core-blue-200: #c7e4f9;
642+
--color-core-blue-100: #dcf2ff;
643+
--color-core-blue-0: #e9f8ff;
644+
--color-core-aqua-1100: #002838;
645+
--color-core-aqua-1000: #083d4f;
646+
--color-core-aqua-900: #035e73;
647+
--color-core-aqua-800: #0f6e84;
648+
--color-core-aqua-700: #0b8599;
649+
--color-core-aqua-600: #0797ae;
650+
--color-core-aqua-500: #17b8ce;
651+
--color-core-aqua-400: #33d6e2;
652+
--color-core-aqua-300: #76e5e2;
653+
--color-core-aqua-200: #a5f2f2;
654+
--color-core-aqua-100: #c5f9f9;
655+
--color-core-aqua-0: #d9fcfb;
656+
--color-core-teal-1100: #002528;
657+
--color-core-teal-1000: #083f3f;
658+
--color-core-teal-900: #026661;
659+
--color-core-teal-800: #067c7c;
660+
--color-core-teal-700: #0b968f;
661+
--color-core-teal-600: #00a99c;
662+
--color-core-teal-500: #08c4b2;
663+
--color-core-teal-400: #24e0c5;
664+
--color-core-teal-300: #7dead5;
665+
--color-core-teal-200: #b3f2e6;
666+
--color-core-teal-100: #cdf7ef;
667+
--color-core-teal-0: #e5f9f5;
668+
--color-core-green-1100: #002b20;
669+
--color-core-green-1000: #08422f;
670+
--color-core-green-900: #006b40;
671+
--color-core-green-800: #008b46;
672+
--color-core-green-700: #0ca750;
673+
--color-core-green-600: #2bb656;
674+
--color-core-green-500: #59cb59;
675+
--color-core-green-400: #75dd66;
676+
--color-core-green-300: #98e58e;
677+
--color-core-green-200: #c2f2bd;
678+
--color-core-green-100: #d7f4d7;
679+
--color-core-green-0: #ebf9eb;
680+
--breakpoint-sm: 768px;
681+
--breakpoint-xs: 304px;
682+
--color-font-success: var(--color-core-green-1000, #08422f);
683+
--color-font-warning: var(--color-core-orange-1000, #601700);
684+
--color-font-danger: var(--color-core-red-1000, #6d1313);
685+
--color-font-tertiary: var(--color-core-neutral-800, #364141);
686+
--color-font-secondary: var(--color-core-neutral-900, #273333);
687+
--color-font-primary: var(--color-core-neutral-1100, #040404);
688+
--color-brand-secondary: var(--color-core-purple-700, #6f5ed3);
689+
--color-brand-primary: var(--color-core-aqua-700, #0b8599);
690+
--color-border-primary: var(--color-core-neutral-300, #c8cccc);
691+
--color-background-info: var(--color-core-blue-0, #e9f8ff);
692+
--color-background-success: var(--color-core-green-0, #ebf9eb);
693+
--color-background-warning: var(--color-core-orange-0, #ffede3);
694+
--color-background-danger: var(--color-core-red-0, #ffeae9);
695+
--color-background-tertiary: var(--color-core-neutral-200, #dee1e1);
696+
--color-background-secondary: var(--color-core-neutral-100, #f3f4f4);
697+
--color-background-primary: var(--color-core-neutral-0, #ffffff);
698+
--breakpoint-md: calc(var(--breakpoint-xs, 304px) / var(--breakpoint-sm, 768px));
699+
--color-font-interactive-disabled: var(--color-font-tertiary, #364141);
700+
--color-font-interactive-active: var(--color-brand-secondary, #6f5ed3);
701+
--color-font-interactive-hover: var(--color-brand-primary, #0b8599);
702+
--color-font-interactive: var(--color-brand-primary, #0b8599);
703+
--color-background-disabled: var(--color-background-tertiary, #dee1e1);
704+
}
705+
`;
706+
/* end snapshot integration css css/variables with referenceFallbacks should match snapshot */
707+

‎__integration__/css.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ describe('integration', async () => {
4343
{
4444
destination: 'variablesWithReferences.css',
4545
format: 'css/variables',
46+
options: {
47+
outputReferences: true,
48+
outputReferenceFallbacks: false,
49+
},
50+
},
51+
{
52+
destination: 'variablesWithReferenceFallbacks.css',
53+
format: 'css/variables',
4654
options: {
4755
outputReferences: true,
4856
outputReferenceFallbacks: true,
@@ -84,6 +92,18 @@ describe('integration', async () => {
8492
});
8593
});
8694

95+
describe(`with referenceFallbacks`, async () => {
96+
it(`should match snapshot`, async () => {
97+
const output = fs.readFileSync(
98+
resolve(`${buildPath}variablesWithReferenceFallbacks.css`),
99+
{
100+
encoding: 'UTF-8',
101+
},
102+
);
103+
await expect(output).to.matchSnapshot();
104+
});
105+
});
106+
87107
describe(`with selector`, async () => {
88108
it(`should match snapshot`, async () => {
89109
const output = fs.readFileSync(resolve(`${buildPath}variablesWithSelector.css`), {

‎lib/common/formatHelpers/formattedVariables.js

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const defaultFormatting = {
3434
* @param {String} options.format - What type of variables to output. Options are: css, sass, less, and stylus
3535
* @param {Dictionary} options.dictionary - The dictionary object that gets passed to the formatter method.
3636
* @param {Boolean} [options.outputReferences] - Whether or not to output references
37+
* @param {Boolean} [options.outputReferenceFallbacks] - Whether or not to output a faLLback value for output references
3738
* @param {Formatting} [options.formatting] - Custom formatting properties that define parts of a declaration line in code. This will get passed to `formatHelpers.createPropertyFormatter` and used for the `lineSeparator` between lines of code.
3839
* @param {Boolean} [options.themeable] [false] - Whether tokens should default to being themeable.
3940
* @param {boolean} [options.usesDtcg] [false] - Whether DTCG token syntax should be uses.
@@ -56,6 +57,7 @@ export default function formattedVariables({
5657
format,
5758
dictionary,
5859
outputReferences = false,
60+
outputReferenceFallbacks,
5961
formatting = {},
6062
themeable = false,
6163
usesDtcg = false,
@@ -85,6 +87,7 @@ export default function formattedVariables({
8587
.map(
8688
createPropertyFormatter({
8789
outputReferences,
90+
outputReferenceFallbacks,
8891
dictionary,
8992
format,
9093
formatting,

‎lib/common/formats.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const formats = {
7171
* @typedef {Object} cssVariablesOpts
7272
* @property {Boolean} [cssVariablesOpts.showFileHeader=true] - Whether or not to include a comment that has the build date
7373
* @property {Boolean} [cssVariablesOpts.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output.
74+
* @property {Boolean} [cssVariablesOpts.outputReferenceFallbacks=false] - Whether or not to add a fallback value for the output reference.
7475
* @property {string} [cssVariablesOpts.selector] - Override the root css selector
7576
* @param {FormatOpts & { options?: cssVariablesOpts}} options
7677
* @example
@@ -83,12 +84,18 @@ const formats = {
8384
*/
8485
'css/variables': async function ({ dictionary, options = {}, file }) {
8586
const selector = options.selector ? options.selector : `:root`;
86-
const { outputReferences, usesDtcg } = options;
87+
const { outputReferences, outputReferenceFallbacks, usesDtcg } = options;
8788
const header = await fileHeader({ file });
8889
return (
8990
header +
9091
`${selector} {\n` +
91-
formattedVariables({ format: 'css', dictionary, outputReferences, usesDtcg }) +
92+
formattedVariables({
93+
format: 'css',
94+
dictionary,
95+
outputReferences,
96+
outputReferenceFallbacks,
97+
usesDtcg,
98+
}) +
9299
`\n}\n`
93100
);
94101
},

0 commit comments

Comments
 (0)
Please sign in to comment.