diff --git a/scripts/dtsgenerator.js b/scripts/dtsgenerator.js index abf58a0372f8..355b277d23fd 100644 --- a/scripts/dtsgenerator.js +++ b/scripts/dtsgenerator.js @@ -96,6 +96,7 @@ const generator = dtsGenerator({ // 1. strip any `/// { const defsFilePath = path.resolve(baseDir, 'eui.d.ts'); @@ -144,5 +145,30 @@ generator.then(() => { ); } ) // end 3. + .replace(/$/, `\n\n${buildEuiTokensObject()}`) // 4. ); }); + +/** For step 4 **/ +// i18ntokens.json is generated as the first step in the build and can be relied upon here +function buildEuiTokensObject() { + // reduce over the tokens list as a few of the tokens are used multiple times and must be + // filtered down to a list + const i18ndefs = require('../i18ntokens.json').reduce( + ({ definitions, tokens }, def) => { + if (tokens.has(def.token)) { + tokens.add(def.token); + definitions.push(def); + } + return definitions; + }, + { definitions: [], tokens: new Set() } + ); + return ` +declare module '@elastic/eui' { + export type EuiTokensObject = { + ${i18ndefs.map(({ token }) => `"${token}": any;`).join('\n')} + } +} + `; +}