-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🪟 🔧 Compile i18n messages at build time (#6481)
- Loading branch information
Showing
5 changed files
with
96 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
airbyte-webapp/packages/vite-plugins/compile-formatjs-msgs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Plugin } from "vite"; | ||
|
||
import { parse } from "@formatjs/icu-messageformat-parser"; | ||
|
||
export function compileFormatJsMessages(): Plugin { | ||
return { | ||
name: "airbyte/compile-formatjs-messages", | ||
transform(code: string, id: string) { | ||
// Transform all JSON files under /locales/ | ||
if (/\/locales\/.+\.json$/.test(id)) { | ||
if (!code.startsWith("export default {") || !code.endsWith("};\n")) { | ||
throw new Error(`Tried to compile JSON locale file, but got an unexpected format. (${id})`); | ||
} | ||
// Remove JS export, to extract raw JSON from that file | ||
const rawJson = code.replace(/^export default {/, "{").replace(/\};\n$/, "}"); | ||
const messages = JSON.parse(rawJson); | ||
// Parse every message using the formatjs utils into an AST | ||
const compiledMsgs = Object.fromEntries( | ||
Object.entries(messages).map(([key, i18nmsg]) => [key, parse(i18nmsg as string)]) | ||
); | ||
return `export default ${JSON.stringify(compiledMsgs)};`; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export { docMiddleware } from "./doc-middleware"; | ||
export { buildInfo } from "./build-info"; | ||
export { environmentVariables } from "./environment-variables"; | ||
export { experimentOverwrites } from "./experiment-overwrites"; | ||
export { compileFormatJsMessages } from "./compile-formatjs-msgs"; |
Oops, something went wrong.