Skip to content

Commit d875cd2

Browse files
committed
feat: sort out loggers
1 parent 361a163 commit d875cd2

16 files changed

Lines changed: 522 additions & 436 deletions

File tree

cmp/compiler/src/plugin/next.ts

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import type { NextConfig } from "next";
22

3-
import path from "path";
4-
import fs from "fs/promises";
5-
import {
6-
getCacheDir,
7-
getCachePath,
8-
getConfigPath,
9-
} from "../utils/path-helpers";
3+
import { getConfigPath } from "../utils/path-helpers";
104
import { createLingoConfig } from "../utils/config-factory";
11-
import { logger } from "../utils/logger";
12-
import { startTranslationServer } from "../translation-server";
5+
import { logger, loggerRegistry } from "../utils/logger";
136
import type { PartialLingoConfig } from "../types";
147
import type { TurbopackOptions } from "next/dist/server/config-shared";
158

@@ -71,6 +64,12 @@ function buildLingoConfig(
7164
): NextConfig {
7265
const lingoConfig = createLingoConfig(lingoOptions);
7366

67+
loggerRegistry.create("plugin", {
68+
enableConsole: true,
69+
// TODO (AleksandrSl 04/12/2025): Make configurable
70+
enableDebug: true,
71+
});
72+
7473
// Prepare Turbopack loader configuration
7574
const loaderConfig = {
7675
loader: "@lingo.dev/_compiler/turbopack-loader",
@@ -156,63 +155,17 @@ function buildLingoConfig(
156155
}
157156

158157
logger.info("Running post-build translation generation...");
159-
let translationServer:
160-
| Awaited<ReturnType<typeof startTranslationServer>>
161-
| undefined;
162-
try {
163-
translationServer = await startTranslationServer({
164-
startPort: lingoConfig.dev?.serverStartPort,
165-
onError: (err) => {
166-
logger.error("Translation server error:", err);
167-
},
168-
config: lingoConfig,
169-
});
170-
171-
logger.info(
172-
`Processing translations for ${lingoConfig.targetLocales.length} locale(s)...`,
173-
);
174158

175-
// Translate all locales in parallel
176-
// Each translator handles its own batching strategy internally
177-
const localePromises = lingoConfig.targetLocales.map(async (locale) => {
178-
logger.info(`Translating to ${locale}...`);
179-
180-
const result = await translationServer!.translateAll(locale);
159+
try {
160+
const { processBuildTranslations } = await import("./build-translator");
181161

182-
if (result.errors.length > 0) {
183-
logger.warn(
184-
`Translation completed with ${result.errors.length} errors for ${locale}`,
185-
);
186-
}
187-
logger.info(`${locale} completed`);
162+
await processBuildTranslations({
163+
config: lingoConfig,
164+
publicOutputPath: distDir,
188165
});
189-
190-
await Promise.all(localePromises);
191-
192-
const publicPath = distDir;
193-
logger.info(`Generating static translation files in ${distDir}`);
194-
195-
await fs.mkdir(publicPath, { recursive: true });
196-
197-
for (const locale of lingoOptions.targetLocales) {
198-
const cacheFilePath = getCachePath(lingoConfig, locale);
199-
const publicFilePath = path.join(publicPath, `${locale}.json`);
200-
201-
try {
202-
await fs.copyFile(cacheFilePath, publicFilePath);
203-
logger.info(`✓ Generated ${locale}.json`);
204-
} catch (error) {
205-
logger.error(`Failed to copy ${locale}.json:`, error);
206-
}
207-
}
208-
209-
logger.info("Translation generation completed successfully");
210166
} catch (error) {
211167
logger.error("Translation generation failed:", error);
212168
throw error;
213-
} finally {
214-
translationServer?.stop();
215-
logger.info("Shutting down translation server...");
216169
}
217170
};
218171

cmp/compiler/src/plugin/unplugin.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,25 @@ export const cacheDir = ${JSON.stringify(cacheDir)};`;
131131
},
132132

133133
async buildEnd() {
134+
// Process build-time translations (only in production)
135+
if (!isDev) {
136+
try {
137+
const { processBuildTranslations } = await import(
138+
"./build-translator"
139+
);
140+
141+
await processBuildTranslations({
142+
config,
143+
// Note: publicOutputPath can be set by users in their config
144+
// For Vite, this might be dist/public or public/translations
145+
// For now, we don't set it here - users can handle it in their pipeline
146+
});
147+
} catch (error) {
148+
logger.error("Build-time translation processing failed:", error);
149+
throw error;
150+
}
151+
}
152+
134153
// Stop translation server
135154
if (globalServer) {
136155
await globalServer.stop();

cmp/compiler/src/translation-server/SERVER_CONFIG.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)