@koobiq/tokens-builder 3.0.0-rc.9 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,11 +1,43 @@
1
1
  # Tokens Builder
2
2
 
3
3
  ## Overview
4
+
4
5
  Tokens builder is a configuration script that utilizes Style Dictionary,
5
- a design-token platform for creating and maintaining consistent design tokens across various platforms.
6
+ a design-token platform for creating and maintaining consistent design tokens across various platforms.
6
7
 
7
8
  Overall, this script orchestrates the generation of design tokens by applying custom transformations,
8
9
  filters, and formats to the specified theme configuration for multiple platforms,
9
10
  facilitating a streamlined design token workflow compatible with
10
11
  various output formats like SCSS, CSS, and TypeScript.
11
12
 
13
+ ## Typography composite tokens
14
+
15
+ Tokens builder supports usage of [composite tokens](https://design-tokens.github.io/community-group/format/#composite-design-token) for CSS platform.
16
+ For now, typography token sets are supported.
17
+
18
+ ### Example of usage
19
+
20
+ ```json
21
+ {
22
+ "<component-name>": {
23
+ "font": {
24
+ "type": "font",
25
+ "<token-with-composite-token-reference>": "{typography.<typography-set>}"
26
+ }
27
+ }
28
+ }
29
+ ```
30
+
31
+ where `<typography-set>` is a set from `@koobiq/design-tokens/web/properties/typography.json5`
32
+
33
+ "type": "font" property is **required.**
34
+
35
+ output:
36
+
37
+ ```
38
+ --kbq-<component-name>-font-<token-with-composite-token-reference>-font-size: <value>;
39
+ --kbq-<component-name>-font-<token-with-composite-token-reference>-font-weight: <value>;
40
+ --kbq-<component-name>-font-<token-with-composite-token-reference>-line-height: <value>;
41
+ --kbq-<component-name>-font-<token-with-composite-token-reference>-font-family: <value>;
42
+ // ...etc
43
+ ```
package/build.js CHANGED
@@ -30,7 +30,6 @@ require('./formats/variables')(StyleDictionary);
30
30
  console.log('Build started...');
31
31
  console.log('==============================================');
32
32
 
33
-
34
33
  module.exports = (themeConfig) => {
35
34
  StyleDictionary.registerFileHeader({
36
35
  name: 'customHeader',
@@ -40,14 +39,14 @@ module.exports = (themeConfig) => {
40
39
 
41
40
  if (!themeConfig || themeConfig.length === 0) {
42
41
  console.error('Build Failed. Please set ThemeConfig, for example: ', {
43
- name: 'default-theme',
44
- buildPath: [
45
- `design-tokens/tokens/properties/**/*.json5`,
46
- `design-tokens/tokens/components/**/*.json5`
47
- ],
48
- outputPath: 'design-tokens/'
49
- }
50
- )
42
+ name: 'default-theme',
43
+ // prettier-ignore
44
+ buildPath: [
45
+ `design-tokens/tokens/properties/**/*.json5`,
46
+ `design-tokens/tokens/components/**/*.json5`
47
+ ],
48
+ outputPath: 'design-tokens/'
49
+ });
51
50
  process.exit(0);
52
51
  }
53
52
 
@@ -63,4 +62,4 @@ module.exports = (themeConfig) => {
63
62
 
64
63
  console.log('\n==============================================');
65
64
  console.log('\nBuild completed!');
66
- }
65
+ };
package/configs/index.js CHANGED
@@ -13,14 +13,14 @@ const filterObj = {
13
13
  function filterOptions(platforms) {
14
14
  const platformObj = {};
15
15
 
16
- platforms.map(p => Object.assign(platformObj, p));
16
+ platforms.map((p) => Object.assign(platformObj, p));
17
17
 
18
18
  Object.keys(platformObj).forEach((p) => {
19
- platformObj[p].files = platformObj[p].files.map(f => ({
19
+ platformObj[p].files = platformObj[p].files.map((f) => ({
20
20
  ...f,
21
21
  ...filterObj,
22
22
  options: {
23
- ...f.options ?? {},
23
+ ...(f.options ?? {}),
24
24
  ...filterObj.options
25
25
  }
26
26
  }));
@@ -30,7 +30,7 @@ function filterOptions(platforms) {
30
30
  }
31
31
 
32
32
  function getSources(theme) {
33
- return theme.buildPath.map(pathName => {
33
+ return theme.buildPath.map((pathName) => {
34
34
  if (pathName.startsWith('@')) {
35
35
  return join('node_modules', pathName);
36
36
  }
@@ -48,7 +48,6 @@ function getConfigs(theme) {
48
48
  }
49
49
 
50
50
  module.exports = (theme) => {
51
-
52
51
  return {
53
52
  source: [...getSources(theme)],
54
53
  platforms: getConfigs(theme)
package/configs/js.js CHANGED
@@ -13,4 +13,3 @@ module.exports = {
13
13
  ]
14
14
  }
15
15
  };
16
-
package/filters/color.js CHANGED
@@ -1,9 +1,6 @@
1
1
  module.exports = (StyleDictionary) => {
2
-
3
2
  StyleDictionary.registerFilter({
4
3
  name: 'color',
5
- matcher: (prop) => !prop.attributes.palette &&
6
- !prop.attributes.typography &&
7
- !prop.attributes['md-typography']
8
- })
9
- }
4
+ matcher: (prop) => !prop.attributes.palette && !prop.attributes.typography && !prop.attributes['md-typography']
5
+ });
6
+ };
@@ -2,23 +2,29 @@ module.exports = (StyleDictionary) => {
2
2
  StyleDictionary.registerFilter({
3
3
  name: 'css-variables',
4
4
  matcher: (prop) => {
5
- return !prop.attributes.font &&!prop.attributes.palette && !prop.attributes.light &&
6
- !prop.attributes.dark && !prop.attributes.font
5
+ return (
6
+ !prop.attributes.font &&
7
+ !prop.attributes.palette &&
8
+ !prop.attributes.light &&
9
+ !prop.attributes.dark &&
10
+ !prop.attributes.font &&
11
+ prop.type !== 'font'
12
+ );
7
13
  }
8
- })
14
+ });
9
15
 
10
16
  StyleDictionary.registerFilter({
11
17
  name: 'css-variables-light',
12
18
  matcher: (prop) => prop.attributes.light
13
- })
19
+ });
14
20
 
15
21
  StyleDictionary.registerFilter({
16
22
  name: 'css-variables-dark',
17
23
  matcher: (prop) => prop.attributes.dark
18
- })
24
+ });
19
25
 
20
26
  StyleDictionary.registerFilter({
21
27
  name: 'css-variables-font',
22
- matcher: (prop) => prop.attributes.font
23
- })
24
- }
28
+ matcher: (prop) => prop.attributes.font || prop.type === 'font'
29
+ });
30
+ };
@@ -1,7 +1,6 @@
1
1
  module.exports = (StyleDictionary) => {
2
-
3
2
  StyleDictionary.registerFilter({
4
3
  name: 'md-typography',
5
4
  matcher: (prop) => prop.attributes['md-typography']
6
- })
7
- }
5
+ });
6
+ };
@@ -1,7 +1,6 @@
1
1
  module.exports = (StyleDictionary) => {
2
-
3
2
  StyleDictionary.registerFilter({
4
3
  name: 'palette',
5
4
  matcher: (prop) => prop.attributes.palette
6
- })
7
- }
5
+ });
6
+ };
@@ -1,7 +1,6 @@
1
1
  module.exports = (StyleDictionary) => {
2
-
3
2
  StyleDictionary.registerFilter({
4
3
  name: 'typography',
5
4
  matcher: (prop) => prop.attributes.typography
6
- })
7
- }
5
+ });
6
+ };
@@ -1,25 +1,10 @@
1
+ const { getMapFromObj } = require('../formats/utils');
1
2
 
