Skip to content

Commit

Permalink
feat: use tokenMap structure to increase performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Nov 26, 2024
1 parent f694f67 commit d15081d
Show file tree
Hide file tree
Showing 16 changed files with 366 additions and 99 deletions.
80 changes: 54 additions & 26 deletions __integration__/__snapshots__/customFormats.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"small"
]
],
"key": "{size.padding.small}"
},
{
"value": "1rem",
Expand All @@ -135,7 +136,8 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"medium"
]
],
"key": "{size.padding.medium}"
},
{
"value": "1rem",
Expand All @@ -156,7 +158,8 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"large"
]
],
"key": "{size.padding.large}"
},
{
"value": "1rem",
Expand All @@ -177,9 +180,11 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"xl"
]
],
"key": "{size.padding.xl}"
}
],
"tokenMap": {},
"unfilteredTokens": {
"size": {
"padding": {
Expand Down Expand Up @@ -290,7 +295,8 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"small"
]
],
"key": "{size.padding.small}"
},
{
"value": "1rem",
Expand All @@ -311,7 +317,8 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"medium"
]
],
"key": "{size.padding.medium}"
},
{
"value": "1rem",
Expand All @@ -332,7 +339,8 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"large"
]
],
"key": "{size.padding.large}"
},
{
"value": "1rem",
Expand All @@ -353,9 +361,11 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"xl"
]
],
"key": "{size.padding.xl}"
}
]
],
"unfilteredTokenMap": {}
},
"allTokens": [
{
Expand All @@ -377,7 +387,8 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"small"
]
],
"key": "{size.padding.small}"
},
{
"value": "1rem",
Expand All @@ -398,7 +409,8 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"medium"
]
],
"key": "{size.padding.medium}"
},
{
"value": "1rem",
Expand All @@ -419,7 +431,8 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"large"
]
],
"key": "{size.padding.large}"
},
{
"value": "1rem",
Expand All @@ -440,7 +453,8 @@ snapshots["integration custom formats inline custom with new args should match s
"size",
"padding",
"xl"
]
],
"key": "{size.padding.xl}"
}
],
"tokens": {
Expand Down Expand Up @@ -1075,7 +1089,8 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"small"
]
],
"key": "{size.padding.small}"
},
{
"value": "1rem",
Expand All @@ -1096,7 +1111,8 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"medium"
]
],
"key": "{size.padding.medium}"
},
{
"value": "1rem",
Expand All @@ -1117,7 +1133,8 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"large"
]
],
"key": "{size.padding.large}"
},
{
"value": "1rem",
Expand All @@ -1138,9 +1155,11 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"xl"
]
],
"key": "{size.padding.xl}"
}
],
"tokenMap": {},
"unfilteredTokens": {
"size": {
"padding": {
Expand Down Expand Up @@ -1251,7 +1270,8 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"small"
]
],
"key": "{size.padding.small}"
},
{
"value": "1rem",
Expand All @@ -1272,7 +1292,8 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"medium"
]
],
"key": "{size.padding.medium}"
},
{
"value": "1rem",
Expand All @@ -1293,7 +1314,8 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"large"
]
],
"key": "{size.padding.large}"
},
{
"value": "1rem",
Expand All @@ -1314,9 +1336,11 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"xl"
]
],
"key": "{size.padding.xl}"
}
]
],
"unfilteredTokenMap": {}
},
"allTokens": [
{
Expand All @@ -1338,7 +1362,8 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"small"
]
],
"key": "{size.padding.small}"
},
{
"value": "1rem",
Expand All @@ -1359,7 +1384,8 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"medium"
]
],
"key": "{size.padding.medium}"
},
{
"value": "1rem",
Expand All @@ -1380,7 +1406,8 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"large"
]
],
"key": "{size.padding.large}"
},
{
"value": "1rem",
Expand All @@ -1401,7 +1428,8 @@ snapshots["integration custom formats register custom format with new args shoul
"size",
"padding",
"xl"
]
],
"key": "{size.padding.xl}"
}
],
"tokens": {
Expand Down
4 changes: 0 additions & 4 deletions __integration__/__snapshots__/iOSObjectiveC.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ snapshots["integration ios objective-c ios/singleton.m should match snapshot"] =
@"category": @"color",
@"type": @"border",
@"item": @"primary"
},
@"secondary": @
},
@"tertiary": @
}
},
@"brand": @{
Expand Down
24 changes: 3 additions & 21 deletions __integration__/__snapshots__/scss.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,13 +877,7 @@ $design-system-tokens: (
'disabled': $color-background-disabled
),
'border': (
'primary': $color-border-primary,
'secondary': (
),
'tertiary': (
)
'primary': $color-border-primary
),
'brand': (
'primary': $color-brand-primary,
Expand Down Expand Up @@ -1265,13 +1259,7 @@ $design-system-tokens: (
'disabled': $color-background-disabled
),
'border': (
'primary': $color-border-primary,
'secondary': (
),
'tertiary': (
)
'primary': $color-border-primary
),
'brand': (
'primary': $color-brand-primary,
Expand Down Expand Up @@ -1653,13 +1641,7 @@ $design-system-tokens: (
'disabled': $color-background-disabled
),
'border': (
'primary': $color-border-primary,
'secondary': (
),
'tertiary': (
)
'primary': $color-border-primary
),
'brand': (
'primary': $color-brand-primary,
Expand Down
3 changes: 1 addition & 2 deletions __perf_tests__/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ describe('cliBuildWithJsConfig', () => {
expect(end - start).to.be.below(70);
});

// TODO: aim for <1000ms (maybe a bit more margin due to CI CPUs being slower)
it('should run tons of refs within 2750ms', async () => {
it.only('should run tons of refs within 1000ms', async () => {

Check failure on line 111 in __perf_tests__/basic.test.js

View workflow job for this annotation

GitHub Actions / Verify changes (18, ubuntu-latest)

Unexpected exclusive mocha test

Check failure on line 111 in __perf_tests__/basic.test.js

View workflow job for this annotation

GitHub Actions / Verify changes (21.x, ubuntu-latest)

Unexpected exclusive mocha test
// 9000 tokens, 6000 refs
// (first layer is raw values, other 2 layers are refs to previous layer)
const fontWeightTokens = generateTokens({
Expand Down
4 changes: 3 additions & 1 deletion __tests__/buildPlatform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ describe('buildPlatform', () => {
expect(typeof output.object.otherTest.value).to.equal('object');
});

it('should handle non-property nodes', async () => {
// we shouldn't... this is kinda random and only makes sense
// for formats that output nested maps? so that feels weird
it.skip('should handle non-property nodes', async () => {
const StyleDictionaryExtended = new StyleDictionary({
source: ['__tests__/__tokens/nonPropertyNode.json'],
platforms: {
Expand Down
11 changes: 8 additions & 3 deletions __tests__/filterTokens.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ describe('filterTokens', () => {
expect(token).to.not.equal(colorRed);
expect(token).not.to.not.equal(colorBlue);
});
expect(filteredDictionary.allTokens).to.eql([sizeSmall, sizeLarge]);
expect(filteredDictionary.allTokens).to.eql([
{ ...sizeSmall, key: '{size.small}' },
{ ...sizeLarge, key: '{size.large}' },
]);
expect(filteredDictionary.tokens).to.have.property('size');
expect(filteredDictionary.tokens).to.not.have.property('color');
});
Expand All @@ -173,7 +176,7 @@ describe('filterTokens', () => {
filteredDictionary.allTokens.forEach((token) => {
expect(token).to.not.equal(not_kept);
});
expect(filteredDictionary.allTokens).to.eql([kept]);
expect(filteredDictionary.allTokens).to.eql([{ ...kept, key: '{kept}' }]);
expect(filteredDictionary.tokens).to.have.property('kept');
expect(filteredDictionary.tokens).to.not.have.property('not_kept');
});
Expand All @@ -184,7 +187,9 @@ describe('filterTokens', () => {
};

const filteredDictionary = await filterTokens(random_meta_dictionary, filter);
expect(filteredDictionary.allTokens).to.eql([random_meta_tokens.foo.bar]);
expect(filteredDictionary.allTokens).to.eql([
{ ...random_meta_tokens.foo.bar, key: '{foo.bar}' },
]);
expect(filteredDictionary.tokens).to.have.nested.property('foo.bar');
expect(filteredDictionary.tokens).to.not.have.property('qux');
});
Expand Down
32 changes: 32 additions & 0 deletions __tests__/utils/convertToTokenMap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import { expect } from 'chai';
import { convertToTokenMap } from '../../lib/utils/convertToTokenMap.js';

describe('utils', () => {
describe('convertToTokenMap', () => {
it('should return an empty array', () => {
const ret = convertToTokenMap([
{ key: 'colors.red.500', value: '#f00', foo: 'bar' },
{ key: 'colors.green.500', value: '#0f0', foo: 'baz' },
{ key: 'colors.blue.500', value: '#00f', foo: 'qux' },
]);
expect(ret.size).to.equal(3);
expect(Array.from(ret)).to.eql([
['colors.red.500', { value: '#f00', foo: 'bar' }],
['colors.green.500', { value: '#0f0', foo: 'baz' }],
['colors.blue.500', { value: '#00f', foo: 'qux' }],
]);
});
});
});
Loading

0 comments on commit d15081d

Please sign in to comment.