Skip to content

feat(tokens): use typescript in the tokens package #5557

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

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9af6920
chore(tokens) use typescript
myrta2302 Apr 29, 2025
0e9ab1f
Merge branch 'main' into 4499-use-typescript-in-the-tokens-package
myrta2302 May 30, 2025
b596540
type object-deepmerge
myrta2302 May 30, 2025
8929299
update object textoutput and deepmerge
myrta2302 May 30, 2025
4911fcb
methods.ts update
myrta2302 Jun 2, 2025
9e2ad2d
update all.ts and methods.ts
myrta2302 Jun 18, 2025
92cbdfe
updated replaceAllReferences function in methods.ts
myrta2302 Jun 27, 2025
cd4bfcc
update style-dictionary.ts
myrta2302 Jun 27, 2025
4c51611
remove meta interface change to LocalOptions
myrta2302 Jun 27, 2025
d76f3d3
type cleanup
myrta2302 Jun 27, 2025
f462532
update package.json and relative paths
myrta2302 Jun 27, 2025
00ff30d
update object-deepmerge.ts
myrta2302 Jun 27, 2025
77904de
Merge branch 'main' into 4499-use-typescript-in-the-tokens-package
myrta2302 Jun 27, 2025
509282b
add pnpm.-lock.yaml
myrta2302 Jun 27, 2025
a8b478f
fix code smell
myrta2302 Jun 27, 2025
7aac924
review comments update
myrta2302 Jul 7, 2025
551df5f
review comments update
myrta2302 Jul 7, 2025
9660451
Merge branch 'main' into 4499-use-typescript-in-the-tokens-package
myrta2302 Jul 18, 2025
74ec32f
update scripts
myrta2302 Jul 18, 2025
98b232a
Update pnpm-lock.yaml
myrta2302 Jul 18, 2025
c8e615b
Merge branch 'main' into 4499-use-typescript-in-the-tokens-package
myrta2302 Jul 18, 2025
f337485
Merge branch 'main' into 4499-use-typescript-in-the-tokens-package
myrta2302 Jul 21, 2025
89fb5c3
fix merge error
myrta2302 Jul 21, 2025
504a111
Merge branch 'main' into 4499-use-typescript-in-the-tokens-package
myrta2302 Jul 25, 2025
8e927ed
merge conflict update
myrta2302 Jul 25, 2025
8f2a440
Merge branch 'main' into 4499-use-typescript-in-the-tokens-package
myrta2302 Jul 25, 2025
9598986
Update pnpm-lock.yaml
myrta2302 Jul 25, 2025
73f48f6
Merge branch '4499-use-typescript-in-the-tokens-package' of https://g…
myrta2302 Jul 25, 2025
eb7f149
Merge branch 'main' into 4499-use-typescript-in-the-tokens-package
myrta2302 Aug 15, 2025
5a700d2
review comments update
myrta2302 Aug 15, 2025
2176689
code smell fix
myrta2302 Aug 15, 2025
373f335
Merge branch 'main' into 4499-use-typescript-in-the-tokens-package
myrta2302 Aug 18, 2025
96b0106
review comments update
myrta2302 Aug 22, 2025
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
12 changes: 0 additions & 12 deletions packages/tokens/_build/utils/object-deepmerge.js

This file was deleted.

