Skip to content

Commit

Permalink
feat: align preprocessor API, adjust expand API
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Apr 30, 2024
1 parent 3486daa commit 1cf13d4
Show file tree
Hide file tree
Showing 22 changed files with 674 additions and 403 deletions.
5 changes: 0 additions & 5 deletions .changeset/fuzzy-laws-hammer.md

This file was deleted.

46 changes: 46 additions & 0 deletions .changeset/short-jokes-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
'style-dictionary': major
---

BREAKING: preprocessors must now also be explicitly applied on global or platform level, rather than only registering it. This is more consistent with how the other hooks work and allows applying it on a platform level rather than only on the global level.

`preprocessors` property that contains the registered preprocessors has been moved under a wrapping property called `hooks`.

Before:

```js
export default {
// register it inline or by SD.registerPreprocessor
// applies automatically, globally
preprocessors: {
foo: (dictionary) => {
// preprocess it
return dictionary;
}
}
}
```

After:

```js
export default {
// register it inline or by SD.registerPreprocessor
hooks: {
preprocessors: {
foo: (dictionary) => {
// preprocess it
return dictionary;
}
}
},
// apply it globally
preprocessors: ['foo'],
platforms: {
css: {
// or apply is per platform
preprocessors: ['foo']
}
}
}
```
2 changes: 1 addition & 1 deletion .changeset/sweet-toes-fly.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
'style-dictionary': minor
'style-dictionary': major
---