2
3
  module.exports = (StyleDictionary) => {
3
4
  StyleDictionary.registerFormat({
4
5
  name: 'kbq-scss/palette',
5
6
  formatter: function (dictionary) {
6
- return dictionary.allProperties
7
- .map((prop) => `\$${prop.name}: ${getMapFromObj(prop.value)};\n`)
8
- .join('\n');
7
+ return dictionary.allProperties.map((prop) => `\$${prop.name}: ${getMapFromObj(prop.value)};\n`).join('\n');
9
8
  }
10
- })
9
+ });
11
10
  };
12
-
13
- function getMapFromObj(object) {
14
- const result = Object.keys(object)
15
- .map((key) => {
16
- if (key === 'contrast') {
17
- return `${key}: ${getMapFromObj(object[key])}`
18
- }
19
-
20
- return `${key}: ${object[key].value},\n`
21
- })
22
- .join('');
23
-
24
- return `(\n${result}\n)`
25
- }
@@ -1,45 +1,45 @@
1
1
  module.exports = (StyleDictionary) => {
2
-
3
2
  StyleDictionary.registerFormat({
4
3
  name: 'kbq-scss/typography',
5
- formatter: function(dictionary) {
4
+ formatter: function (dictionary) {
6
5
  let string;
7
6
 
8
7
  string = dictionary.allProperties
9
8
  .map((prop) => {
10
9
  let output = '';
11
10
 
12
- const value = prop.attributes.category === 'asset' ? `"${ prop.value }"` : prop.value;
11
+ const value = prop.attributes.category === 'asset' ? `"${prop.value}"` : prop.value;
13
12
 
14
- output += `$${ prop.name }: ${ value };`
13
+ output += `$${prop.name}: ${value};`;
15
14
 
16
15
  if (prop.comment) {
17
- output += ` // ${ prop.comment }`;
16
+ output += ` // ${prop.comment}`;
18
17
  }
19
18
 
20
19
  return output + '\n';
21
20
  })
22
21
  .join('');
23
22
 
24
- return string + `\n$${this.mapName || 'tokens' }: ${processJsonNode(dictionary.properties, 0)};\n`;
23
+ return string + `\n$${this.mapName || 'tokens'}: ${processJsonNode(dictionary.properties, 0)};\n`;
25
24
  }
26
- })
25
+ });
27
26
  };
