Skip to content

Commit

Permalink
feat: Update configurations to improve DX (#53)
Browse files Browse the repository at this point in the history
- Updated build to replace css from `/dist`
- Updated `mergeType` format
- Created `formatType` format to generate ts file with inline jsDoc
- Updated parser to rename description by comment

[category:Infrastructure]

Co-authored-by: @alanbsmith <[email protected]>
  • Loading branch information
RayRedGoose and alanbsmith authored Oct 23, 2023
1 parent ceb9b1f commit fdc28a7
Show file tree
Hide file tree
Showing 15 changed files with 511 additions and 311 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# compiled output
dist
docs
*/canvas-tokens-web/css
*/canvas-tokens-web/less
*/canvas-tokens-web/scss
tmp
/out-tsc
*.tsbuildinfo
Expand Down
6 changes: 3 additions & 3 deletions packages/canvas-tokens-docs/.storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Preview} from '@storybook/react';
import '@workday/canvas-tokens-web/dist/css/base/_variables.css';
import '@workday/canvas-tokens-web/dist/css/brand/_variables.css';
import '@workday/canvas-tokens-web/dist/css/system/_variables.css';
import '@workday/canvas-tokens-web/css/base/_variables.css';
import '@workday/canvas-tokens-web/css/brand/_variables.css';
import '@workday/canvas-tokens-web/css/system/_variables.css';