12 changes: 8 additions & 4 deletions packages/tokens/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "Apache-2.0",
"private": false,
"type": "module",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/swisspost/design-system.git"
Expand All @@ -20,21 +21,24 @@
"linkDirectory": true
},
"scripts": {
"build": "pnpm clean && node ./build.js && pnpm copy-files",
"build:verbose": "node ./build.js --verbosity=verbose",
"build": "pnpm clean && tsc -p . && node ./dist/build.js && pnpm copy-files",
"build:verbose": "pnpm clean && tsc -p . && node ./dist/build.js --verbosity=verbose && pnpm copy-files",
"clean": "rimraf dist",
"lint": "eslint",
"lint:fix": "eslint --fix",
"copy-files": "copyfiles -f package.json README.md CONTRIBUTING.md CHANGELOG.md LICENSE dist"
"copy-files": "copyfiles -f package.json README.md CONTRIBUTING.md CHANGELOG.md LICENSE *.scss dist"
},
"devDependencies": {
"@eslint/js": "9.18.0",
"@tokens-studio/sd-transforms": "1.2.9",
"@types/node": "22.10.5",
"copyfiles": "2.4.1",
"eslint": "9.18.0",
"globals": "16.0.0",
"rimraf": "6.0.1",
"style-dictionary": "4.3.0"
"style-dictionary": "4.3.0",
"typescript": "5.8.3",
"typescript-eslint": "8.20.0"
},
"keywords": [
"tokens",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { fileHeader } from 'style-dictionary/utils';
import { expandTypesMap } from '@tokens-studio/sd-transforms';
import StyleDictionary from '../style-dictionary.js';
import StyleDictionary from 'style-dictionary';
import { getSetName, getSet, getTokenValue, registerConfigMethod } from '../methods.js';

import { ConfigWithMeta } from '_build/types.js';
/**
* Registers a config getter method to generate output files for all code relevant tokens in the tokens.json.
*/
Expand All @@ -19,7 +19,11 @@ registerConfigMethod((tokenSets, { sourcePath, buildPath }) => {
include: [`${sourcePath}_temp/source/**/*.json`],
platforms: {
scss: {
transforms: ['name/kebab', 'swisspost/scss-no-unitless-zero-values', 'swisspost/px-to-rem'],
transforms: [
'name/kebab',
'swisspost/scss-no-unitless-zero-values',
'swisspost/px-to-rem',
],
buildPath,
expand: {
include: ['typography'],
Expand Down Expand Up @@ -55,8 +59,9 @@ registerConfigMethod((tokenSets, { sourcePath, buildPath }) => {
*/
StyleDictionary.registerFilter({
name: 'swisspost/scss-filter',
filter: (token, { meta }) => {
return token.filePath.includes(`/output/${meta.filePath}`);
filter: (token, options) => {
const configOptions = options as ConfigWithMeta;
return token.filePath.includes(`/output/${configOptions.meta?.filePath}`);
},
});

Expand All @@ -75,12 +80,13 @@ StyleDictionary.registerFilter({
StyleDictionary.registerFormat({
name: 'swisspost/scss-format',
format: async ({ dictionary, options, file }) => {
const { meta } = options;
const { meta } = options as ConfigWithMeta;

const header = await fileHeader({ file, commentStyle: 'short' });

return (
header +
meta.setNames
meta?.setNames
.map(setName => {
const tokenSetName = getSetName(options, setName);
const tokenSet = getSet(options, dictionary, setName)
Expand All @@ -92,7 +98,6 @@ StyleDictionary.registerFormat({
: ` ${token.name}: ${tokenValue},`;
})
.join('\n');

return meta.layer === 'core'
? `:root {\n${tokenSet}\n}\n`
: `$${tokenSetName}: (\n${tokenSet}\n);\n`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fileHeader } from 'style-dictionary/utils';
import { TOKENSET_LAYERS, TOKENSET_NAMES, TOKENSET_PREFIX } from '../constants.js';
import StyleDictionary from '../style-dictionary.js';
import { registerConfigMethod, getTokenValue } from '../methods.js';

import { TokenProperty } from '_build/types.js';
const TAILWIND_TOKENSET_NAMES = [TOKENSET_NAMES.Utilities, TOKENSET_NAMES.Helpers];

/**
Expand Down Expand Up @@ -60,17 +60,20 @@ StyleDictionary.registerFormat({
name: 'swisspost/tailwind-v4-format',
format: async ({ dictionary, options, file }) => {
const header = await fileHeader({ file, commentStyle: 'long' }); // CSS comments

const themeVariables = dictionary.allTokens.reduce((allTokens, token) => {
const tokenPath = token.path.slice(token.path.indexOf(TOKENSET_PREFIX) + 1);

const cssVarName = `--${tokenPath.join('-')}`;

const tokenValue = getTokenValue(options, token);

allTokens[cssVarName] = tokenValue;
return allTokens;
}, {});

const themeVariables = dictionary.allTokens.reduce<Record<string, TokenProperty>>(
(allTokens, token) => {
const tokenPath = token.path.slice(token.path.indexOf(TOKENSET_PREFIX) + 1);

const cssVarName = `--${tokenPath.join('-')}`;

const tokenValue = getTokenValue(options, token);

allTokens[cssVarName] = tokenValue;
return allTokens;
},
{},
);

const themeCSS = Object.entries(themeVariables)
.map(([name, value]) => ` ${name}: ${value};`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@ import { fileHeader } from 'style-dictionary/utils';
import { TOKENSET_NAMES } from '../constants.js';
import StyleDictionary from '../style-dictionary.js';
import { registerConfigMethod, getTokenValue } from '../methods.js';
import { UtilityAttributes } from '_build/types.js';

/**
* Registers a config method to generate output files for utility tokens.
*/
registerConfigMethod((tokenSets, { sourcePath, buildPath }) => {
const { type, layer, filePath, sets } = tokenSets.output[TOKENSET_NAMES.Utilities];

return {
meta: {
type,
layer,
filePath,
setNames: Object.keys(sets),
},
source: [`${sourcePath}_temp/output/${filePath}`],
include: [`${sourcePath}_temp/source/**/*.json`],
platforms: {
utilities: {
transforms: ['name/kebab'],
buildPath,
files: [
{
destination: `_utilities-formatted.scss`,
filter: 'swisspost/source-tokens-filter',
format: 'swisspost/utility-format',
options: {
outputReferences: true,
const { type, layer, filePath, setNames } = tokenSets.output[TOKENSET_NAMES.Utilities];
return [
{
meta: {
type,
layer,
filePath,
setNames: Object.keys(setNames),
},
source: [`${sourcePath}_temp/output/${filePath}`],
include: [`${sourcePath}_temp/source/**/*.json`],
platforms: {
utilities: {
transforms: ['name/kebab'],
buildPath,
files: [
{
destination: `_utilities-formatted.scss`,
filter: 'swisspost/source-tokens-filter',
format: 'swisspost/utility-format',
options: {
outputReferences: true,
},
},
},
],
],
},
},
},
};
];
});

/**
Expand All @@ -48,7 +50,7 @@ StyleDictionary.registerFormat({
const utilityTokens = new Map();

dictionary.allTokens.forEach(token => {
const { subitem, state } = token.attributes;
const { subitem, state } = token.attributes as UtilityAttributes;

const previousStates = utilityTokens.get(subitem) ?? [];
const newState = `\n ${state}: ${getTokenValue(options, token)},`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ export const EXPLICIT_COMPONENT_LAYER_GROUPNAMES = [
];
export const EXPLICIT_FIGMAONLY_GROUPNAMES = ['figmaonly'];
export const EXPLICIT_FIGMAONLY_SETNAMES = ['figmaonly'];
export const TOKENSET_LAYERS = {
core: 'core',
semantic: 'semantic',
component: 'component',
};

export enum TOKENSET_LAYERS {
core = 'core',
component = 'component',
semantic = 'semantic',
}

export const TOKENSET_PREFIX = 'post';
export const CUSTOM_FORMAT_INDENT = ' ';

Expand Down
Loading