@weglot/translate-react 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +6 -0
- package/README.md +190 -2
- package/build/_virtual/rolldown_runtime.cjs +29 -0
- package/build/_virtual/rolldown_runtime.mjs +7 -0
- package/build/index.cjs +0 -0
- package/build/index.d.cts +2 -0
- package/build/index.d.mts +2 -0
- package/build/index.mjs +1 -0
- package/build/locale.d.cts +6 -0
- package/build/locale.d.cts.map +1 -0
- package/build/locale.d.mts +6 -0
- package/build/locale.d.mts.map +1 -0
- package/build/metadata/manager.cjs +119 -0
- package/build/metadata/manager.mjs +114 -0
- package/build/metadata/manager.mjs.map +1 -0
- package/build/plugin/build-translator.cjs +207 -0
- package/build/plugin/build-translator.mjs +205 -0
- package/build/plugin/build-translator.mjs.map +1 -0
- package/build/plugin/cleanup.cjs +20 -0
- package/build/plugin/cleanup.mjs +20 -0
- package/build/plugin/cleanup.mjs.map +1 -0
- package/build/plugin/index.cjs +0 -0
- package/build/plugin/index.d.cts +2 -0
- package/build/plugin/index.d.mts +2 -0
- package/build/plugin/index.mjs +1 -0
- package/build/plugin/next-compiler-loader.cjs +40 -0
- package/build/plugin/next-compiler-loader.d.cts +12 -0
- package/build/plugin/next-compiler-loader.d.cts.map +1 -0
- package/build/plugin/next-compiler-loader.d.mts +13 -0
- package/build/plugin/next-compiler-loader.d.mts.map +1 -0
- package/build/plugin/next-compiler-loader.mjs +41 -0
- package/build/plugin/next-compiler-loader.mjs.map +1 -0
- package/build/plugin/next-config-loader.cjs +13 -0
- package/build/plugin/next-config-loader.d.cts +8 -0
- package/build/plugin/next-config-loader.d.cts.map +1 -0
- package/build/plugin/next-config-loader.d.mts +9 -0
- package/build/plugin/next-config-loader.d.mts.map +1 -0
- package/build/plugin/next-config-loader.mjs +14 -0
- package/build/plugin/next-config-loader.mjs.map +1 -0
- package/build/plugin/next-locale-client-loader.cjs +9 -0
- package/build/plugin/next-locale-client-loader.d.cts +8 -0
- package/build/plugin/next-locale-client-loader.d.cts.map +1 -0
- package/build/plugin/next-locale-client-loader.d.mts +9 -0
- package/build/plugin/next-locale-client-loader.d.mts.map +1 -0
- package/build/plugin/next-locale-client-loader.mjs +10 -0
- package/build/plugin/next-locale-client-loader.mjs.map +1 -0
- package/build/plugin/next-locale-server-loader.cjs +9 -0
- package/build/plugin/next-locale-server-loader.d.cts +8 -0
- package/build/plugin/next-locale-server-loader.d.cts.map +1 -0
- package/build/plugin/next-locale-server-loader.d.mts +9 -0
- package/build/plugin/next-locale-server-loader.d.mts.map +1 -0
- package/build/plugin/next-locale-server-loader.mjs +10 -0
- package/build/plugin/next-locale-server-loader.mjs.map +1 -0
- package/build/plugin/next.cjs +226 -0
- package/build/plugin/next.d.cts +9 -0
- package/build/plugin/next.d.cts.map +1 -0
- package/build/plugin/next.d.mts +9 -0
- package/build/plugin/next.d.mts.map +1 -0
- package/build/plugin/next.mjs +228 -0
- package/build/plugin/next.mjs.map +1 -0
- package/build/plugin/transform/babel-compat.cjs +13 -0
- package/build/plugin/transform/babel-compat.mjs +10 -0
- package/build/plugin/transform/babel-compat.mjs.map +1 -0
- package/build/plugin/transform/expression-strings.cjs +145 -0
- package/build/plugin/transform/expression-strings.mjs +140 -0
- package/build/plugin/transform/expression-strings.mjs.map +1 -0
- package/build/plugin/transform/index.cjs +45 -0
- package/build/plugin/transform/index.mjs +43 -0
- package/build/plugin/transform/index.mjs.map +1 -0
- package/build/plugin/transform/metadata.cjs +142 -0
- package/build/plugin/transform/metadata.mjs +141 -0
- package/build/plugin/transform/metadata.mjs.map +1 -0
- package/build/plugin/transform/process-file.cjs +525 -0
- package/build/plugin/transform/process-file.mjs +524 -0
- package/build/plugin/transform/process-file.mjs.map +1 -0
- package/build/plugin/transform/use-i18n.cjs +8 -0
- package/build/plugin/transform/use-i18n.mjs +7 -0
- package/build/plugin/transform/use-i18n.mjs.map +1 -0
- package/build/plugin/transform/utils.cjs +237 -0
- package/build/plugin/transform/utils.mjs +219 -0
- package/build/plugin/transform/utils.mjs.map +1 -0
- package/build/plugin/unplugin.cjs +188 -0
- package/build/plugin/unplugin.d.cts +8 -0
- package/build/plugin/unplugin.d.cts.map +1 -0
- package/build/plugin/unplugin.d.mts +8 -0
- package/build/plugin/unplugin.d.mts.map +1 -0
- package/build/plugin/unplugin.mjs +188 -0
- package/build/plugin/unplugin.mjs.map +1 -0
- package/build/plugin/vite.cjs +27 -0
- package/build/plugin/vite.d.cts +9 -0
- package/build/plugin/vite.d.cts.map +1 -0
- package/build/plugin/vite.d.mts +9 -0
- package/build/plugin/vite.d.mts.map +1 -0
- package/build/plugin/vite.mjs +28 -0
- package/build/plugin/vite.mjs.map +1 -0
- package/build/plugin/webpack.cjs +26 -0
- package/build/plugin/webpack.d.cts +8 -0
- package/build/plugin/webpack.d.cts.map +1 -0
- package/build/plugin/webpack.d.mts +8 -0
- package/build/plugin/webpack.d.mts.map +1 -0
- package/build/plugin/webpack.mjs +27 -0
- package/build/plugin/webpack.mjs.map +1 -0
- package/build/react/client/index.cjs +9 -0
- package/build/react/client/index.d.cts +5 -0
- package/build/react/client/index.d.mts +5 -0
- package/build/react/client/index.mjs +6 -0
- package/build/react/client/useTranslation.cjs +71 -0
- package/build/react/client/useTranslation.d.cts +42 -0
- package/build/react/client/useTranslation.d.cts.map +1 -0
- package/build/react/client/useTranslation.d.mts +42 -0
- package/build/react/client/useTranslation.d.mts.map +1 -0
- package/build/react/client/useTranslation.mjs +71 -0
- package/build/react/client/useTranslation.mjs.map +1 -0
- package/build/react/next/client.cjs +25 -0
- package/build/react/next/client.d.cts +9 -0
- package/build/react/next/client.d.cts.map +1 -0
- package/build/react/next/client.d.mts +9 -0
- package/build/react/next/client.d.mts.map +1 -0
- package/build/react/next/client.mjs +24 -0
- package/build/react/next/client.mjs.map +1 -0
- package/build/react/next/cookie-locale-resolver.cjs +29 -0
- package/build/react/next/cookie-locale-resolver.d.cts +33 -0
- package/build/react/next/cookie-locale-resolver.d.cts.map +1 -0
- package/build/react/next/cookie-locale-resolver.d.mts +33 -0
- package/build/react/next/cookie-locale-resolver.d.mts.map +1 -0
- package/build/react/next/cookie-locale-resolver.mjs +29 -0
- package/build/react/next/cookie-locale-resolver.mjs.map +1 -0
- package/build/react/next/server.cjs +21 -0
- package/build/react/next/server.d.cts +13 -0
- package/build/react/next/server.d.cts.map +1 -0
- package/build/react/next/server.d.mts +14 -0
- package/build/react/next/server.d.mts.map +1 -0
- package/build/react/next/server.mjs +20 -0
- package/build/react/next/server.mjs.map +1 -0
- package/build/react/server/ServerWeglotProvider.cjs +19 -0
- package/build/react/server/ServerWeglotProvider.d.cts +12 -0
- package/build/react/server/ServerWeglotProvider.d.cts.map +1 -0
- package/build/react/server/ServerWeglotProvider.d.mts +12 -0
- package/build/react/server/ServerWeglotProvider.d.mts.map +1 -0
- package/build/react/server/ServerWeglotProvider.mjs +19 -0
- package/build/react/server/ServerWeglotProvider.mjs.map +1 -0
- package/build/react/server/index.cjs +7 -0
- package/build/react/server/index.d.cts +4 -0
- package/build/react/server/index.d.mts +4 -0
- package/build/react/server/index.mjs +5 -0
- package/build/react/server/useTranslation.cjs +60 -0
- package/build/react/server/useTranslation.d.cts +36 -0
- package/build/react/server/useTranslation.d.cts.map +1 -0
- package/build/react/server/useTranslation.d.mts +36 -0
- package/build/react/server/useTranslation.d.mts.map +1 -0
- package/build/react/server/useTranslation.mjs +60 -0
- package/build/react/server/useTranslation.mjs.map +1 -0
- package/build/react/server-only/index.cjs +42 -0
- package/build/react/server-only/index.d.cts +38 -0
- package/build/react/server-only/index.d.cts.map +1 -0
- package/build/react/server-only/index.d.mts +38 -0
- package/build/react/server-only/index.d.mts.map +1 -0
- package/build/react/server-only/index.mjs +42 -0
- package/build/react/server-only/index.mjs.map +1 -0
- package/build/react/server-only/translations.cjs +86 -0
- package/build/react/server-only/translations.mjs +86 -0
- package/build/react/server-only/translations.mjs.map +1 -0
- package/build/react/shared/LocaleSwitcher.cjs +61 -0
- package/build/react/shared/LocaleSwitcher.d.cts +71 -0
- package/build/react/shared/LocaleSwitcher.d.cts.map +1 -0
- package/build/react/shared/LocaleSwitcher.d.mts +71 -0
- package/build/react/shared/LocaleSwitcher.d.mts.map +1 -0
- package/build/react/shared/LocaleSwitcher.mjs +61 -0
- package/build/react/shared/LocaleSwitcher.mjs.map +1 -0
- package/build/react/shared/WeglotContext.cjs +14 -0
- package/build/react/shared/WeglotContext.d.cts +34 -0
- package/build/react/shared/WeglotContext.d.cts.map +1 -0
- package/build/react/shared/WeglotContext.d.mts +34 -0
- package/build/react/shared/WeglotContext.d.mts.map +1 -0
- package/build/react/shared/WeglotContext.mjs +13 -0
- package/build/react/shared/WeglotContext.mjs.map +1 -0
- package/build/react/shared/WeglotProvider.cjs +250 -0
- package/build/react/shared/WeglotProvider.d.cts +60 -0
- package/build/react/shared/WeglotProvider.d.cts.map +1 -0
- package/build/react/shared/WeglotProvider.d.mts +60 -0
- package/build/react/shared/WeglotProvider.d.mts.map +1 -0
- package/build/react/shared/WeglotProvider.mjs +250 -0
- package/build/react/shared/WeglotProvider.mjs.map +1 -0
- package/build/react/shared/render-rich-text.cjs +56 -0
- package/build/react/shared/render-rich-text.d.cts +16 -0
- package/build/react/shared/render-rich-text.d.cts.map +1 -0
- package/build/react/shared/render-rich-text.d.mts +16 -0
- package/build/react/shared/render-rich-text.d.mts.map +1 -0
- package/build/react/shared/render-rich-text.mjs +55 -0
- package/build/react/shared/render-rich-text.mjs.map +1 -0
- package/build/react/shared/utils.cjs +34 -0
- package/build/react/shared/utils.mjs +35 -0
- package/build/react/shared/utils.mjs.map +1 -0
- package/build/react/types.d.cts +16 -0
- package/build/react/types.d.cts.map +1 -0
- package/build/react/types.d.mts +16 -0
- package/build/react/types.d.mts.map +1 -0
- package/build/runtime/html/segmenter.cjs +226 -0
- package/build/runtime/html/segmenter.mjs +225 -0
- package/build/runtime/html/segmenter.mjs.map +1 -0
- package/build/runtime/index.cjs +6 -0
- package/build/runtime/index.d.cts +2 -0
- package/build/runtime/index.d.mts +2 -0
- package/build/runtime/index.mjs +3 -0
- package/build/runtime/translate-dynamic.cjs +142 -0
- package/build/runtime/translate-dynamic.d.cts +29 -0
- package/build/runtime/translate-dynamic.d.cts.map +1 -0
- package/build/runtime/translate-dynamic.d.mts +29 -0
- package/build/runtime/translate-dynamic.d.mts.map +1 -0
- package/build/runtime/translate-dynamic.mjs +139 -0
- package/build/runtime/translate-dynamic.mjs.map +1 -0
- package/build/translation-server/logger.cjs +37 -0
- package/build/translation-server/logger.mjs +37 -0
- package/build/translation-server/logger.mjs.map +1 -0
- package/build/translation-server/translation-server.cjs +451 -0
- package/build/translation-server/translation-server.mjs +448 -0
- package/build/translation-server/translation-server.mjs.map +1 -0
- package/build/translators/api.cjs +12 -0
- package/build/translators/api.mjs +12 -0
- package/build/translators/api.mjs.map +1 -0
- package/build/translators/cache-factory.cjs +26 -0
- package/build/translators/cache-factory.mjs +27 -0
- package/build/translators/cache-factory.mjs.map +1 -0
- package/build/translators/local-cache.cjs +132 -0
- package/build/translators/local-cache.mjs +130 -0
- package/build/translators/local-cache.mjs.map +1 -0
- package/build/translators/translation-service.cjs +137 -0
- package/build/translators/translation-service.mjs +138 -0
- package/build/translators/translation-service.mjs.map +1 -0
- package/build/translators/weglot/api-client.cjs +79 -0
- package/build/translators/weglot/api-client.mjs +79 -0
- package/build/translators/weglot/api-client.mjs.map +1 -0
- package/build/translators/weglot/definitions/definitions.generated.cjs +53 -0
- package/build/translators/weglot/definitions/definitions.generated.mjs +52 -0
- package/build/translators/weglot/definitions/definitions.generated.mjs.map +1 -0
- package/build/translators/weglot/engine.cjs +46 -0
- package/build/translators/weglot/engine.mjs +43 -0
- package/build/translators/weglot/engine.mjs.map +1 -0
- package/build/translators/weglot/translator.cjs +61 -0
- package/build/translators/weglot/translator.mjs +62 -0
- package/build/translators/weglot/translator.mjs.map +1 -0
- package/build/translators/weglot/wire-format.cjs +128 -0
- package/build/translators/weglot/wire-format.mjs +129 -0
- package/build/translators/weglot/wire-format.mjs.map +1 -0
- package/build/types.d.cts +143 -0
- package/build/types.d.cts.map +1 -0
- package/build/types.d.mts +143 -0
- package/build/types.d.mts.map +1 -0
- package/build/utils/config-factory.cjs +86 -0
- package/build/utils/config-factory.mjs +83 -0
- package/build/utils/config-factory.mjs.map +1 -0
- package/build/utils/hash.cjs +17 -0
- package/build/utils/hash.mjs +16 -0
- package/build/utils/hash.mjs.map +1 -0
- package/build/utils/is-valid-locale.cjs +13 -0
- package/build/utils/is-valid-locale.mjs +13 -0
- package/build/utils/is-valid-locale.mjs.map +1 -0
- package/build/utils/logger.cjs +51 -0
- package/build/utils/logger.mjs +50 -0
- package/build/utils/logger.mjs.map +1 -0
- package/build/utils/path-helpers.cjs +49 -0
- package/build/utils/path-helpers.mjs +47 -0
- package/build/utils/path-helpers.mjs.map +1 -0
- package/build/utils/timeout.cjs +42 -0
- package/build/utils/timeout.mjs +41 -0
- package/build/utils/timeout.mjs.map +1 -0
- package/build/virtual/code-generator.cjs +56 -0
- package/build/virtual/code-generator.mjs +55 -0
- package/build/virtual/code-generator.mjs.map +1 -0
- package/build/virtual/config.cjs +16 -0
- package/build/virtual/config.d.cts +13 -0
- package/build/virtual/config.d.cts.map +1 -0
- package/build/virtual/config.d.mts +13 -0
- package/build/virtual/config.d.mts.map +1 -0
- package/build/virtual/config.mjs +12 -0
- package/build/virtual/config.mjs.map +1 -0
- package/build/virtual/locale/client.cjs +23 -0
- package/build/virtual/locale/client.d.cts +19 -0
- package/build/virtual/locale/client.d.cts.map +1 -0
- package/build/virtual/locale/client.d.mts +19 -0
- package/build/virtual/locale/client.d.mts.map +1 -0
- package/build/virtual/locale/client.mjs +22 -0
- package/build/virtual/locale/client.mjs.map +1 -0
- package/build/virtual/locale/server.cjs +13 -0
- package/build/virtual/locale/server.d.cts +13 -0
- package/build/virtual/locale/server.d.cts.map +1 -0
- package/build/virtual/locale/server.d.mts +13 -0
- package/build/virtual/locale/server.d.mts.map +1 -0
- package/build/virtual/locale/server.mjs +13 -0
- package/build/virtual/locale/server.mjs.map +1 -0
- package/package.json +260 -4
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { logger } from "../../utils/logger.mjs";
|
|
2
|
+
import { generate } from "./babel-compat.mjs";
|
|
3
|
+
import { processFile } from "./process-file.mjs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import * as parser from "@babel/parser";
|
|
6
|
+
|
|
7
|
+
//#region src/plugin/transform/index.ts
|
|
8
|
+
/**
|
|
9
|
+
* Transform component code to inject translation calls
|
|
10
|
+
*/
|
|
11
|
+
function transformComponent({ code, filePath, config }) {
|
|
12
|
+
const relativeFilePath = path.relative(path.resolve(config.sourceRoot), filePath).split(path.sep).join("/");
|
|
13
|
+
try {
|
|
14
|
+
const ast = parser.parse(code, {
|
|
15
|
+
sourceType: "module",
|
|
16
|
+
plugins: ["jsx", "typescript"]
|
|
17
|
+
});
|
|
18
|
+
logger.debug(`Transforming ${filePath}`);
|
|
19
|
+
const translationEntries = processFile(ast, {
|
|
20
|
+
relativeFilePath,
|
|
21
|
+
needsDirective: config.useDirective,
|
|
22
|
+
translatableAttributes: config.translatableAttributes
|
|
23
|
+
});
|
|
24
|
+
const output = generate(ast, { sourceMaps: true }, code);
|
|
25
|
+
logger.debug(`Transformed ${filePath}. Code: ${output.code}`);
|
|
26
|
+
return {
|
|
27
|
+
code: output.code,
|
|
28
|
+
map: output.map,
|
|
29
|
+
newEntries: translationEntries,
|
|
30
|
+
transformed: translationEntries.length > 0
|
|
31
|
+
};
|
|
32
|
+
} catch (error) {
|
|
33
|
+
logger.error(`Failed to transform ${filePath}:`, error);
|
|
34
|
+
return {
|
|
35
|
+
code,
|
|
36
|
+
transformed: false
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
export { transformComponent };
|
|
43
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/plugin/transform/index.ts"],"sourcesContent":["import * as parser from \"@babel/parser\";\nimport path from \"path\";\nimport type { WeglotConfig, TranslationEntry } from \"../../types\";\nimport { processFile } from \"./process-file\";\nimport { logger } from \"../../utils/logger\";\nimport { generate } from \"./babel-compat\";\n\nexport interface TransformResult {\n code: string;\n map?: any;\n newEntries?: TranslationEntry[];\n transformed: boolean;\n}\n\nexport interface BabelTransformOptions {\n code: string;\n filePath: string;\n config: WeglotConfig;\n}\n\n/**\n * Transform component code to inject translation calls\n */\nexport function transformComponent({\n code,\n filePath,\n config,\n}: BabelTransformOptions): TransformResult {\n // Get relative file path for consistent hashing\n const relativeFilePath = path\n .relative(path.resolve(config.sourceRoot), filePath)\n .split(path.sep)\n // '/' is used as a cross-platform path separator\n .join(\"/\");\n\n try {\n const ast = parser.parse(code, {\n sourceType: \"module\",\n plugins: [\"jsx\", \"typescript\"],\n });\n\n logger.debug(`Transforming ${filePath}`);\n\n const translationEntries = processFile(ast, {\n relativeFilePath,\n needsDirective: config.useDirective,\n translatableAttributes: config.translatableAttributes,\n });\n\n const output = generate(\n ast,\n {\n sourceMaps: true,\n },\n code,\n );\n\n logger.debug(`Transformed ${filePath}. Code: ${output.code}`);\n\n return {\n code: output.code,\n map: output.map,\n newEntries: translationEntries,\n transformed: translationEntries.length > 0,\n };\n } catch (error) {\n // TODO (AleksandrSl 08/12/2025): Should also log the message\n logger.error(`Failed to transform ${filePath}:`, error);\n // Return original code on error\n return {\n code,\n transformed: false,\n };\n }\n}\n"],"mappings":";;;;;;;;;;AAuBA,SAAgB,mBAAmB,EACjC,MACA,UACA,UACyC;CAEzC,MAAM,mBAAmB,KACtB,SAAS,KAAK,QAAQ,OAAO,WAAW,EAAE,SAAS,CACnD,MAAM,KAAK,IAAI,CAEf,KAAK,IAAI;AAEZ,KAAI;EACF,MAAM,MAAM,OAAO,MAAM,MAAM;GAC7B,YAAY;GACZ,SAAS,CAAC,OAAO,aAAa;GAC/B,CAAC;AAEF,SAAO,MAAM,gBAAgB,WAAW;EAExC,MAAM,qBAAqB,YAAY,KAAK;GAC1C;GACA,gBAAgB,OAAO;GACvB,wBAAwB,OAAO;GAChC,CAAC;EAEF,MAAM,SAAS,SACb,KACA,EACE,YAAY,MACb,EACD,KACD;AAED,SAAO,MAAM,eAAe,SAAS,UAAU,OAAO,OAAO;AAE7D,SAAO;GACL,MAAM,OAAO;GACb,KAAK,OAAO;GACZ,YAAY;GACZ,aAAa,mBAAmB,SAAS;GAC1C;UACM,OAAO;AAEd,SAAO,MAAM,uBAAuB,SAAS,IAAI,MAAM;AAEvD,SAAO;GACL;GACA,aAAa;GACd"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_utils = require('./utils.cjs');
|
|
3
|
+
let _babel_types = require("@babel/types");
|
|
4
|
+
_babel_types = require_rolldown_runtime.__toESM(_babel_types);
|
|
5
|
+
|
|
6
|
+
//#region src/plugin/transform/metadata.ts
|
|
7
|
+
/**
|
|
8
|
+
* Transforms Next.js metadata object to use t() calls. See https://nextjs.org/docs/app/getting-started/metadata-and-og-images for docs on next metadata.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Whitelist of metadata fields that should be translated.
|
|
12
|
+
* Other fields (like technical configurations, URLs, etc.) are left unchanged.
|
|
13
|
+
*/
|
|
14
|
+
const TRANSLATABLE_METADATA_FIELDS = new Set([
|
|
15
|
+
"title",
|
|
16
|
+
"description",
|
|
17
|
+
"title.template",
|
|
18
|
+
"title.default",
|
|
19
|
+
"openGraph.title",
|
|
20
|
+
"openGraph.description",
|
|
21
|
+
"openGraph.images[*].alt",
|
|
22
|
+
"twitter.title",
|
|
23
|
+
"twitter.description",
|
|
24
|
+
"twitter.images[*].alt",
|
|
25
|
+
"appleWebApp.title"
|
|
26
|
+
]);
|
|
27
|
+
const NEXT_METADATA_EXPORT_NAME = "metadata";
|
|
28
|
+
const NEXT_GENERATE_METADATA_FUNCTION_NAME = "generateMetadata";
|
|
29
|
+
/**
|
|
30
|
+
* Check if a field path matches a pattern with array indices.
|
|
31
|
+
* For example: "openGraph.images[0].alt" matches "openGraph.images[*].alt"
|
|
32
|
+
*/
|
|
33
|
+
function matchesArrayPattern(fieldPath, pattern) {
|
|
34
|
+
return fieldPath.replace(/\[\d+]/g, "[*]") === pattern;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Check if a metadata field path should be translated
|
|
38
|
+
*/
|
|
39
|
+
function isTranslatableMetadataField(fieldPath) {
|
|
40
|
+
if (TRANSLATABLE_METADATA_FIELDS.has(fieldPath)) return true;
|
|
41
|
+
for (const pattern of TRANSLATABLE_METADATA_FIELDS) if (pattern.includes("[*]") && matchesArrayPattern(fieldPath, pattern)) return true;
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
function isNextGenerateMetadataFunction(path) {
|
|
45
|
+
if (path.node.id?.name === NEXT_GENERATE_METADATA_FUNCTION_NAME) {
|
|
46
|
+
const parent = path.parent;
|
|
47
|
+
if (parent.type === "ExportNamedDeclaration" || parent.type === "Program") return true;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function getNextMetadataObjectOrNull(path) {
|
|
51
|
+
const declaration = path.node.declaration;
|
|
52
|
+
if (!declaration || declaration.type !== "VariableDeclaration") return null;
|
|
53
|
+
const declarator = declaration.declarations[0];
|
|
54
|
+
if (!declarator || declarator.id.type !== "Identifier") return null;
|
|
55
|
+
if (declarator.id.name === NEXT_METADATA_EXPORT_NAME && declarator.init) return declarator.init;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Recursively transforms metadata object properties to use t() calls
|
|
59
|
+
*/
|
|
60
|
+
function transformMetadataObject(objectExpression, state, entries, parentPath = "") {
|
|
61
|
+
for (const prop of objectExpression.properties) {
|
|
62
|
+
if (prop.type !== "ObjectProperty") continue;
|
|
63
|
+
let keyName = null;
|
|
64
|
+
if (prop.key.type === "Identifier") keyName = prop.key.name;
|
|
65
|
+
else if (prop.key.type === "StringLiteral") keyName = prop.key.value;
|
|
66
|
+
if (!keyName) continue;
|
|
67
|
+
const fieldPath = parentPath ? `${parentPath}.${keyName}` : keyName;
|
|
68
|
+
const shouldTranslate = isTranslatableMetadataField(fieldPath);
|
|
69
|
+
if (prop.value.type === "StringLiteral" && shouldTranslate) {
|
|
70
|
+
const text = prop.value.value;
|
|
71
|
+
const entry = require_utils.createTranslationEntry("metadata", text, { fieldPath }, state.filePath, prop.value.loc?.start.line, prop.value.loc?.start.column);
|
|
72
|
+
entries.push(entry);
|
|
73
|
+
prop.value = _babel_types.callExpression(_babel_types.identifier("t"), [_babel_types.stringLiteral(entry.hash), _babel_types.stringLiteral(text)]);
|
|
74
|
+
} else if (prop.value.type === "TemplateLiteral" && shouldTranslate) {
|
|
75
|
+
if (prop.value.expressions.length === 0 && prop.value.quasis.length === 1) {
|
|
76
|
+
const staticValue = prop.value.quasis[0].value.cooked;
|
|
77
|
+
if (staticValue) {
|
|
78
|
+
const entry = require_utils.createTranslationEntry("metadata", staticValue, { fieldPath }, state.filePath, prop.value.loc?.start.line, prop.value.loc?.start.column);
|
|
79
|
+
entries.push(entry);
|
|
80
|
+
prop.value = _babel_types.callExpression(_babel_types.identifier("t"), [_babel_types.stringLiteral(entry.hash), _babel_types.stringLiteral(staticValue)]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
} else if (prop.value.type === "ObjectExpression") transformMetadataObject(prop.value, state, entries, fieldPath);
|
|
84
|
+
else if (prop.value.type === "ArrayExpression") prop.value.elements.forEach((element, index) => {
|
|
85
|
+
if (element && element.type === "ObjectExpression") transformMetadataObject(element, state, entries, `${fieldPath}[${index}]`);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Transform existing generateMetadata function to add translations
|
|
91
|
+
*/
|
|
92
|
+
function transformMetadataFunction(path, state) {
|
|
93
|
+
const body = path.get("body");
|
|
94
|
+
if (!body.isBlockStatement()) return null;
|
|
95
|
+
let metadataReturn = void 0;
|
|
96
|
+
body.traverse({ ReturnStatement(returnPath) {
|
|
97
|
+
if (!metadataReturn) metadataReturn = returnPath;
|
|
98
|
+
} });
|
|
99
|
+
if (!(0, _babel_types.isReturnStatement)(metadataReturn) || !metadataReturn.node.argument) return null;
|
|
100
|
+
if (!(0, _babel_types.isObjectExpression)(metadataReturn.node.argument)) return null;
|
|
101
|
+
const entries = [];
|
|
102
|
+
const metadataObject = metadataReturn.node.argument;
|
|
103
|
+
transformMetadataObject(metadataObject, state, entries);
|
|
104
|
+
if (entries.length === 0) return null;
|
|
105
|
+
if (!path.node.async) path.node.async = true;
|
|
106
|
+
const serverCall = require_utils.constructServerTranslationHookCall({ hashes: entries.map((e) => e.hash) });
|
|
107
|
+
body.node.body.unshift(serverCall);
|
|
108
|
+
return {
|
|
109
|
+
needsAsyncImport: true,
|
|
110
|
+
newEntries: entries
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Process static metadata export
|
|
115
|
+
* Only converts to function if there are translatable strings
|
|
116
|
+
*/
|
|
117
|
+
function processNextStaticMetadata(path, state) {
|
|
118
|
+
const metadataObject = getNextMetadataObjectOrNull(path);
|
|
119
|
+
if (!metadataObject || metadataObject.type !== "ObjectExpression") return null;
|
|
120
|
+
const clonedObject = _babel_types.cloneNode(metadataObject, true);
|
|
121
|
+
const entries = [];
|
|
122
|
+
transformMetadataObject(clonedObject, state, entries);
|
|
123
|
+
if (entries.length === 0) return null;
|
|
124
|
+
const serverCall = require_utils.constructServerTranslationHookCall({ hashes: entries.map((e) => e.hash) });
|
|
125
|
+
const generateMetadataFunction = _babel_types.functionDeclaration(_babel_types.identifier(NEXT_GENERATE_METADATA_FUNCTION_NAME), [], _babel_types.blockStatement([serverCall, _babel_types.returnStatement(clonedObject)]), false, true);
|
|
126
|
+
path.replaceWith(_babel_types.exportNamedDeclaration(generateMetadataFunction, []));
|
|
127
|
+
path.skip();
|
|
128
|
+
return {
|
|
129
|
+
needsAsyncImport: true,
|
|
130
|
+
newEntries: entries
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function processNextDynamicMetadata(path, state) {
|
|
134
|
+
if (!isNextGenerateMetadataFunction(path)) return null;
|
|
135
|
+
const newState = transformMetadataFunction(path, state);
|
|
136
|
+
path.skip();
|
|
137
|
+
return newState;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
//#endregion
|
|
141
|
+
exports.processNextDynamicMetadata = processNextDynamicMetadata;
|
|
142
|
+
exports.processNextStaticMetadata = processNextStaticMetadata;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { constructServerTranslationHookCall, createTranslationEntry } from "./utils.mjs";
|
|
2
|
+
import * as t from "@babel/types";
|
|
3
|
+
import { isObjectExpression, isReturnStatement } from "@babel/types";
|
|
4
|
+
|
|
5
|
+
//#region src/plugin/transform/metadata.ts
|
|
6
|
+
/**
|
|
7
|
+
* Transforms Next.js metadata object to use t() calls. See https://nextjs.org/docs/app/getting-started/metadata-and-og-images for docs on next metadata.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Whitelist of metadata fields that should be translated.
|
|
11
|
+
* Other fields (like technical configurations, URLs, etc.) are left unchanged.
|
|
12
|
+
*/
|
|
13
|
+
const TRANSLATABLE_METADATA_FIELDS = new Set([
|
|
14
|
+
"title",
|
|
15
|
+
"description",
|
|
16
|
+
"title.template",
|
|
17
|
+
"title.default",
|
|
18
|
+
"openGraph.title",
|
|
19
|
+
"openGraph.description",
|
|
20
|
+
"openGraph.images[*].alt",
|
|
21
|
+
"twitter.title",
|
|
22
|
+
"twitter.description",
|
|
23
|
+
"twitter.images[*].alt",
|
|
24
|
+
"appleWebApp.title"
|
|
25
|
+
]);
|
|
26
|
+
const NEXT_METADATA_EXPORT_NAME = "metadata";
|
|
27
|
+
const NEXT_GENERATE_METADATA_FUNCTION_NAME = "generateMetadata";
|
|
28
|
+
/**
|
|
29
|
+
* Check if a field path matches a pattern with array indices.
|
|
30
|
+
* For example: "openGraph.images[0].alt" matches "openGraph.images[*].alt"
|
|
31
|
+
*/
|
|
32
|
+
function matchesArrayPattern(fieldPath, pattern) {
|
|
33
|
+
return fieldPath.replace(/\[\d+]/g, "[*]") === pattern;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Check if a metadata field path should be translated
|
|
37
|
+
*/
|
|
38
|
+
function isTranslatableMetadataField(fieldPath) {
|
|
39
|
+
if (TRANSLATABLE_METADATA_FIELDS.has(fieldPath)) return true;
|
|
40
|
+
for (const pattern of TRANSLATABLE_METADATA_FIELDS) if (pattern.includes("[*]") && matchesArrayPattern(fieldPath, pattern)) return true;
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
function isNextGenerateMetadataFunction(path) {
|
|
44
|
+
if (path.node.id?.name === NEXT_GENERATE_METADATA_FUNCTION_NAME) {
|
|
45
|
+
const parent = path.parent;
|
|
46
|
+
if (parent.type === "ExportNamedDeclaration" || parent.type === "Program") return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function getNextMetadataObjectOrNull(path) {
|
|
50
|
+
const declaration = path.node.declaration;
|
|
51
|
+
if (!declaration || declaration.type !== "VariableDeclaration") return null;
|
|
52
|
+
const declarator = declaration.declarations[0];
|
|
53
|
+
if (!declarator || declarator.id.type !== "Identifier") return null;
|
|
54
|
+
if (declarator.id.name === NEXT_METADATA_EXPORT_NAME && declarator.init) return declarator.init;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Recursively transforms metadata object properties to use t() calls
|
|
58
|
+
*/
|
|
59
|
+
function transformMetadataObject(objectExpression, state, entries, parentPath = "") {
|
|
60
|
+
for (const prop of objectExpression.properties) {
|
|
61
|
+
if (prop.type !== "ObjectProperty") continue;
|
|
62
|
+
let keyName = null;
|
|
63
|
+
if (prop.key.type === "Identifier") keyName = prop.key.name;
|
|
64
|
+
else if (prop.key.type === "StringLiteral") keyName = prop.key.value;
|
|
65
|
+
if (!keyName) continue;
|
|
66
|
+
const fieldPath = parentPath ? `${parentPath}.${keyName}` : keyName;
|
|
67
|
+
const shouldTranslate = isTranslatableMetadataField(fieldPath);
|
|
68
|
+
if (prop.value.type === "StringLiteral" && shouldTranslate) {
|
|
69
|
+
const text = prop.value.value;
|
|
70
|
+
const entry = createTranslationEntry("metadata", text, { fieldPath }, state.filePath, prop.value.loc?.start.line, prop.value.loc?.start.column);
|
|
71
|
+
entries.push(entry);
|
|
72
|
+
prop.value = t.callExpression(t.identifier("t"), [t.stringLiteral(entry.hash), t.stringLiteral(text)]);
|
|
73
|
+
} else if (prop.value.type === "TemplateLiteral" && shouldTranslate) {
|
|
74
|
+
if (prop.value.expressions.length === 0 && prop.value.quasis.length === 1) {
|
|
75
|
+
const staticValue = prop.value.quasis[0].value.cooked;
|
|
76
|
+
if (staticValue) {
|
|
77
|
+
const entry = createTranslationEntry("metadata", staticValue, { fieldPath }, state.filePath, prop.value.loc?.start.line, prop.value.loc?.start.column);
|
|
78
|
+
entries.push(entry);
|
|
79
|
+
prop.value = t.callExpression(t.identifier("t"), [t.stringLiteral(entry.hash), t.stringLiteral(staticValue)]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} else if (prop.value.type === "ObjectExpression") transformMetadataObject(prop.value, state, entries, fieldPath);
|
|
83
|
+
else if (prop.value.type === "ArrayExpression") prop.value.elements.forEach((element, index) => {
|
|
84
|
+
if (element && element.type === "ObjectExpression") transformMetadataObject(element, state, entries, `${fieldPath}[${index}]`);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Transform existing generateMetadata function to add translations
|
|
90
|
+
*/
|
|
91
|
+
function transformMetadataFunction(path, state) {
|
|
92
|
+
const body = path.get("body");
|
|
93
|
+
if (!body.isBlockStatement()) return null;
|
|
94
|
+
let metadataReturn = void 0;
|
|
95
|
+
body.traverse({ ReturnStatement(returnPath) {
|
|
96
|
+
if (!metadataReturn) metadataReturn = returnPath;
|
|
97
|
+
} });
|
|
98
|
+
if (!isReturnStatement(metadataReturn) || !metadataReturn.node.argument) return null;
|
|
99
|
+
if (!isObjectExpression(metadataReturn.node.argument)) return null;
|
|
100
|
+
const entries = [];
|
|
101
|
+
const metadataObject = metadataReturn.node.argument;
|
|
102
|
+
transformMetadataObject(metadataObject, state, entries);
|
|
103
|
+
if (entries.length === 0) return null;
|
|
104
|
+
if (!path.node.async) path.node.async = true;
|
|
105
|
+
const serverCall = constructServerTranslationHookCall({ hashes: entries.map((e) => e.hash) });
|
|
106
|
+
body.node.body.unshift(serverCall);
|
|
107
|
+
return {
|
|
108
|
+
needsAsyncImport: true,
|
|
109
|
+
newEntries: entries
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Process static metadata export
|
|
114
|
+
* Only converts to function if there are translatable strings
|
|
115
|
+
*/
|
|
116
|
+
function processNextStaticMetadata(path, state) {
|
|
117
|
+
const metadataObject = getNextMetadataObjectOrNull(path);
|
|
118
|
+
if (!metadataObject || metadataObject.type !== "ObjectExpression") return null;
|
|
119
|
+
const clonedObject = t.cloneNode(metadataObject, true);
|
|
120
|
+
const entries = [];
|
|
121
|
+
transformMetadataObject(clonedObject, state, entries);
|
|
122
|
+
if (entries.length === 0) return null;
|
|
123
|
+
const serverCall = constructServerTranslationHookCall({ hashes: entries.map((e) => e.hash) });
|
|
124
|
+
const generateMetadataFunction = t.functionDeclaration(t.identifier(NEXT_GENERATE_METADATA_FUNCTION_NAME), [], t.blockStatement([serverCall, t.returnStatement(clonedObject)]), false, true);
|
|
125
|
+
path.replaceWith(t.exportNamedDeclaration(generateMetadataFunction, []));
|
|
126
|
+
path.skip();
|
|
127
|
+
return {
|
|
128
|
+
needsAsyncImport: true,
|
|
129
|
+
newEntries: entries
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function processNextDynamicMetadata(path, state) {
|
|
133
|
+
if (!isNextGenerateMetadataFunction(path)) return null;
|
|
134
|
+
const newState = transformMetadataFunction(path, state);
|
|
135
|
+
path.skip();
|
|
136
|
+
return newState;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
//#endregion
|
|
140
|
+
export { processNextDynamicMetadata, processNextStaticMetadata };
|
|
141
|
+
//# sourceMappingURL=metadata.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metadata.mjs","names":["keyName: string | null","entries: TranslationEntry[]"],"sources":["../../../src/plugin/transform/metadata.ts"],"sourcesContent":["/**\n * Transforms Next.js metadata object to use t() calls. See https://nextjs.org/docs/app/getting-started/metadata-and-og-images for docs on next metadata.\n */\nimport * as t from \"@babel/types\";\nimport {\n isObjectExpression,\n isReturnStatement,\n type VariableDeclarator,\n} from \"@babel/types\";\nimport {\n constructServerTranslationHookCall,\n createTranslationEntry,\n} from \"./utils\";\nimport type { NodePath } from \"@babel/traverse\";\nimport type { TranslationEntry } from \"../../types\";\nimport type { VisitorsInternalState } from \"./process-file\";\n\n/**\n * Whitelist of metadata fields that should be translated.\n * Other fields (like technical configurations, URLs, etc.) are left unchanged.\n */\nconst TRANSLATABLE_METADATA_FIELDS = new Set([\n // Top-level fields\n \"title\",\n \"description\",\n\n // Title object fields (template and default)\n \"title.template\",\n \"title.default\",\n\n // OpenGraph fields\n \"openGraph.title\",\n \"openGraph.description\",\n \"openGraph.images[*].alt\",\n\n // Twitter fields\n \"twitter.title\",\n \"twitter.description\",\n \"twitter.images[*].alt\",\n\n // Apple Web App\n \"appleWebApp.title\",\n]);\n\nconst NEXT_METADATA_EXPORT_NAME = \"metadata\";\nconst NEXT_GENERATE_METADATA_FUNCTION_NAME = \"generateMetadata\";\n\ntype MetadataInputState = Pick<VisitorsInternalState, \"filePath\">;\nexport type MetadataOutputState = Pick<\n VisitorsInternalState,\n \"newEntries\" | \"needsAsyncImport\"\n>;\n\n/**\n * Check if a field path matches a pattern with array indices.\n * For example: \"openGraph.images[0].alt\" matches \"openGraph.images[*].alt\"\n */\nfunction matchesArrayPattern(fieldPath: string, pattern: string): boolean {\n // Replace [number] with [*] for comparison\n const normalizedPath = fieldPath.replace(/\\[\\d+]/g, \"[*]\");\n return normalizedPath === pattern;\n}\n\n/**\n * Check if a metadata field path should be translated\n */\nfunction isTranslatableMetadataField(fieldPath: string): boolean {\n // Direct match\n if (TRANSLATABLE_METADATA_FIELDS.has(fieldPath)) {\n return true;\n }\n\n // Check for array pattern matches (e.g., images[0].alt matches images[*].alt)\n for (const pattern of TRANSLATABLE_METADATA_FIELDS) {\n if (pattern.includes(\"[*]\") && matchesArrayPattern(fieldPath, pattern)) {\n return true;\n }\n }\n\n return false;\n}\n\nfunction isNextGenerateMetadataFunction(path: NodePath<t.FunctionDeclaration>) {\n if (path.node.id?.name === NEXT_GENERATE_METADATA_FUNCTION_NAME) {\n // Check if it's exported\n const parent = path.parent;\n if (parent.type === \"ExportNamedDeclaration\" || parent.type === \"Program\") {\n return true;\n }\n }\n}\n\nfunction getNextMetadataObjectOrNull(\n path: NodePath<t.ExportNamedDeclaration>,\n): VariableDeclarator[\"init\"] | null {\n const declaration = path.node.declaration;\n if (!declaration || declaration.type !== \"VariableDeclaration\") return null;\n\n const declarator = declaration.declarations[0];\n if (!declarator || declarator.id.type !== \"Identifier\") return null;\n if (declarator.id.name === NEXT_METADATA_EXPORT_NAME && declarator.init) {\n return declarator.init;\n }\n}\n\n/**\n * Recursively transforms metadata object properties to use t() calls\n */\nfunction transformMetadataObject(\n objectExpression: t.ObjectExpression,\n state: MetadataInputState,\n entries: TranslationEntry[],\n parentPath: string = \"\",\n): void {\n for (const prop of objectExpression.properties) {\n if (prop.type !== \"ObjectProperty\") continue;\n\n // Get the key name\n let keyName: string | null = null;\n if (prop.key.type === \"Identifier\") {\n keyName = prop.key.name;\n } else if (prop.key.type === \"StringLiteral\") {\n keyName = prop.key.value;\n }\n\n if (!keyName) continue;\n\n const fieldPath = parentPath ? `${parentPath}.${keyName}` : keyName;\n\n // Check if this field should be translated\n const shouldTranslate = isTranslatableMetadataField(fieldPath);\n // Transform string values (only if whitelisted)\n if (prop.value.type === \"StringLiteral\" && shouldTranslate) {\n const text = prop.value.value;\n const entry = createTranslationEntry(\n \"metadata\",\n text,\n {\n fieldPath: fieldPath,\n },\n state.filePath,\n prop.value.loc?.start.line,\n prop.value.loc?.start.column,\n );\n entries.push(entry);\n\n // Replace with t(hash, fallback) call\n prop.value = t.callExpression(t.identifier(\"t\"), [\n t.stringLiteral(entry.hash),\n t.stringLiteral(text),\n ]);\n // Only static template literals like `content`\n } else if (prop.value.type === \"TemplateLiteral\" && shouldTranslate) {\n if (\n prop.value.expressions.length === 0 &&\n prop.value.quasis.length === 1\n ) {\n const staticValue = prop.value.quasis[0].value.cooked;\n if (staticValue) {\n const entry = createTranslationEntry(\n \"metadata\",\n staticValue,\n {\n fieldPath: fieldPath,\n },\n state.filePath,\n prop.value.loc?.start.line,\n prop.value.loc?.start.column,\n );\n entries.push(entry);\n\n // Replace with t(hash, fallback) call\n prop.value = t.callExpression(t.identifier(\"t\"), [\n t.stringLiteral(entry.hash),\n t.stringLiteral(staticValue),\n ]);\n }\n }\n }\n // Transform nested objects (always recurse to check nested fields)\n else if (prop.value.type === \"ObjectExpression\") {\n transformMetadataObject(prop.value, state, entries, fieldPath);\n }\n // Handle arrays (e.g., images: [{url: '...', alt: '...'}])\n else if (prop.value.type === \"ArrayExpression\") {\n prop.value.elements.forEach((element, index) => {\n if (element && element.type === \"ObjectExpression\") {\n transformMetadataObject(\n element,\n state,\n entries,\n `${fieldPath}[${index}]`,\n );\n }\n });\n }\n }\n}\n\n/**\n * Transform existing generateMetadata function to add translations\n */\nfunction transformMetadataFunction(\n path: NodePath<t.FunctionDeclaration>,\n state: MetadataInputState,\n): MetadataOutputState | null {\n const body = path.get(\"body\");\n if (!body.isBlockStatement()) return null;\n\n // Find the return statement with metadata object\n let metadataReturn = undefined as NodePath<t.ReturnStatement> | undefined;\n body.traverse({\n ReturnStatement(returnPath) {\n if (!metadataReturn) {\n metadataReturn = returnPath;\n }\n },\n });\n\n if (!isReturnStatement(metadataReturn) || !metadataReturn.node.argument)\n return null;\n if (!isObjectExpression(metadataReturn.node.argument)) return null;\n\n const entries: TranslationEntry[] = [];\n const metadataObject = metadataReturn.node.argument;\n transformMetadataObject(metadataObject, state, entries);\n\n if (entries.length === 0) {\n return null;\n }\n\n // Server translations require function to be async.\n if (!path.node.async) {\n path.node.async = true;\n }\n\n const serverCall = constructServerTranslationHookCall({\n hashes: entries.map((e) => e.hash),\n });\n body.node.body.unshift(serverCall);\n\n return {\n needsAsyncImport: true,\n newEntries: entries,\n };\n}\n\n/**\n * Process static metadata export\n * Only converts to function if there are translatable strings\n */\nexport function processNextStaticMetadata(\n path: NodePath<t.ExportNamedDeclaration>,\n state: MetadataInputState,\n): MetadataOutputState | null {\n // Get the metadata object\n const metadataObject = getNextMetadataObjectOrNull(path);\n if (!metadataObject || metadataObject.type !== \"ObjectExpression\")\n return null;\n\n // Clone and try transforming (transformMetadataObject modifies in place)\n const clonedObject = t.cloneNode(metadataObject, true);\n const entries: TranslationEntry[] = [];\n transformMetadataObject(clonedObject, state, entries);\n\n // If nothing to translate, leave as-is\n if (entries.length === 0) {\n return null;\n }\n\n // Create async generateMetadata function with transformed object\n const serverCall = constructServerTranslationHookCall({\n hashes: entries.map((e) => e.hash),\n });\n\n const generateMetadataFunction = t.functionDeclaration(\n t.identifier(NEXT_GENERATE_METADATA_FUNCTION_NAME),\n [],\n t.blockStatement([serverCall, t.returnStatement(clonedObject)]),\n false,\n true, // async - required for server translations\n );\n\n // Replace static export with function export\n path.replaceWith(t.exportNamedDeclaration(generateMetadataFunction, []));\n path.skip();\n\n return {\n needsAsyncImport: true,\n newEntries: entries,\n };\n}\n\nexport function processNextDynamicMetadata(\n path: NodePath<t.FunctionDeclaration>,\n state: MetadataInputState,\n): MetadataOutputState | null {\n if (!isNextGenerateMetadataFunction(path)) return null;\n const newState = transformMetadataFunction(path, state);\n path.skip();\n return newState;\n}\n"],"mappings":"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"}
|