Skip to content

Commit f88f048

Browse files
committed
feat: emit extraction details when eneable DEBUG=rsbuild env
- Add debug mode support via DEBUG environment variable - Output extraction details to node_modules/.rsbuild-plugin-i18next-extractor/ when enabled
1 parent 174846b commit f88f048

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.1.4
4+
5+
### Features
6+
7+
- Add debug mode support via `DEBUG` environment variable to output extraction details to `node_modules/.rsbuild-plugin-i18next-extractor/`.
8+
39
## 0.1.3
410

511
### Bug Fixes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rsbuild-plugin-i18next-extractor",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "A Rsbuild plugin for extracting i18n translations using i18next-cli",
55
"keywords": [
66
"rsbuild",

src/I18nextExtractorWebpackPlugin.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import {
99
resolveLocaleFilePath,
1010
} from './utils.js';
1111

12+
const DEBUG = (function isDebug() {
13+
if (!process.env.DEBUG) {
14+
return false;
15+
}
16+
const values = process.env.DEBUG.toLocaleLowerCase().split(',');
17+
return ['rsbuild', 'rsbuild:i18next', 'rsbuild:*', '*'].some((key) =>
18+
values.includes(key),
19+
);
20+
})();
21+
1222
interface I18nextExtractorWebpackPluginOptions
1323
extends PluginI18nextExtractorOptions {
1424
logger: { warn: (message: string) => void };
@@ -146,6 +156,46 @@ export class I18nextExtractorWebpackPlugin {
146156
},
147157
);
148158

159+
// Write debug output to node_modules when DEBUG is enabled
160+
if (DEBUG) {
161+
try {
162+
const debugDir = path.resolve(
163+
compiler.context,
164+
'node_modules',
165+
'.rsbuild-plugin-i18next-extractor',
166+
entryName,
167+
);
168+
await fs.mkdir(debugDir, { recursive: true });
169+
const debugFilePath = path.join(
170+
debugDir,
171+
`${locale}.json`,
172+
);
173+
await fs.writeFile(
174+
debugFilePath,
175+
JSON.stringify(
176+
{
177+
locale,
178+
entry: entryName,
179+
extractedKeys: extractedTranslationKeys[locale],
180+
extractedTranslations,
181+
originTranslations: originTranslations[locale],
182+
},
183+
null,
184+
2,
185+
),
186+
'utf-8',
187+
);
188+
console.log(
189+
`[rsbuild-plugin-i18next-extractor] Debug file written: ${path.relative(compiler.context, debugFilePath)}`,
190+
);
191+
} catch (error) {
192+
console.warn(
193+
`[rsbuild-plugin-i18next-extractor] Failed to write debug file for ${locale}:`,
194+
error,
195+
);
196+
}
197+
}
198+
149199
i18nTranslationDefinitions.push(
150200
`const ${getLocaleVariableName(locale)} = ${JSON.stringify(extractedTranslations)};`,
151201
);

0 commit comments

Comments
 (0)