style-dictionary 4.1.4 → 4.3.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 +8 -5
- package/bin/style-dictionary.js +4 -4
- package/examples/advanced/assets-base64-embed/package.json +1 -1
- package/examples/advanced/create-react-app/package.json +1 -1
- package/examples/advanced/create-react-native-app/package.json +1 -1
- package/examples/advanced/create-react-native-app/src/App.js +0 -1
- package/examples/advanced/custom-parser/package.json +1 -1
- package/examples/advanced/custom-parser/sd.config.js +3 -2
- package/examples/advanced/custom-transforms/README.md +1 -1
- package/examples/advanced/custom-transforms/build.js +11 -8
- package/examples/advanced/custom-transforms/package.json +1 -1
- package/examples/advanced/font-face-rules/package.json +1 -1
- package/examples/advanced/font-face-rules/sd.config.js +2 -1
- package/examples/advanced/format-helpers/package.json +1 -1
- package/examples/advanced/format-helpers/sd.config.js +3 -2
- package/examples/advanced/matching-build-files/config.js +15 -10
- package/examples/advanced/matching-build-files/package.json +1 -1
- package/examples/advanced/multi-brand-multi-platform/build.js +10 -7
- package/examples/advanced/multi-brand-multi-platform/package.json +1 -1
- package/examples/advanced/node-modules-as-config-and-properties/config.js +17 -12
- package/examples/advanced/node-modules-as-config-and-properties/package.json +1 -1
- package/examples/advanced/npm-module/package.json +1 -1
- package/examples/advanced/referencing_aliasing/package.json +1 -1
- package/examples/advanced/s3/package.json +1 -1
- package/examples/advanced/tailwind-preset/README.md +94 -0
- package/examples/advanced/tailwind-preset/config/filter.js +3 -0
- package/examples/advanced/tailwind-preset/config/filter.test.js +12 -0
- package/examples/advanced/tailwind-preset/config/format.js +60 -0
- package/examples/advanced/tailwind-preset/config/transform.js +21 -0
- package/examples/advanced/tailwind-preset/config/transform.test.js +37 -0
- package/examples/advanced/tailwind-preset/config.js +55 -0
- package/examples/advanced/tailwind-preset/demo/index.html +27 -0
- package/examples/advanced/tailwind-preset/demo/input.css +3 -0
- package/examples/advanced/tailwind-preset/package.json +18 -0
- package/examples/advanced/tailwind-preset/tailwind.config.js +11 -0
- package/examples/advanced/tailwind-preset/tokens/tokens.json +49 -0
- package/examples/advanced/tokens-deprecation/build.js +2 -1
- package/examples/advanced/tokens-deprecation/package.json +1 -1
- package/examples/advanced/transitive-transforms/package.json +1 -1
- package/examples/advanced/transitive-transforms/sd.config.js +12 -9
- package/examples/advanced/variables-in-outputs/package.json +1 -1
- package/examples/advanced/variables-in-outputs/sd.config.js +10 -5
- package/examples/advanced/yaml-tokens/package.json +1 -1
- package/examples/advanced/yaml-tokens/sd.config.js +3 -2
- package/lib/Register.d.ts +12 -12
- package/lib/Register.js +14 -11
- package/lib/StyleDictionary.d.ts +38 -36
- package/lib/StyleDictionary.js +103 -85
- package/lib/cleanActions.d.ts +9 -9
- package/lib/cleanActions.js +4 -4
- package/lib/cleanDir.d.ts +7 -7
- package/lib/cleanDir.js +5 -4
- package/lib/cleanDirs.d.ts +5 -5
- package/lib/cleanDirs.js +2 -2
- package/lib/cleanFile.d.ts +4 -9
- package/lib/cleanFile.js +8 -5
- package/lib/cleanFiles.d.ts +5 -5
- package/lib/cleanFiles.js +2 -2
- package/lib/common/actions.d.ts +23 -4
- package/lib/common/actions.js +11 -7
- package/lib/common/filters.d.ts +8 -2
- package/lib/common/filters.js +1 -1
- package/lib/common/formatHelpers/createPropertyFormatter.d.ts +11 -9
- package/lib/common/formatHelpers/createPropertyFormatter.js +31 -24
- package/lib/common/formatHelpers/fileHeader.d.ts +8 -8
- package/lib/common/formatHelpers/fileHeader.js +8 -6
- package/lib/common/formatHelpers/formattedVariables.d.ts +10 -8
- package/lib/common/formatHelpers/formattedVariables.js +8 -6
- package/lib/common/formatHelpers/getTypeScriptType.d.ts +6 -6
- package/lib/common/formatHelpers/getTypeScriptType.js +2 -2
- package/lib/common/formatHelpers/iconsWithPrefix.d.ts +6 -6
- package/lib/common/formatHelpers/iconsWithPrefix.js +3 -3
- package/lib/common/formatHelpers/minifyDictionary.d.ts +3 -3
- package/lib/common/formatHelpers/minifyDictionary.js +1 -1
- package/lib/common/formatHelpers/setComposeObjectProperties.d.ts +3 -3
- package/lib/common/formatHelpers/setComposeObjectProperties.js +1 -1
- package/lib/common/formatHelpers/setSwiftFileProperties.d.ts +3 -3
- package/lib/common/formatHelpers/setSwiftFileProperties.js +1 -1
- package/lib/common/formatHelpers/sortByReference.d.ts +3 -3
- package/lib/common/formatHelpers/sortByReference.js +2 -2
- package/lib/common/formats.d.ts +9 -6
- package/lib/common/formats.js +345 -178
- package/lib/common/templates/android/colors.template.d.ts +3 -3
- package/lib/common/templates/android/colors.template.js +3 -3
- package/lib/common/templates/android/dimens.template.d.ts +3 -3
- package/lib/common/templates/android/dimens.template.js +3 -3
- package/lib/common/templates/android/fontDimens.template.d.ts +3 -3
- package/lib/common/templates/android/fontDimens.template.js +3 -3
- package/lib/common/templates/android/integers.template.d.ts +3 -3
- package/lib/common/templates/android/integers.template.js +3 -3
- package/lib/common/templates/android/resources.template.d.ts +6 -6
- package/lib/common/templates/android/resources.template.js +6 -6
- package/lib/common/templates/android/strings.template.d.ts +3 -3
- package/lib/common/templates/android/strings.template.js +3 -3
- package/lib/common/templates/compose/object.kt.template.d.ts +3 -3
- package/lib/common/templates/compose/object.kt.template.js +11 -7
- package/lib/common/templates/css/fonts.css.template.d.ts +1 -1
- package/lib/common/templates/css/fonts.css.template.js +3 -3
- package/lib/common/templates/flutter/class.dart.template.d.ts +4 -4
- package/lib/common/templates/flutter/class.dart.template.js +4 -4
- package/lib/common/templates/ios/colors.h.template.d.ts +4 -4
- package/lib/common/templates/ios/colors.h.template.js +4 -4
- package/lib/common/templates/ios/colors.m.template.d.ts +4 -4
- package/lib/common/templates/ios/colors.m.template.js +4 -4
- package/lib/common/templates/ios/macros.template.d.ts +4 -4
- package/lib/common/templates/ios/macros.template.js +4 -4
- package/lib/common/templates/ios/plist.template.d.ts +4 -4
- package/lib/common/templates/ios/plist.template.js +4 -4
- package/lib/common/templates/ios/singleton.h.template.d.ts +3 -3
- package/lib/common/templates/ios/singleton.h.template.js +3 -3
- package/lib/common/templates/ios/singleton.m.template.d.ts +6 -6
- package/lib/common/templates/ios/singleton.m.template.js +6 -6
- package/lib/common/templates/ios/static.h.template.d.ts +4 -4
- package/lib/common/templates/ios/static.h.template.js +4 -4
- package/lib/common/templates/ios/static.m.template.d.ts +4 -4
- package/lib/common/templates/ios/static.m.template.js +4 -4
- package/lib/common/templates/ios/strings.h.template.d.ts +4 -4
- package/lib/common/templates/ios/strings.h.template.js +4 -4
- package/lib/common/templates/ios/strings.m.template.d.ts +5 -5
- package/lib/common/templates/ios/strings.m.template.js +5 -5
- package/lib/common/templates/ios-swift/any.swift.template.d.ts +4 -4
- package/lib/common/templates/ios-swift/any.swift.template.js +4 -4
- package/lib/common/templates/scss/map-deep.template.d.ts +4 -4
- package/lib/common/templates/scss/map-deep.template.js +4 -4
- package/lib/common/templates/scss/map-flat.template.d.ts +3 -3
- package/lib/common/templates/scss/map-flat.template.js +7 -6
- package/lib/common/transformGroups.d.ts +209 -1
- package/lib/common/transformGroups.js +139 -93
- package/lib/common/transforms.d.ts +965 -5
- package/lib/common/transforms.js +116 -114
- package/lib/enums/actions.d.ts +4 -0
- package/lib/enums/actions.js +4 -0
- package/lib/enums/commentPositions.d.ts +4 -0
- package/lib/enums/commentPositions.js +4 -0
- package/lib/enums/commentStyles.d.ts +5 -0
- package/lib/enums/commentStyles.js +5 -0
- package/lib/enums/fileHeaderCommentStyles.d.ts +5 -0
- package/lib/enums/fileHeaderCommentStyles.js +5 -0
- package/lib/enums/formats.d.ts +46 -0
- package/lib/enums/formats.js +50 -0
- package/lib/enums/index.d.ts +12 -0
- package/lib/enums/index.js +12 -0
- package/lib/enums/logBrokenReferenceLevels.d.ts +5 -0
- package/lib/enums/logBrokenReferenceLevels.js +4 -0
- package/lib/enums/logVerbosityLevels.d.ts +6 -0
- package/lib/enums/logVerbosityLevels.js +5 -0
- package/lib/enums/logWarningLevels.d.ts +5 -0
- package/lib/enums/logWarningLevels.js +5 -0
- package/lib/enums/propertyFormatNames.d.ts +6 -0
- package/lib/enums/propertyFormatNames.js +6 -0
- package/lib/enums/transformGroups.d.ts +17 -0
- package/lib/enums/transformGroups.js +17 -0
- package/lib/enums/transformTypes.d.ts +5 -0
- package/lib/enums/transformTypes.js +5 -0
- package/lib/enums/transforms.d.ts +57 -0
- package/lib/enums/transforms.js +57 -0
- package/lib/filterTokens.d.ts +6 -6
- package/lib/filterTokens.js +10 -5
- package/lib/fs-node.d.ts +2 -2
- package/lib/fs-node.js +1 -1
- package/lib/fs.d.ts +3 -3
- package/lib/fs.js +2 -2
- package/lib/performActions.d.ts +9 -9
- package/lib/performActions.js +4 -4
- package/lib/resolve.d.ts +1 -1
- package/lib/resolve.js +4 -3
- package/lib/transform/config.d.ts +4 -4
- package/lib/transform/config.js +7 -6
- package/lib/transform/object.d.ts +15 -15
- package/lib/transform/object.js +7 -7
- package/lib/transform/token.d.ts +7 -15
- package/lib/transform/token.js +16 -9
- package/lib/transform/tokenSetup.d.ts +4 -4
- package/lib/transform/tokenSetup.js +2 -2
- package/lib/utils/cleanActions.d.ts +23 -0
- package/lib/utils/cleanActions.js +44 -0
- package/lib/utils/combineJSON.d.ts +5 -5
- package/lib/utils/combineJSON.js +16 -36
- package/lib/utils/convertToBase64.d.ts +3 -3
- package/lib/utils/convertToBase64.js +1 -1
- package/lib/utils/convertToDTCG.d.ts +5 -5
- package/lib/utils/convertToDTCG.js +2 -2
- package/lib/utils/convertTokenData.d.ts +30 -0
- package/lib/utils/convertTokenData.js +123 -0
- package/lib/utils/createFormatArgs.d.ts +8 -8
- package/lib/utils/createFormatArgs.js +4 -4
- package/lib/utils/deepExtend.d.ts +5 -5
- package/lib/utils/deepExtend.js +2 -2
- package/lib/utils/deepmerge.d.ts +1 -1
- package/lib/utils/expandObjectTokens.d.ts +9 -9
- package/lib/utils/expandObjectTokens.js +8 -8
- package/lib/utils/flattenTokens.d.ts +9 -5
- package/lib/utils/flattenTokens.js +28 -16
- package/lib/utils/index.d.ts +3 -1
- package/lib/utils/index.js +5 -0
- package/lib/utils/loadFile.d.ts +13 -0
- package/lib/utils/loadFile.js +89 -0
- package/lib/utils/preprocess.d.ts +9 -9
- package/lib/utils/preprocess.js +4 -4
- package/lib/utils/references/createReferenceRegex.d.ts +2 -2
- package/lib/utils/references/createReferenceRegex.js +1 -1
- package/lib/utils/references/defaults.d.ts +5 -1
- package/lib/utils/references/defaults.js +1 -1
- package/lib/utils/references/getName.d.ts +3 -3
- package/lib/utils/references/getName.js +1 -1
- package/lib/utils/references/getReferences.d.ts +7 -7
- package/lib/utils/references/getReferences.js +3 -3
- package/lib/utils/references/getValueByPath.d.ts +4 -4
- package/lib/utils/references/getValueByPath.js +2 -2
- package/lib/utils/references/outputReferencesFilter.d.ts +4 -4
- package/lib/utils/references/outputReferencesFilter.js +2 -2
- package/lib/utils/references/outputReferencesTransformed.d.ts +4 -4
- package/lib/utils/references/outputReferencesTransformed.js +3 -3
- package/lib/utils/references/resolveReferences.d.ts +10 -10
- package/lib/utils/references/resolveReferences.js +4 -4
- package/lib/utils/references/usesReferences.d.ts +3 -3
- package/lib/utils/references/usesReferences.js +1 -1
- package/lib/utils/resolveObject.d.ts +4 -4
- package/lib/utils/resolveObject.js +3 -3
- package/lib/utils/stripMeta.d.ts +20 -0
- package/lib/utils/stripMeta.js +51 -0
- package/lib/utils/typeDtcgDelegate.d.ts +6 -6
- package/lib/utils/typeDtcgDelegate.js +3 -3
- package/package.json +26 -13
- package/types/Action.d.ts +3 -3
- package/types/Config.d.ts +17 -11
- package/types/DesignToken.d.ts +6 -0
- package/types/File.d.ts +10 -6
- package/types/Filter.d.ts +2 -2
- package/types/Format.d.ts +7 -4
- package/types/Parser.d.ts +1 -1
- package/types/Preprocessor.d.ts +2 -2
- package/types/Transform.d.ts +8 -7
- package/types/index.d.ts +10 -10
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Tailwind preset
|
|
2
|
+
|
|
3
|
+
Builds [Tailwind preset](https://tailwindcss.com/docs/presets#creating-a-preset) from tokens.
|
|
4
|
+
|
|
5
|
+
## Building the preset
|
|
6
|
+
|
|
7
|
+
Run `npm run build-tokens` to generate these files in `build/tailwind`:
|
|
8
|
+
|
|
9
|
+
### cssVarPlugin.js
|
|
10
|
+
|
|
11
|
+
A [Tailwind plugin](https://tailwindcss.com/docs/plugins) for registering new [base styles](https://tailwindcss.com/docs/plugins#adding-base-styles).
|
|
12
|
+
|
|
13
|
+
The [rgbChannels](./config/transform.js) transform removes the color space function for compatability with [Tailwind's opacity modifier syntax](https://tailwindcss.com/docs/text-color#changing-the-opacity).
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import plugin from 'tailwindcss/plugin.js';
|
|
17
|
+
|
|
18
|
+
export default plugin(function ({ addBase }) {
|
|
19
|
+
addBase({
|
|
20
|
+
':root': {
|
|
21
|
+
'--sd-text-small': '0.75',
|
|
22
|
+
'--sd-text-base': '46 46 70',
|
|
23
|
+
'--sd-text-secondary': '100 100 115',
|
|
24
|
+
'--sd-text-tertiary': '129 129 142',
|
|
25
|
+
'--sd-text-neutral': '0 0 0 / 0.55',
|
|
26
|
+
'--sd-theme': '31 197 191',
|
|
27
|
+
'--sd-theme-light': '153 235 226',
|
|
28
|
+
'--sd-theme-dark': '0 179 172',
|
|
29
|
+
'--sd-theme-secondary': '106 80 150',
|
|
30
|
+
'--sd-theme-secondary-dark': '63 28 119',
|
|
31
|
+
'--sd-theme-secondary-light': '196 178 225',
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### themeColors.js
|
|
38
|
+
|
|
39
|
+
Tailwind theme color values that reference the plugin [css vars](https://tailwindcss.com/docs/customizing-colors#using-css-variables).
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
export default {
|
|
43
|
+
'sd-text-base': 'rgb(var(--sd-text-base))',
|
|
44
|
+
'sd-text-secondary': 'rgb(var(--sd-text-secondary))',
|
|
45
|
+
'sd-text-tertiary': 'rgb(var(--sd-text-tertiary))',
|
|
46
|
+
'sd-text-neutral': 'rgb(var(--sd-text-neutral))',
|
|
47
|
+
'sd-theme': 'rgb(var(--sd-theme))',
|
|
48
|
+
'sd-theme-light': 'rgb(var(--sd-theme-light))',
|
|
49
|
+
'sd-theme-dark': 'rgb(var(--sd-theme-dark))',
|
|
50
|
+
'sd-theme-secondary': 'rgb(var(--sd-theme-secondary))',
|
|
51
|
+
'sd-theme-secondary-dark': 'rgb(var(--sd-theme-secondary-dark))',
|
|
52
|
+
'sd-theme-secondary-light': 'rgb(var(--sd-theme-secondary-light))',
|
|
53
|
+
};
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### preset.js
|
|
57
|
+
|
|
58
|
+
[Tailwind preset](https://tailwindcss.com/docs/presets) file that imports the colors and plugin.
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import themeColors from './themeColors.js';
|
|
62
|
+
import cssVarsPlugin from './cssVarsPlugin.js';
|
|
63
|
+
|
|
64
|
+
export default {
|
|
65
|
+
theme: {
|
|
66
|
+
extend: {
|
|
67
|
+
colors: {
|
|
68
|
+
...themeColors, // <-- theme colors defined here
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
plugins: [cssVarsPlugin], // <-- plugin imported here
|
|
73
|
+
};
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Building the CSS
|
|
77
|
+
|
|
78
|
+
The [Tailwind preset](https://tailwindcss.com/docs/presets#creating-a-preset) is imported from the build directory in `tailwind.config.js`.
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
import tailwindPreset from './build/tailwind/preset.js';
|
|
82
|
+
|
|
83
|
+
/** @type {import('tailwindcss').Config} */
|
|
84
|
+
export default {
|
|
85
|
+
theme: {
|
|
86
|
+
extend: {},
|
|
87
|
+
},
|
|
88
|
+
presets: [tailwindPreset],
|
|
89
|
+
content: ['./demo/**/*.{html,js}'],
|
|
90
|
+
plugins: [],
|
|
91
|
+
};
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Run `npm run build-css` to watch the `demo/index.html` file for changes -- any Tailwind classes used will be compiled into `demo/output.css`.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { describe, it } from 'mocha';
|
|
3
|
+
import { isColor } from './filter.js';
|
|
4
|
+
|
|
5
|
+
describe('isColor', () => {
|
|
6
|
+
it('should handle legacy and dtcg formats', () => {
|
|
7
|
+
expect(isColor({ type: 'color' })).to.equal(true);
|
|
8
|
+
expect(isColor({ $type: 'color' })).to.equal(true);
|
|
9
|
+
expect(isColor({ type: 'fontSize' })).to.equal(false);
|
|
10
|
+
expect(isColor({ $type: 'fontSize' })).to.equal(false);
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { isColor } from './filter.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Exports tailwind plugin for declaring root CSS vars
|
|
5
|
+
* @see https://tailwindcss.com/docs/plugins#overview
|
|
6
|
+
*/
|
|
7
|
+
export function cssVarsPlugin({ dictionary }) {
|
|
8
|
+
const vars = dictionary.allTokens
|
|
9
|
+
.map((token) => {
|
|
10
|
+
const value = token?.$value || token?.value;
|
|
11
|
+
return `'--${token.name}': '${value}'`;
|
|
12
|
+
})
|
|
13
|
+
.join(',\n\t\t\t');
|
|
14
|
+
|
|
15
|
+
return `import plugin from 'tailwindcss/plugin.js';
|
|
16
|
+
|
|
17
|
+
export default plugin(function ({ addBase }) {
|
|
18
|
+
\taddBase({
|
|
19
|
+
\t\t':root': {
|
|
20
|
+
\t\t\t${vars},
|
|
21
|
+
\t\t},
|
|
22
|
+
\t});
|
|
23
|
+
});\n`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Exports theme color definitions
|
|
28
|
+
* @see https://tailwindcss.com/docs/customizing-colors#using-css-variables
|
|
29
|
+
*/
|
|
30
|
+
export function themeColors({ dictionary, options }) {
|
|
31
|
+
const tokens = dictionary.allTokens.filter((token) => isColor(token, options));
|
|
32
|
+
|
|
33
|
+
const theme = tokens
|
|
34
|
+
.map((token) => {
|
|
35
|
+
return `\t'${token.name}': 'rgb(var(--${token.name}))'`;
|
|
36
|
+
})
|
|
37
|
+
.join(',\n');
|
|
38
|
+
|
|
39
|
+
return `export default {\n${theme},\n};\n`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Exports tailwind preset
|
|
44
|
+
* @see https://tailwindcss.com/docs/presets
|
|
45
|
+
*/
|
|
46
|
+
export function preset() {
|
|
47
|
+
return `import themeColors from './themeColors.js';
|
|
48
|
+
import cssVarsPlugin from './cssVarsPlugin.js';
|
|
49
|
+
|
|
50
|
+
export default {
|
|
51
|
+
\ttheme: {
|
|
52
|
+
\t\textend: {
|
|
53
|
+
\t\t\tcolors: {
|
|
54
|
+
\t\t\t\t...themeColors, // <-- theme colors defined here
|
|
55
|
+
\t\t\t},
|
|
56
|
+
\t\t},
|
|
57
|
+
\t},
|
|
58
|
+
\tplugins: [cssVarsPlugin], // <-- plugin imported here
|
|
59
|
+
};\n`;
|
|
60
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function rgbChannels(token) {
|
|
2
|
+
const value = token?.$value || token?.value;
|
|
3
|
+
const { r, g, b, a } = parseRGBA(value);
|
|
4
|
+
const hasAlpha = a !== undefined;
|
|
5
|
+
return `${r} ${g} ${b}${hasAlpha ? ' / ' + a : ''}`;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function parseRGBA(value) {
|
|
9
|
+
const regex = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)(?:\s*,\s*([\d.]+%?))?\s*\)/;
|
|
10
|
+
const matches = value.match(regex);
|
|
11
|
+
if (!matches) {
|
|
12
|
+
throw new Error(`Value '${value}' is not a valid rgb or rgba format.`);
|
|
13
|
+
}
|
|
14
|
+
const [, r, g, b, a] = matches;
|
|
15
|
+
return {
|
|
16
|
+
r,
|
|
17
|
+
g,
|
|
18
|
+
b,
|
|
19
|
+
a: a !== undefined ? (a.endsWith('%') ? parseFloat(a) / 100 : parseFloat(a)) : undefined,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { describe, it } from 'mocha';
|
|
3
|
+
import { rgbChannels } from './transform.js';
|
|
4
|
+
|
|
5
|
+
describe('rgbChannels', () => {
|
|
6
|
+
it('should extract RGB channels from valid RGB string', () => {
|
|
7
|
+
const tokenValue = 'rgb(255, 255, 255)';
|
|
8
|
+
expect(rgbChannels({ value: tokenValue })).to.equal('255 255 255');
|
|
9
|
+
expect(rgbChannels({ $value: tokenValue })).to.equal('255 255 255');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should extract RGB and alpha channels from valid RGBA string', () => {
|
|
13
|
+
const tokenValue = 'rgba(255, 255, 255, 0.5)';
|
|
14
|
+
expect(rgbChannels({ value: tokenValue })).to.equal('255 255 255 / 0.5');
|
|
15
|
+
expect(rgbChannels({ $value: tokenValue })).to.equal('255 255 255 / 0.5');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should handle different whitespace variations', () => {
|
|
19
|
+
let tokenValue = 'rgb( 123 , 45,67 )';
|
|
20
|
+
expect(rgbChannels({ value: tokenValue })).to.equal('123 45 67');
|
|
21
|
+
tokenValue = 'rgba( 12, 34 , 56 , 0.75 )';
|
|
22
|
+
expect(rgbChannels({ value: tokenValue })).to.equal('12 34 56 / 0.75');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should handle different alpha formats', () => {
|
|
26
|
+
const tokenValues = ['rgb(1, 2, 3, 50%)', 'rgb(1, 2, 3, .5)', 'rgb(1, 2, 3, .50)'];
|
|
27
|
+
for (const tokenValue of tokenValues) {
|
|
28
|
+
expect(rgbChannels({ value: tokenValue })).to.equal('1 2 3 / 0.5');
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should throw error for invalid RGB string', () => {
|
|
33
|
+
const expectedErr = "Value 'mock' is not a valid rgb or rgba format.";
|
|
34
|
+
expect(() => rgbChannels({ value: 'mock' })).to.throw(expectedErr);
|
|
35
|
+
expect(() => rgbChannels({ $value: 'mock' })).to.throw(expectedErr);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import StyleDictionary from 'style-dictionary';
|
|
2
|
+
import { isColor } from './config/filter.js';
|
|
3
|
+
import { cssVarsPlugin, preset, themeColors } from './config/format.js';
|
|
4
|
+
import { rgbChannels } from './config/transform.js';
|
|
5
|
+
|
|
6
|
+
StyleDictionary.registerTransform({
|
|
7
|
+
name: 'color/rgb-channels',
|
|
8
|
+
type: 'value',
|
|
9
|
+
filter: isColor,
|
|
10
|
+
transform: rgbChannels,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
StyleDictionary.registerTransformGroup({
|
|
14
|
+
name: 'tailwind',
|
|
15
|
+
transforms: ['name/kebab', 'color/rgb', 'color/rgb-channels'],
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
StyleDictionary.registerFormat({
|
|
19
|
+
name: 'tailwind/css-vars-plugin',
|
|
20
|
+
format: cssVarsPlugin,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
StyleDictionary.registerFormat({
|
|
24
|
+
name: 'tailwind/theme-colors',
|
|
25
|
+
format: themeColors,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
StyleDictionary.registerFormat({
|
|
29
|
+
name: 'tailwind/preset',
|
|
30
|
+
format: preset,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export default {
|
|
34
|
+
source: ['./tokens/**/*.json'],
|
|
35
|
+
platforms: {
|
|
36
|
+
tailwindPreset: {
|
|
37
|
+
buildPath: 'build/tailwind/',
|
|
38
|
+
transformGroup: 'tailwind',
|
|
39
|
+
files: [
|
|
40
|
+
{
|
|
41
|
+
destination: 'cssVarsPlugin.js',
|
|
42
|
+
format: 'tailwind/css-vars-plugin',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
destination: 'themeColors.js',
|
|
46
|
+
format: 'tailwind/theme-colors',
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
destination: 'preset.js',
|
|
50
|
+
format: 'tailwind/preset',
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<!--
|
|
7
|
+
Run `npm run build-tokens` to generate build/preset.js.
|
|
8
|
+
Then run `npm run build-css` to generate `output.css`.
|
|
9
|
+
-->
|
|
10
|
+
<link href="./output.css" rel="stylesheet" />
|
|
11
|
+
</head>
|
|
12
|
+
<body>
|
|
13
|
+
<div class="h-screen w-full bg-sd-theme-secondary/10">
|
|
14
|
+
<div class="p-4 grid grid-cols-1 gap-4 text-5xl">
|
|
15
|
+
<span class="text-sd-theme-dark">Hello tokens</span>
|
|
16
|
+
<span class="text-sd-theme-dark/50">Hello tokens</span>
|
|
17
|
+
<span class="text-sd-theme-dark/30">Hello tokens</span>
|
|
18
|
+
<span class="text-sd-theme-dark/20">Hello tokens</span>
|
|
19
|
+
<span class="text-sd-theme-secondary-dark">Hello tokens</span>
|
|
20
|
+
<span class="text-sd-theme-secondary-dark/50">Hello tokens</span>
|
|
21
|
+
<span class="text-sd-theme-secondary-dark/30">Hello tokens</span>
|
|
22
|
+
<span class="text-sd-theme-secondary-dark/20">Hello tokens</span>
|
|
23
|
+
<span class="text-sd-text-neutral">Hello tokens</span>
|
|
24
|
+
</div>
|
|
25
|
+
</div>
|
|
26
|
+
</body>
|
|
27
|
+
</html>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tailwind-preset",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Builds tailwind preset from tokens",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build-tokens": "style-dictionary build --config ./config.js",
|
|
8
|
+
"build-css": "npx tailwindcss -i ./demo/input.css -o ./demo/output.css --watch",
|
|
9
|
+
"test": "mocha 'config/**/*test.js'"
|
|
10
|
+
},
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"style-dictionary": "^4.3.0",
|
|
14
|
+
"tailwindcss": "^3.4.15",
|
|
15
|
+
"mocha": "^10.2.0",
|
|
16
|
+
"chai": "^5.1.1"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"sd": {
|
|
3
|
+
"text": {
|
|
4
|
+
"small": {
|
|
5
|
+
"$value": "0.75",
|
|
6
|
+
"$type": "fontSize"
|
|
7
|
+
},
|
|
8
|
+
"base": {
|
|
9
|
+
"$type": "color",
|
|
10
|
+
"$value": "#2E2E46"
|
|
11
|
+
},
|
|
12
|
+
"secondary": {
|
|
13
|
+
"$type": "color",
|
|
14
|
+
"$value": "#646473"
|
|
15
|
+
},
|
|
16
|
+
"tertiary": {
|
|
17
|
+
"$type": "color",
|
|
18
|
+
"$value": "#81818E"
|
|
19
|
+
},
|
|
20
|
+
"neutral": {
|
|
21
|
+
"$type": "color",
|
|
22
|
+
"$value": "#0000008C"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"theme": {
|
|
26
|
+
"$type": "color",
|
|
27
|
+
"_": {
|
|
28
|
+
"$value": "#1FC5BF"
|
|
29
|
+
},
|
|
30
|
+
"light": {
|
|
31
|
+
"$value": "#99EBE2"
|
|
32
|
+
},
|
|
33
|
+
"dark": {
|
|
34
|
+
"$value": "#00B3AC"
|
|
35
|
+
},
|
|
36
|
+
"secondary": {
|
|
37
|
+
"_": {
|
|
38
|
+
"$value": "#6A5096"
|
|
39
|
+
},
|
|
40
|
+
"dark": {
|
|
41
|
+
"$value": "#3F1C77"
|
|
42
|
+
},
|
|
43
|
+
"light": {
|
|
44
|
+
"$value": "#C4B2E1"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import StyleDictionary from 'style-dictionary';
|
|
2
2
|
import { fileHeader } from 'style-dictionary/utils';
|
|
3
|
+
import { commentStyles } from 'style-dictionary/enums';
|
|
3
4
|
import { dirname } from 'path';
|
|
4
5
|
import { fileURLToPath } from 'url';
|
|
5
6
|
import webScssTemplate from './templates/web-scss.template.js';
|
|
@@ -15,7 +16,7 @@ sd.registerFormat({
|
|
|
15
16
|
name: 'custom/format/scss',
|
|
16
17
|
format: async ({ dictionary, file, options }) => {
|
|
17
18
|
const { allTokens } = dictionary;
|
|
18
|
-
const header = await fileHeader({ file, commentStyle:
|
|
19
|
+
const header = await fileHeader({ file, commentStyle: commentStyles.long });
|
|
19
20
|
return webScssTemplate({ allTokens, file, options, header });
|
|
20
21
|
},
|
|
21
22
|
});
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import StyleDictionary from 'style-dictionary';
|
|
2
2
|
import { usesReferences } from 'style-dictionary/utils';
|
|
3
|
+
import { formats, transforms } from 'style-dictionary/enums';
|
|
3
4
|
import Color from 'colorjs.io';
|
|
4
5
|
|
|
6
|
+
const { attributeCti, colorCss, nameKebab } = transforms;
|
|
7
|
+
|
|
5
8
|
const colorTransform = (token) => {
|
|
6
9
|
const { value, modify = [] } = token;
|
|
7
10
|
|
|
8
11
|
// This assumes "hex" format, if you want to support { h, s, l } format you have to do
|
|
9
|
-
//
|
|
12
|
+
// 'new Color('hsl', [value.h, value.s, value.l]);'
|
|
10
13
|
const color = new Color(value);
|
|
11
14
|
|
|
12
15
|
// defer until reference is resolved
|
|
@@ -42,14 +45,14 @@ const colorTransform = (token) => {
|
|
|
42
45
|
export default {
|
|
43
46
|
// This will match any files ending in json or json5
|
|
44
47
|
// I am using json5 here so I can add comments in the token files for reference
|
|
45
|
-
source: [
|
|
48
|
+
source: ['tokens/**/*.@(json|json5)'],
|
|
46
49
|
|
|
47
50
|
// I am directly defining transforms here
|
|
48
51
|
// This would work if you were to call StyleDictionary.registerTransform() as well
|
|
49
52
|
hooks: {
|
|
50
53
|
transforms: {
|
|
51
54
|
colorTransform: {
|
|
52
|
-
type:
|
|
55
|
+
type: 'value',
|
|
53
56
|
// only transforms that have transitive: true will be applied to tokens
|
|
54
57
|
// that alias/reference other tokens
|
|
55
58
|
transitive: true,
|
|
@@ -59,8 +62,8 @@ export default {
|
|
|
59
62
|
|
|
60
63
|
// For backwards compatibility, all built-in transforms are not transitive
|
|
61
64
|
// by default. This will make the 'color/css' transform transitive
|
|
62
|
-
|
|
63
|
-
...StyleDictionary.hooks.transforms[
|
|
65
|
+
[colorCss]: {
|
|
66
|
+
...StyleDictionary.hooks.transforms[colorCss],
|
|
64
67
|
transitive: true,
|
|
65
68
|
},
|
|
66
69
|
},
|
|
@@ -68,12 +71,12 @@ export default {
|
|
|
68
71
|
|
|
69
72
|
platforms: {
|
|
70
73
|
css: {
|
|
71
|
-
transforms: [
|
|
72
|
-
buildPath:
|
|
74
|
+
transforms: [attributeCti, nameKebab, 'colorTransform', colorCss],
|
|
75
|
+
buildPath: 'build/',
|
|
73
76
|
files: [
|
|
74
77
|
{
|
|
75
|
-
destination:
|
|
76
|
-
format:
|
|
78
|
+
destination: 'variables.css',
|
|
79
|
+
format: formats.cssVariables,
|
|
77
80
|
},
|
|
78
81
|
],
|
|
79
82
|
},
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { formats, transformGroups } from 'style-dictionary/enums';
|
|
2
|
+
|
|
3
|
+
const { cssVariables, jsonNested, scssVariables } = formats;
|
|
4
|
+
const { css, js } = transformGroups;
|
|
5
|
+
|
|
1
6
|
export default {
|
|
2
7
|
hooks: {
|
|
3
8
|
formats: {
|
|
@@ -37,13 +42,13 @@ export default {
|
|
|
37
42
|
files: [
|
|
38
43
|
{
|
|
39
44
|
destination: 'tokens.json',
|
|
40
|
-
format:
|
|
45
|
+
format: jsonNested,
|
|
41
46
|
},
|
|
42
47
|
],
|
|
43
48
|
},
|
|
44
49
|
js: {
|
|
45
50
|
buildPath: 'build/',
|
|
46
|
-
transformGroup:
|
|
51
|
+
transformGroup: js,
|
|
47
52
|
files: [
|
|
48
53
|
{
|
|
49
54
|
destination: 'tokens.js',
|
|
@@ -55,19 +60,19 @@ export default {
|
|
|
55
60
|
],
|
|
56
61
|
},
|
|
57
62
|
css: {
|
|
58
|
-
transformGroup:
|
|
63
|
+
transformGroup: css,
|
|
59
64
|
buildPath: 'build/',
|
|
60
65
|
files: [
|
|
61
66
|
{
|
|
62
67
|
destination: 'tokens.css',
|
|
63
|
-
format:
|
|
68
|
+
format: cssVariables,
|
|
64
69
|
options: {
|
|
65
70
|
outputReferences: true, // new setting, if true will use variable references
|
|
66
71
|
},
|
|
67
72
|
},
|
|
68
73
|
{
|
|
69
74
|
destination: 'tokens.scss',
|
|
70
|
-
format:
|
|
75
|
+
format: scssVariables,
|
|
71
76
|
options: {
|
|
72
77
|
outputReferences: true, // new setting, if true will use variable references
|
|
73
78
|
},
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import yaml from 'yaml';
|
|
2
|
+
import { formats, transformGroups } from 'style-dictionary/enums';
|
|
2
3
|
|
|
3
4
|
export default {
|
|
4
5
|
hooks: {
|
|
@@ -20,12 +21,12 @@ export default {
|
|
|
20
21
|
source: [`tokens/**/*.yaml`],
|
|
21
22
|
platforms: {
|
|
22
23
|
css: {
|
|
23
|
-
transformGroup:
|
|
24
|
+
transformGroup: transformGroups.css,
|
|
24
25
|
buildPath: 'build/',
|
|
25
26
|
files: [
|
|
26
27
|
{
|
|
27
28
|
destination: 'variables.css',
|
|
28
|
-
format:
|
|
29
|
+
format: formats.cssVariables,
|
|
29
30
|
},
|
|
30
31
|
],
|
|
31
32
|
},
|
package/lib/Register.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export class Register {
|
|
|
7
7
|
*
|
|
8
8
|
* Therefore, we have to make use of static props vs instance props and use getters and setters to merge these together.
|
|
9
9
|
*/
|
|
10
|
-
static hooks: Required<import("../types/Config.ts").Hooks>;
|
|
10
|
+
static hooks: Required<import("../types/Config.d.ts").Hooks>;
|
|
11
11
|
/**
|
|
12
12
|
* @param {typeof Register | Register} target
|
|
13
13
|
* @param {keyof Hooks} hook
|
|
@@ -101,10 +101,10 @@ export class Register {
|
|
|
101
101
|
/**
|
|
102
102
|
* @param {Required<Hooks>} v
|
|
103
103
|
*/
|
|
104
|
-
set hooks(v: Required<
|
|
104
|
+
set hooks(v: Required<Hooks>);
|
|
105
105
|
/** @type {Required<Hooks>} */
|
|
106
|
-
get hooks(): Required<
|
|
107
|
-
_hooks: Required<import("../types/Config.ts").Hooks> | undefined;
|
|
106
|
+
get hooks(): Required<Hooks>;
|
|
107
|
+
_hooks: Required<import("../types/Config.d.ts").Hooks> | undefined;
|
|
108
108
|
/**
|
|
109
109
|
* @param {Transform} cfg
|
|
110
110
|
*/
|
|
@@ -144,11 +144,11 @@ export class Register {
|
|
|
144
144
|
fileHeader: FileHeader;
|
|
145
145
|
}): void;
|
|
146
146
|
}
|
|
147
|
-
export type FileHeader = import("../types/File.ts").FileHeader;
|
|
148
|
-
export type Parser = import("../types/Parser.ts").Parser;
|
|
149
|
-
export type Preprocessor = import("../types/Preprocessor.ts").Preprocessor;
|
|
150
|
-
export type Transform = import("../types/Transform.ts").Transform;
|
|
151
|
-
export type Filter = import("../types/Filter.ts").Filter;
|
|
152
|
-
export type Format = import("../types/Format.ts").Format;
|
|
153
|
-
export type Action = import("../types/Action.ts").Action;
|
|
154
|
-
export type Hooks = import("../types/Config.ts").Hooks;
|
|
147
|
+
export type FileHeader = import("../types/File.d.ts").FileHeader;
|
|
148
|
+
export type Parser = import("../types/Parser.d.ts").Parser;
|
|
149
|
+
export type Preprocessor = import("../types/Preprocessor.d.ts").Preprocessor;
|
|
150
|
+
export type Transform = import("../types/Transform.d.ts").Transform;
|
|
151
|
+
export type Filter = import("../types/Filter.d.ts").Filter;
|
|
152
|
+
export type Format = import("../types/Format.d.ts").Format;
|
|
153
|
+
export type Action = import("../types/Action.d.ts").Action;
|
|
154
|
+
export type Hooks = import("../types/Config.d.ts").Hooks;
|