BREAKING: expose getReferences and usesReference utilities as standalone utils rather than requiring them to be bound to dictionary object. This makes it easier to use.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ This tells the style dictionary build system how and what to build. The default
}
```

| Attribute | Type | Description |
| Property | Type | Description |
| :----------------------------------- | :---------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| source | Array | An array of file path [globs](https://github.com/isaacs/node-glob) to design token files. Style Dictionary will do a deep merge of all of the token files, allowing you to organize your files files however you want. |
| include | Array | An array of file path [globs](https://github.com/isaacs/node-glob) to design token files that contain default styles. The Style Dictionary uses this as a base collection of tokens. The tokens found using the "source" attribute will overwrite tokens found using include. |
Expand Down
1 change: 1 addition & 0 deletions __integration__/async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('integration', async function () {

const sd = new SDExtension({
source: [`__integration__/tokens/**/[!_]*.json?(c)`],
preprocessors: ['foo-processor'],
platforms: {
css: {
transforms: [
Expand Down
45 changes: 18 additions & 27 deletions __tests__/StyleDictionary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,6 @@ describe('StyleDictionary class + extend method', () => {
const sd = new StyleDictionary({
tokens: input,
expand: true,
expandTypesMap: {
border: {
style: 'other',
},
},
});
await sd.hasInitialized;
expect(sd.tokens).to.eql({
Expand All @@ -377,11 +372,11 @@ describe('StyleDictionary class + extend method', () => {
value: '#000',
},
style: {
type: 'other',
type: 'strokeStyle',
value: 'solid',
},
width: {
type: 'width',
type: 'dimension',
value: '2px',
},
},
Expand All @@ -401,11 +396,6 @@ describe('StyleDictionary class + extend method', () => {
};
const sd = new StyleDictionary({
tokens: input,
expandTypesMap: {
border: {
style: 'other',
},
},
platforms: {
css: {
expand: true,
Expand All @@ -423,11 +413,11 @@ describe('StyleDictionary class + extend method', () => {
value: '#000',
},
style: {
type: 'other',
type: 'strokeStyle',
value: 'solid',
},
width: {
type: 'width',
type: 'dimension',
value: '2px',
},
},
Expand Down Expand Up @@ -456,17 +446,18 @@ describe('StyleDictionary class + extend method', () => {
};
const sd = new StyleDictionary({
tokens: input,
expand: (token) => {
return token.value.width === '2px';
},
expandTypesMap: {
border: {
style: 'other',
expand: {
include: (token) => {
return token.value.width === '2px';
},
},
platforms: {
css: {},
js: { expand: true },
js: {
expand: {
typesMap: true,
},
},
},
});
await sd.hasInitialized;
Expand All @@ -480,11 +471,11 @@ describe('StyleDictionary class + extend method', () => {
value: '#000',
},
style: {
type: 'other',
type: 'strokeStyle',
value: 'solid',
},
width: {
type: 'width',
type: 'dimension',
value: '2px',
},
},
Expand All @@ -497,11 +488,11 @@ describe('StyleDictionary class + extend method', () => {
value: '#000',
},
style: {
type: 'other',
type: 'strokeStyle',
value: 'solid',
},
width: {
type: 'width',
type: 'dimension',
value: '2px',
},
},
Expand All @@ -511,11 +502,11 @@ describe('StyleDictionary class + extend method', () => {
value: '#ccc',
},
style: {
type: 'other',
type: 'strokeStyle',
value: 'dashed',
},
width: {
type: 'width',
type: 'dimension',
value: '1px',
},
},
Expand Down
9 changes: 6 additions & 3 deletions __tests__/register/preprocessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ registerSuite({
},
registerMethod: 'registerPreprocessor',
prop: 'preprocessors',
hooks: true,
});

describe('register/transformGroup', async () => {
let StyleDictionaryExtended;
beforeEach(async () => {
StyleDictionary.preprocessors = {};
StyleDictionary.hooks.preprocessors = {};
StyleDictionaryExtended = new StyleDictionary({});
await StyleDictionaryExtended.hasInitialized;
});
Expand All @@ -35,8 +36,8 @@ describe('register/transformGroup', async () => {
name: 'example-preprocessor',
preprocessor: (dict) => dict,
});
expect(StyleDictionary.preprocessors['example-preprocessor']).to.not.be.undefined;
expect(StyleDictionaryExtended.preprocessors['example-preprocessor']).to.not.be.undefined;
expect(StyleDictionary.hooks.preprocessors['example-preprocessor']).to.not.be.undefined;
expect(StyleDictionaryExtended.hooks.preprocessors['example-preprocessor']).to.not.be.undefined;
});

it('should throw if the preprocessor name is not a string', () => {
Expand Down Expand Up @@ -76,6 +77,7 @@ describe('register/transformGroup', async () => {
});

StyleDictionaryExtended = new StyleDictionary({
preprocessors: ['strip-descriptions'],
tokens: {
foo: {
value: '4px',
Expand Down Expand Up @@ -120,6 +122,7 @@ describe('register/transformGroup', async () => {
});

StyleDictionaryExtended = new StyleDictionary({
preprocessors: ['strip-descriptions'],
tokens: {
foo: {
value: '4px',
Expand Down
59 changes: 42 additions & 17 deletions __tests__/register/register.suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export function registerSuite(opts) {

describe('Register Test Suite', () => {
const reset = () => {
StyleDictionary[prop] = defaultPropVal;
if (opts.hooks) {
StyleDictionary.hooks[prop] = defaultPropVal;
} else {
StyleDictionary[prop] = defaultPropVal;
}
};
beforeEach(() => {
reset();
Expand All @@ -33,10 +37,15 @@ export function registerSuite(opts) {
const sd1 = new StyleDictionary();
const sd2 = new StyleDictionary();
const sd3 = await sd2.extend();

expect(sd1[prop][configFoo.name]).to.not.be.undefined;
expect(sd2[prop][configFoo.name]).to.not.be.undefined;
expect(sd3[prop][configFoo.name]).to.not.be.undefined;
if (opts.hooks) {
expect(sd1.hooks[prop][configFoo.name]).to.not.be.undefined;
expect(sd2.hooks[prop][configFoo.name]).to.not.be.undefined;
expect(sd3.hooks[prop][configFoo.name]).to.not.be.undefined;
} else {
expect(sd1[prop][configFoo.name]).to.not.be.undefined;
expect(sd2[prop][configFoo.name]).to.not.be.undefined;
expect(sd3[prop][configFoo.name]).to.not.be.undefined;
}
});

it(`should allow registering ${prop} on instance, affecting only that instance`, async () => {
Expand All @@ -45,10 +54,15 @@ export function registerSuite(opts) {
const sd3 = await sd2.extend();

sd2[registerMethod](configFoo);

expect(sd1[prop][configFoo.name]).to.be.undefined;
expect(sd2[prop][configFoo.name]).to.not.be.undefined;
expect(sd3[prop][configFoo.name]).to.be.undefined;
if (opts.hooks) {
expect(sd1.hooks[prop][configFoo.name]).to.be.undefined;
expect(sd2.hooks[prop][configFoo.name]).to.not.be.undefined;
expect(sd3.hooks[prop][configFoo.name]).to.be.undefined;
} else {
expect(sd1[prop][configFoo.name]).to.be.undefined;
expect(sd2[prop][configFoo.name]).to.not.be.undefined;
expect(sd3[prop][configFoo.name]).to.be.undefined;
}
});

it(`should combine class and instance registrations for ${prop} on the instance`, async () => {
Expand All @@ -59,14 +73,25 @@ export function registerSuite(opts) {
sd2[registerMethod](configBar);
const sd3 = await sd2.extend();

expect(sd1[prop][configFoo.name]).to.not.be.undefined;
expect(sd2[prop][configFoo.name]).to.not.be.undefined;
expect(sd3[prop][configFoo.name]).to.not.be.undefined;
// should not be registered on sd1, because we registered only on sd2
expect(sd1[prop][configBar.name]).to.be.undefined;
expect(sd2[prop][configBar.name]).to.not.be.undefined;
// should be registered because sd3 extends sd2
expect(sd3[prop][configBar.name]).to.not.be.undefined;
if (opts.hooks) {
expect(sd1.hooks[prop][configFoo.name]).to.not.be.undefined;
expect(sd2.hooks[prop][configFoo.name]).to.not.be.undefined;
expect(sd3.hooks[prop][configFoo.name]).to.not.be.undefined;
// should not be registered on sd1, because we registered only on sd2
expect(sd1.hooks[prop][configBar.name]).to.be.undefined;
expect(sd2.hooks[prop][configBar.name]).to.not.be.undefined;
// should be registered because sd3 extends sd2
expect(sd3.hooks[prop][configBar.name]).to.not.be.undefined;
} else {
expect(sd1[prop][configFoo.name]).to.not.be.undefined;
expect(sd2[prop][configFoo.name]).to.not.be.undefined;
expect(sd3[prop][configFoo.name]).to.not.be.undefined;
// should not be registered on sd1, because we registered only on sd2
expect(sd1[prop][configBar.name]).to.be.undefined;
expect(sd2[prop][configBar.name]).to.not.be.undefined;
// should be registered because sd3 extends sd2
expect(sd3[prop][configBar.name]).to.not.be.undefined;
}
});
});
});
Expand Down
Loading

0 comments on commit 1cf13d4

Please sign in to comment.