28
27
 
29
-
30
28
  function processJsonNode(obj, depth) {
31
29
  let output = '';
32
30
 
33
31
  if (obj.hasOwnProperty('value')) {
34
32
  output += `$${obj.name}`;
35
33
  } else {
36
- output += '(\n'
37
- output += Object.keys(obj).map((newKey) => {
38
- const newProp = obj[newKey];
39
- const indent = ' '.repeat(depth+1);
40
-
41
- return `${indent}'${newKey}': ${processJsonNode(newProp, depth + 1)}`;
42
- }).join(',\n');
34
+ output += '(\n';
35
+ output += Object.keys(obj)
36
+ .map((newKey) => {
37
+ const newProp = obj[newKey];
38
+ const indent = ' '.repeat(depth + 1);
39
+
40
+ return `${indent}'${newKey}': ${processJsonNode(newProp, depth + 1)}`;
41
+ })
42
+ .join(',\n');
43
43
 
44
44
  output += '\n' + ' '.repeat(depth) + ')';
45
45
  }
@@ -0,0 +1,35 @@
1
+ const transform = require('style-dictionary/lib/common/transforms');
2
+
3
+ const unwrapObjectTransformer = (token) => {
4
+ return Object.keys(token.value).map((key) => {
5
+ const subToken = token.value[key];
6
+ const res = {
7
+ ...subToken,
8
+ filePath: token.filePath,
9
+ path: [...token.path, key],
10
+ name: `kbq-${[...token.path, key].join('-')}`,
11
+ original: { value: subToken }
12
+ };
13
+ res.attributes = { ...transform['attribute/cti'].transformer(res), font: true };
14
+ return res;
15
+ }, {});
16
+ };
17
+
18
+ const getMapFromObj = (object) => {
19
+ const result = Object.keys(object)
20
+ .map((key) => {
21
+ if (key === 'contrast') {
22
+ return `${key}: ${getMapFromObj(object[key])}`;
23
+ }
24
+
25
+ return `${key}: ${object[key].value},\n`;
26
+ })
27
+ .join('');
28
+
29
+ return `(\n${result}\n)`;
30
+ };
31
+
32
+ module.exports = {
33
+ unwrapObjectTransformer,
34
+ getMapFromObj
35
+ };
@@ -1,19 +1,31 @@
1
1
  const { formatHelpers } = require('style-dictionary');
2
+ const { unwrapObjectTransformer } = require('../formats/utils');
2
3
 
3
4
  module.exports = (StyleDictionary) => {
4
5
  StyleDictionary.registerFormat({
5
6
  name: 'kbq-css/variables',
6
- formatter: function({dictionary, options={}, file}) {
7
+ formatter: function ({ dictionary, options = {}, file }) {
7
8
  const selector = options.selector ? options.selector : `:root`;
8
9
  const { outputReferences } = options;
9
10
 
11
+ // apply custom transformations for tokens
12
+ dictionary.allProperties = dictionary.allTokens = dictionary.allTokens.flatMap((token) => {
13
+ if (typeof token.value === 'object' && token.type === 'font') {
14
+ return unwrapObjectTransformer(token);
15
+ }
16
+ return token;
17
+ });
18
+
10
19
  dictionary.allTokens.forEach((token) => {
11
20
  token.name = token.name.replace(/(light|dark)-/, '');
12
21
  });
13
22
 
14
- return formatHelpers.fileHeader({file}) +
23
+ return (
24
+ formatHelpers.fileHeader({ file }) +
15
25
  `${selector} {\n` +
16
26
  formatHelpers.formattedVariables({ format: 'css', dictionary, outputReferences }) +
17
- `\n}\n`;
18
- } })
27
+ `\n}\n`
28
+ );
29
+ }
30
+ });
19
31
  };
