Skip to content

Commit 6ff17ec

Browse files
DarioSollerDario Soller
and
Dario Soller
authored
fix: guaranteed order of preprocessor execution (#1489)
* fix: guaranteed order of preprocessor execution * test: add regression test for guaranteed order of preprocessor execution * chore: add changeset --------- Co-authored-by: Dario Soller <[email protected]>
1 parent ee85609 commit 6ff17ec

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

.changeset/swift-buses-march.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'style-dictionary': patch
3+
---
4+
5+
If several preprocessors are defined in the SD configuration, the execution of the preprocessors is now guaranteed in the exact order in which they were configured in the SD configuration.

__tests__/utils/preprocess.test.js

+29
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,34 @@ describe('utils', () => {
6464
value: '5px',
6565
});
6666
});
67+
68+
it('should support asynchronous preprocessors in the order of the config array', async () => {
69+
const output = await preprocess(
70+
{
71+
foo: {
72+
value: '5px',
73+
},
74+
},
75+
['preprocessorA', 'preprocessorC', 'preprocessorB'],
76+
{
77+
preprocessorB: (tokens) => {
78+
tokens.baz = tokens.qux;
79+
return tokens;
80+
},
81+
preprocessorC: async (tokens) => {
82+
await new Promise((resolve) => setTimeout(resolve, 100));
83+
tokens.qux = tokens.bar;
84+
return tokens;
85+
},
86+
preprocessorA: (tokens) => {
87+
tokens.bar = tokens.foo;
88+
return tokens;
89+
},
90+
},
91+
);
92+
expect(output).to.have.property('baz').eql({
93+
value: '5px',
94+
});
95+
});
6796
});
6897
});

lib/utils/preprocess.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ export async function preprocess(
3838

3939
const preprocessors = Object.entries(preprocessorObj);
4040
if (preprocessors.length > 0) {
41-
for (const [key, preprocessor] of preprocessors) {
42-
if (appliedPreprocessors.includes(key)) {
41+
for (const key of appliedPreprocessors) {
42+
const preprocessor = preprocessorObj[key];
43+
if (preprocessor) {
4344
processedTokens = await preprocessor(processedTokens, options);
4445
}
4546
}

0 commit comments

Comments
 (0)