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

task: adds fonts #21

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Binary file added assets/fonts/Inter-Medium.woff
Binary file not shown.
Binary file added assets/fonts/Inter-Medium.woff2
Binary file not shown.
Binary file added assets/fonts/Inter-Regular.woff
Binary file not shown.
Binary file added assets/fonts/Inter-Regular.woff2
Binary file not shown.
Binary file added assets/fonts/wellcome-bold-webfont.woff
Binary file not shown.
Binary file added assets/fonts/wellcome-bold-webfont.woff2
Binary file not shown.
42 changes: 39 additions & 3 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import StyleDictionary from "style-dictionary";
import { registerTransforms } from "@tokens-studio/sd-transforms";

import filterExcludeTokens from "./utils/filters/filterExcludeTokens.js";

import formatFontFace from "./utils/formats/formatFontFace.js";

import transformFont from "./utils/transforms/transformFont.js";
import transformToRem from "./utils/transforms/transformToRem.js";

registerTransforms(StyleDictionary);
Expand All @@ -15,7 +19,7 @@ const common = {
};

const sd = new StyleDictionary({
source: ["tokens-figma/*.json"],
source: ["tokens/**/*.json"],
platforms: {
css: {
...common,
Expand All @@ -25,11 +29,30 @@ const sd = new StyleDictionary({
destination: `css/${groupName}.css`,
format: "css/variables",
filter: (token) =>
token.filePath === `tokens-figma/${groupName}.json` &&
token.filePath === `tokens/tokens-figma/${groupName}.json` &&
filterExcludeTokens(token),
};
}),
},
fonts: {
...common,
transforms: ["custom/font"],
files: [
{
destination: "css/fonts.css",
format: "custom/font-face",
filter: {
attributes: {
category: "asset",
type: "font",
},
},
options: {
fontPathPrefix: "../../assets/",
},
},
],
},
json: {
...common,
files: [
Expand All @@ -43,6 +66,19 @@ const sd = new StyleDictionary({
},
});

sd.registerFormat({
name: "custom/font-face",
format: ({ dictionary: { allTokens }, options }) =>
formatFontFace(allTokens, options),
});

sd.registerTransform({
name: "custom/font",
type: "attribute",
filter: (token) => token.path[0] === "asset" && token.path[1] === "font",
transform: (token) => transformFont(token.path),
});

sd.registerTransform({
name: "custom/rem",
type: "value",
Expand All @@ -59,4 +95,4 @@ sd.registerTransform({
});

sd.cleanAllPlatforms();
sd.buildAllPlatforms();
sd.buildAllPlatforms();
4 changes: 2 additions & 2 deletions split-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const rawData = fs.readFileSync(jsonFile);
const jsonData = JSON.parse(rawData);

// Define the base output directory
const baseDir = path.join(__dirname, "tokens-figma");
const baseDir = path.join(__dirname, "tokens/tokens-figma");

// Define keys to include
const includeKeys = ["GLOBAL/core", "GLOBAL/semantic"];
Expand Down Expand Up @@ -46,4 +46,4 @@ includeKeys.forEach((key) => {
}
});

console.log("JSON files have been created in the tokens directory.");
console.log("JSON files have been created in the tokens directory.");
1 change: 0 additions & 1 deletion tokens-figma/README.md

This file was deleted.

26 changes: 26 additions & 0 deletions tokens-generated/css/fonts.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@font-face {
font-display: swap;
font-family: "Inter";
font-style: normal;
font-weight: 400;
src: url("../../assets/fonts/Inter-Regular.woff2") format("woff2"),
url("../../assets/fonts/Inter-Regular.woff") format("woff");
}

@font-face {
font-display: swap;
font-family: "Inter";
font-style: normal;
font-weight: 500;
src: url("../../assets/fonts/Inter-Medium.woff2") format("woff2"),
url("../../assets/fonts/Inter-Medium.woff") format("woff");
}

@font-face {
font-display: swap;
font-family: "Wellcome";
font-style: normal;
font-weight: 700;
src: url("../../assets/fonts/wellcome-bold-webfont.woff2") format("woff2"),
url("../../assets/fonts/wellcome-bold-webfont.woff") format("woff");
}
5 changes: 5 additions & 0 deletions tokens/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Important

Please note that editing the files in the *tokens-figma* folder directly is not recommended. These files are automatically generated after running the command `npm run split-json`.

`fonts.json` can be edited directly. Please run `npm run build` after editing this file.
37 changes: 37 additions & 0 deletions tokens/fonts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"asset": {
"font": {
"Inter": {
"400": {
"normal": {
"value": "fonts/Inter-Regular",
"formats": [
"woff2",
"woff"
]
}
},
"500": {
"normal": {
"value": "fonts/Inter-Medium",
"formats": [
"woff2",
"woff"
]
}
}
},
"Wellcome": {
"700": {
"normal": {
"value": "fonts/wellcome-bold-webfont",
"formats": [
"woff2",
"woff"
]
}
}
}
}
}
}
File renamed without changes.
File renamed without changes.
48 changes: 48 additions & 0 deletions utils/formats/formatFontFace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
*
* @param allTokens
* @param options
* @returns {*}
*
* @see {@link https://github.com/amzn/style-dictionary/blob/main/examples/advanced/font-face-rules/sd.config.js#L18}
*/
const formatFontFace = (allTokens, options) => {
const fontPathPrefix = options.fontPathPrefix || "../";

// https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/src
const formatsMap = {
woff2: "woff2",
woff: "woff",
};

return allTokens
.reduce((fontList, prop) => {
const {
attributes: { family, weight, style },
formats,
value: path,
} = prop;

const urls = formats.map(
(extension) =>
`url("${fontPathPrefix}${path}.${extension}") format("${formatsMap[extension]}")`,
);

const fontCss = [
"@font-face {",
"\n\tfont-display: swap;",
`\n\tfont-family: "${family}";`,
`\n\tfont-style: ${style};`,
`\n\tfont-weight: ${weight};`,
`\n\tsrc: ${urls.join(",\n\t\t\t ")};`,
"\n}\n",
].join("");

fontList.push(fontCss);

return fontList;
}, [])
.join("\n");
};

export default formatFontFace;
17 changes: 17 additions & 0 deletions utils/transforms/transformFont.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Helper: transform to codify the font's details as named attributes
*
* @see {@link https://github.com/amzn/style-dictionary/blob/main/examples/advanced/font-face-rules/sd.config.js#L8}
*/

function transformFont(tokenPath) {
return {
category: tokenPath[0],
type: tokenPath[1],
family: tokenPath[2],
weight: tokenPath[3],
style: tokenPath[4],
};
}

export default transformFont;