package/package.json CHANGED
@@ -19,5 +19,5 @@
19
19
  "default": "./*"
20
20
  }
21
21
  },
22
- "version": "3.0.0-rc.9"
22
+ "version": "3.1.0"
23
23
  }
@@ -1,5 +1,4 @@
1
1
  module.exports = (StyleDictionary) => {
2
-
3
2
  StyleDictionary.registerTransformGroup({
4
3
  name: 'kbq/css',
5
4
  transforms: [
@@ -1,5 +1,4 @@
1
1
  module.exports = (StyleDictionary) => {
2
-
3
2
  StyleDictionary.registerTransformGroup({
4
3
  name: 'kbq/scss',
5
4
  transforms: [
@@ -10,4 +9,4 @@ module.exports = (StyleDictionary) => {
10
9
  'name/cti/kebab'
11
10
  ]
12
11
  });
13
- }
12
+ };
@@ -1,12 +1,6 @@
1
1
  module.exports = (StyleDictionary) => {
2
2
  StyleDictionary.registerTransformGroup({
3
3
  name: 'kbq/ts',
4
- transforms: [
5
- 'attribute/cti',
6
- 'name/cti/pascal',
7
- 'kbq-attribute/palette',
8
- 'size/px',
9
- 'color/hex'
10
- ]
4
+ transforms: ['attribute/cti', 'name/cti/pascal', 'kbq-attribute/palette', 'size/px', 'color/hex']
11
5
  });
12
- }
6
+ };
@@ -1,5 +1,4 @@
1
1
  module.exports = (StyleDictionary) => {
2
-
3
2
  StyleDictionary.registerTransform({
4
3
  name: 'kbq-attribute/md-typography',
5
4
  type: 'attribute',
@@ -1,5 +1,4 @@
1
1
  module.exports = (StyleDictionary) => {
2
-
3
2
  StyleDictionary.registerTransform({
4
3
  name: 'kbq-attribute/palette',
5
4
  type: 'attribute',
@@ -1,5 +1,4 @@
1
1
  module.exports = (StyleDictionary) => {
2
-
3
2
  StyleDictionary.registerTransform({
4
3
  name: 'kbq-attribute/typography',
5
4
  type: 'attribute',