Skip to content

Commit

Permalink
Define set of i18n tokens via TypeScript (#4927)
Browse files Browse the repository at this point in the history
* Created an EuiTokensObject definition in eui.d.ts to declare existant i18n tokens

* changelog

* it's helpful to write working code
  • Loading branch information
chandlerprall authored Jul 6, 2021
1 parent 26f0a2c commit cd5e954
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Updated `EuiContextMenuPanelDescriptor`'s `title` prop type from `string` to `ReactNode` ([#4933](https://github.com/elastic/eui/pull/4933))
- Added `EuiTokensObject` type definition to allow enforcing i18n token coverage in consuming applications ([#4927](https://github.com/elastic/eui/issues/4927))

## [`34.5.2`](https://github.com/elastic/eui/tree/v34.5.2)

Expand Down
26 changes: 26 additions & 0 deletions scripts/dtsgenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const generator = dtsGenerator({
// 1. strip any `/// <reference` lines from the generated eui.d.ts
// 2. replace any import("src/...") declarations to import("@elastic/eui/src/...")
// 3. replace any import("./...") declarations to import("@elastic/eui/src/...)
// 4. generate & add EuiTokenObject
generator.then(() => {
const defsFilePath = path.resolve(baseDir, 'eui.d.ts');

Expand Down Expand Up @@ -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(
({ i18ndefs, tokens }, def) => {
if (!tokens.has(def.token)) {
tokens.add(def.token);
i18ndefs.push(def);
}
return { i18ndefs, tokens };
},
{ i18ndefs: [], tokens: new Set() }
);
return `
declare module '@elastic/eui' {
export type EuiTokensObject = {
${i18ndefs.map(({ token }) => `"${token}": any;`).join('\n')}
}
}
`;
}

0 comments on commit cd5e954

Please sign in to comment.