Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass dictionary options to preprocessors #1224

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-mangos-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Pass dictionary options to preprocessor functions.
24 changes: 24 additions & 0 deletions __tests__/register/preprocessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,28 @@ describe('register/transformGroup', async () => {
},
});
});

it('should pass options to preprocessor function as second argument', async () => {
let opts;
StyleDictionary.registerPreprocessor({
name: 'foo-processor',
preprocessor: async (dict, options) => {
opts = options;
return dict;
},
});

StyleDictionaryExtended = new StyleDictionary({
preprocessors: ['foo-processor'],
tokens: {
foo: {
$value: '4px',
$type: 'dimension',
$description: 'Foo description',
},
},
});
await StyleDictionaryExtended.hasInitialized;
expect(opts.usesDtcg).to.be.true;
});
});
7 changes: 6 additions & 1 deletion lib/StyleDictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,13 @@ export default class StyleDictionary extends Register {
if (this.shouldRunExpansion(this.expand)) {
tokens = expandTokens(tokens, this.options);
}
this.tokens = await preprocess(tokens, this.preprocessors, this.hooks.preprocessors);
this.options = { ...this.options, usesDtcg: this.usesDtcg };
this.tokens = await preprocess(
tokens,
this.preprocessors,
this.hooks.preprocessors,
this.options,
);
this.hasInitializedResolve(null);

// For chaining
Expand Down
11 changes: 9 additions & 2 deletions lib/utils/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/**
* @typedef {import('../../types/DesignToken.d.ts').DesignTokens} DesignTokens
* @typedef {import('../../types/Config.d.ts').Config} Config
* @typedef {import('../../types/Preprocessor.d.ts').Preprocessor} Preprocessor
*/

Expand All @@ -23,16 +24,22 @@
* @param {DesignTokens} tokens
* @param {string[]} [appliedPreprocessors]
* @param {Record<string, Preprocessor['preprocessor']>} [preprocessorObj]
* @param {Config} [options]
* @returns {Promise<DesignTokens>}
*/
export async function preprocess(tokens, appliedPreprocessors = [], preprocessorObj = {}) {
export async function preprocess(
tokens,
appliedPreprocessors = [],
preprocessorObj = {},
options = {},
) {
let processedTokens = tokens;

const preprocessors = Object.entries(preprocessorObj);
if (preprocessors.length > 0) {
for (const [key, preprocessor] of preprocessors) {
if (appliedPreprocessors.includes(key)) {
processedTokens = await preprocessor(processedTokens);
processedTokens = await preprocessor(processedTokens, options);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion types/Preprocessor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
*/

import type { DesignTokens } from './DesignToken.d.ts';
import type { Config } from './Config.d.ts';

export type Preprocessor = {
name: string;
preprocessor: (dictionary: DesignTokens) => DesignTokens | Promise<DesignTokens>;
preprocessor: (dictionary: DesignTokens, options: Config) => DesignTokens | Promise<DesignTokens>;
};
Loading