Skip to content
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

UX-749 Generate JS token files #1028

Merged
merged 3 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 35 additions & 6 deletions packages/design-tokens-sd/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { mapGet, utils, colorMapGet } = require('./templates/scss-functions');
const { toSnake, toCamel } = require('./utils/utils');

module.exports = {
source: ['tokens/**/*.json'],
Expand Down Expand Up @@ -31,17 +32,45 @@ module.exports = {
},
],
},
// Generates JS files in snake case
js: {
transformGroup: 'js',
buildPath: 'dist/js/',
transforms: ['name/cti/snake'],
files: [
{
destination: 'tokens.js',
format: 'javascript/module-flat',
},
],
},
// Generates JS files in the old format of snake & camel using a custom transform
js_legacy: {
transformGroup: 'js',
buildPath: 'dist/js/',
transforms: ['name/cti/legacy'],
files: [
{
destination: 'tokens.legacy.js',
format: 'javascript/module-flat',
},
],
},
},
transform: {
'name/cti/legacy': {
type: 'name',
transformer: (token) => {
const [category, ...rest] = token.path;
return `${toCamel(category)}_${toSnake(rest.join('-'))}`;
},
},
},
format: {
'scss/functions': (args) => {
const keys = Object.keys(args.dictionary.tokens);
const rootFontSize = args.dictionary.allTokens.find(({ name }) => name === 'font-size-root');

const functions = keys
.map((key) => {
return key !== 'color' ? mapGet(key) : colorMapGet();
})
.join('');
const functions = keys.map((key) => (key !== 'color' ? mapGet(key) : colorMapGet())).join('');
return `${utils(rootFontSize)}\n${functions}`;
},
},
Expand Down
21 changes: 21 additions & 0 deletions packages/design-tokens-sd/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function toCamel(string) {
return string
.replace('-', ' ')
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
})
.replace(/\s+/g, '');
}

function toSnake(string) {
return string
.replace(/\W+/g, ' ')
.split(/ |\B(?=[A-Z])/)
.map((word) => word.toLowerCase())
.join('_');
}

module.exports = {
toCamel,
toSnake,
};