const preview: Preview = {
parameters: {
Expand Down
5 changes: 4 additions & 1 deletion packages/canvas-tokens-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"clean": "rimraf dist"
},
"files": [
"dist/"
"dist/",
"css/",
"less/",
"scss/"
],
"dependencies": {}
}
14 changes: 6 additions & 8 deletions packages/canvas-tokens/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ const config = setConfig({
platforms: ['css', 'scss', 'less', 'es6', 'common-js'],
levels: ['base', 'brand', 'sys'],
platformOptions: {
'*': {
buildPath: '../canvas-tokens-web/dist/',
},
'css, scss, less': {
buildPath: '../canvas-tokens-web/',
transformGroup: 'web',
fileName: '{platform}/{level}/_variables',
prefix: 'cnvs-',
Expand All @@ -36,22 +34,22 @@ const config = setConfig({
extensions: ['sass', 'scss'],
},
'es6, common-js': {
buildPath: '../canvas-tokens-web/dist/',
transformGroup: 'js',
transforms: ['value/variables', 'name/camel'],
transforms: ['value/flatten-border', 'value/flatten-base-shadow', 'name/camel'],
fileName: '{platform}/{level}/index',
extensions: ['js', 'd.ts'],
modifiers: [
{
level: ['base'],
extensions: ['js'],
format: 'javascript/{platform}',
format: 'js/{platform}',
filterByLevel: true,
},
{
level: ['base'],
extensions: ['d.ts'],
format: 'merge/types',
combineWith: ['javascript/{platform}'],
format: 'ts/inline',
filterByLevel: true,
},
{
Expand All @@ -64,7 +62,7 @@ const config = setConfig({
level: ['brand', 'sys'],
extensions: ['d.ts'],
format: 'merge/types',
combineWith: ['merge/objects', '{platform}/types'],
combineWith: ['merge/objects', 'ts/jsdoc-object'],
},
{
level: ['base'],
Expand Down
52 changes: 36 additions & 16 deletions packages/canvas-tokens/utils/formatters/formatJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,45 @@ import {jsFileHeader} from './helpers/jsFileHeader';
* options can contains `withoutModule` property js module header should not be generated.
* @returns file content as a string
*/
export const formatToInlineModule: Formatter = ({dictionary, file, options}) => {
export const formatToInlineCommonJSModule: Formatter = ({dictionary, file, options}) => {
const headerContent = !options.withoutModule
? jsFileHeader({file})
: formatHelpers.fileHeader({file});
return dictionary.allTokens.reduce((acc: string, {name, value}) => {
acc += `exports.${name} = "${value}";\n`;
return dictionary.allTokens.reduce((acc: string, {name, path}) => {
const cssVarName = path.join('-');
acc += `exports.${name} = "--cnvs-${cssVarName}";\n`;
return acc;
}, headerContent);
};

/**
* Style Dictionary format function that creates es6 file structure.
* This structure contains separated exports of each token.
* @param {*} FormatterArguments - Style Dictionary formatter object containing `dictionary`, `options`, `file` and `platform` properties.
* options can contains `withoutModule` property js module header should not be generated.
* @returns file content as a string
*/
export const formatToInlineES6Module: Formatter = ({dictionary, file, options}) => {
const headerContent = formatHelpers.fileHeader({file});
return dictionary.allTokens.reduce((acc: string, {name, path}) => {
const cssVarName = path.join('-');
acc += `export const ${name} = "--cnvs-${cssVarName}";\n`;
return acc;
}, headerContent);
};

/**
* Style Dictionary format function that creates ts file structure.
* This structure contains separated exports of each token with `as const`.
* @param {*} FormatterArguments - Style Dictionary formatter object containing `dictionary`, `options`, `file` and `platform` properties.
* options can contains `withoutModule` property js module header should not be generated.
* @returns file content as a string
*/
export const formatInlineTypes: Formatter = ({dictionary, file}) => {
const headerContent = formatHelpers.fileHeader({file});
return dictionary.allTokens.reduce((acc: string, {name, path}) => {
const cssVarName = path.join('-');
acc += `export declare const ${name} = "--cnvs-${cssVarName}" as const;\n`;
return acc;
}, headerContent);
};
Expand Down Expand Up @@ -45,19 +78,6 @@ export const formatES6ToObjects: Formatter = ({dictionary, file}) => {
}, headerContent);
};

/**
* Style Dictionary format function that create token type definitions.
* @param {*} FormatterArguments - Style Dictionary formatter object containing `dictionary`, `options`, `file` and `platform` properties.
* @returns file content as a string
*/
export const formatES6ToTypes: Formatter = ({dictionary, file}) => {
const headerContent = formatHelpers.fileHeader({file});
return Object.entries(dictionary.properties).reduce((acc: string, [key, values]) => {
return (acc +=
`export declare const ${key} = ` + JSON.stringify(values, null, 2) + ' as const;\n\n');
}, headerContent);
};

/**
* Style Dictionary format function that create the export index file for es6 folder.
* @param {*} FormatterArguments - Style Dictionary formatter object containing `dictionary`, `options`, `file` and `platform` properties.
Expand Down
99 changes: 99 additions & 0 deletions packages/canvas-tokens/utils/formatters/formatTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {Formatter, formatHelpers} from 'style-dictionary';

/**
* Style Dictionary format function that creates token type definitions with JSDoc.
* @param {*} FormatterArguments - Style Dictionary formatter object containing `dictionary`, `options`, `file` and `platform` properties.
* @returns file content as a string
*/
export const formatJSToTypes: Formatter = ({dictionary, file, options}) => {
const {originalValues} = options;
const headerContent = formatHelpers.fileHeader({file});

const placeholders = Object.keys(dictionary.properties)
.map(k => `**${k}**`)
.join('\n');

let content = placeholders;

const replaceInContent: ReplaceFn = (pattern, newValue) => {
content = content.replace(pattern, newValue);
return content;
};

recursivelyCreateFileStructure({
originalValues,
tokens: dictionary.properties,
content,
replaceInContent,
});

return headerContent + content;
};

const startingText = 'export declare const';
const endingText = 'as const;\n';

type ReplaceFn = (pattern: string, newText: string) => string;

type HelperArgs = {
originalValues: Record<string, any>;
tokens: Record<string, any>;
depth?: number;
content: string;
replaceInContent: ReplaceFn;
};

const recursivelyCreateFileStructure = ({
originalValues,
tokens,
content,
replaceInContent,
depth = 0,
}: HelperArgs) => {
let updatedContent = content;
const entries = Object.entries(tokens);

entries.forEach(([key, values]) => {
const original = originalValues[key];
const spaces = ' '.repeat(depth);
const extraSpaces = spaces + ' ';

if (typeof values === 'string') {
const pxVal = original.value.includes('rem') ? parseFloat(original.value) * 16 : null;
const hasComment = original.comment;
const commentParts = hasComment ? original.comment.split('; ') : [];
const commentText = hasComment
? `\n${extraSpaces}* ${commentParts.join(`\n${extraSpaces}* `)}\n${extraSpaces}*`
: '';
const valueText = ` ${original.value}${pxVal ? ` (${pxVal}px)` : ''} `;
const jsDocText = `${spaces}/**${commentText}${valueText}${
original.comment ? '\n' + extraSpaces : ''
}*/\n`;
const innerText = depth
? `${spaces}"${key}": "${values}",`
: `${startingText} ${key} = "${values}" ${endingText}`;
const fullInnerText = jsDocText + innerText;

updatedContent = replaceInContent(`**${key}**`, fullInnerText);
return;
}

const placeholders = Object.keys(values)
.map(k => `**${k}**`)
.join('\n');

const innerText = !depth
? `${startingText} ${key} = {\n${placeholders}\n} ${endingText}`
: `${spaces}"${key}": {\n${placeholders}\n${spaces}},`;

updatedContent = replaceInContent(`**${key}**`, innerText);

recursivelyCreateFileStructure({
originalValues: original,
tokens: values,
depth: depth + 1,
content: updatedContent,
replaceInContent,
});
});
};
Loading

0 comments on commit fdc28a7

Please sign in to comment.