Skip to content

Commit

Permalink
Update Design Tokens package to export multiple objects (#1380)
Browse files Browse the repository at this point in the history
* Separate token exports in design tokens package to make more tree shakable 
---------

Co-authored-by: James Mockett <1166188+jamesmockett@users.noreply.github.com>
  • Loading branch information
oliverabrahams and jamesmockett committed Apr 26, 2024
1 parent d3af7ef commit a8195c6
Show file tree
Hide file tree
Showing 14 changed files with 4,810 additions and 4,847 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-vans-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/source-foundations': patch
---

improved tree shaking by splitting tokens into separate objects
21 changes: 12 additions & 9 deletions libs/@guardian/cobalt-plugin-ts/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export default function pluginTS(options) {
*/
`;

// this is where we'll store the transformed tokens
/** @type {Object.<string, string>} */
const transformedTokens = {};

/** @type {Object.<string, string>} */
Expand All @@ -38,20 +38,23 @@ export default function pluginTS(options) {
}
}

const serialisedJS = serializeJS(transformedTokens, {
comments: jsDoc,
}).trim();
let typescriptSource = '';

// create a typescript source string containing the transformed
// tokens
const typescriptSource = `export const tokens = ${serialisedJS.replace(/;$/, '')} as const;`;
Object.keys(transformedTokens).forEach((tokenGroup) => {
const serialisedJS = serializeJS(transformedTokens[tokenGroup], {
comments: jsDoc,
}).trim();

// create a typescript source string containing the transformed tokens
typescriptSource += `export const ${tokenGroup} = ${serialisedJS.replace(/;$/, '')} as const;`;
});

// now we create a ts program to create the JS and declaration file
// contents. the program would write to disk by default, so we
// override that behaviour to capture the output in memory instead.

// this is where we'll store the output for the JS and declaration
// files
// this is where we'll store the output for the JS and declaration files
/** @type {Object.<string, string>} */
const output = {};

// we need to create a fake file name to fool the compiler into
Expand Down

0 comments on commit a8195c6

Please sign in to comment.