i18next-cli 1.11.11 → 1.11.13
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/CHANGELOG.md +8 -0
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/extractor/parsers/jsx-parser.js +1 -1
- package/dist/esm/cli.js +1 -1
- package/dist/esm/extractor/parsers/jsx-parser.js +1 -1
- package/package.json +1 -1
- package/src/cli.ts +24 -7
- package/src/extractor/parsers/jsx-parser.ts +68 -10
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.11.13](https://github.com/i18next/i18next-cli/compare/v1.11.12...v1.11.13) - 2025-10-22
|
|
9
|
+
|
|
10
|
+
- Fix: Stop infinite watch loops by properly honoring extract.ignore patterns [#70](https://github.com/i18next/i18next-cli/issues/70)
|
|
11
|
+
|
|
12
|
+
## [1.11.12](https://github.com/i18next/i18next-cli/compare/v1.11.11...v1.11.12) - 2025-10-21
|
|
13
|
+
|
|
14
|
+
- further improve jsx-parser [#66](https://github.com/i18next/i18next-cli/issues/66)
|
|
15
|
+
|
|
8
16
|
## [1.11.11](https://github.com/i18next/i18next-cli/compare/v1.11.10...v1.11.11) - 2025-10-20
|
|
9
17
|
|
|
10
18
|
- Expose TranslationResult type for working with afterSync plugin / hook [#69](https://github.com/i18next/i18next-cli/issues/69)
|
package/dist/cjs/cli.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";var e=require("commander"),t=require("chokidar"),n=require("
|
|
2
|
+
"use strict";var e=require("commander"),t=require("chokidar"),n=require("chalk"),o=require("./config.js"),i=require("./heuristic-config.js"),r=require("./extractor/core/extractor.js");require("glob"),require("node:path"),require("node:fs/promises"),require("jiti");var a=require("./types-generator.js"),c=require("./syncer.js"),s=require("./migrator.js"),l=require("./init.js"),u=require("./linter.js"),g=require("./status.js"),d=require("./locize.js");const p=new e.Command;p.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.11.13"),p.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async e=>{try{const n=await o.ensureConfig(),i=async()=>{const t=await r.runExtractor(n,{isWatchMode:!!e.watch,isDryRun:!!e.dryRun,syncPrimaryWithDefaults:!!e.syncPrimary});return e.ci&&!t?(console.log("✅ No files were updated."),process.exit(0)):e.ci&&t&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),t};if(await i(),e.watch){console.log("\nWatching for changes...");const e=[/node_modules/,...Array.isArray(n.extract.ignore)?n.extract.ignore:n.extract.ignore?[n.extract.ignore]:[]],o=n.extract.input||[];t.watch(o,{ignored:e,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),i()})}}catch(e){console.error("Error running extractor:",e),process.exit(1)}}),p.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(e,t)=>{let r=await o.loadConfig();if(!r){console.log(n.blue("No config file found. Attempting to detect project structure..."));const e=await i.detectConfig();e||(console.error(n.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${n.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(n.green("Project structure detected successfully!")),r=e}await g.runStatus(r,{detail:e,namespace:t.namespace})}),p.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async e=>{const n=await o.ensureConfig(),i=()=>a.runTypesGenerator(n);if(await i(),e.watch){console.log("\nWatching for changes...");const e=[/node_modules/,...Array.isArray(n.extract?.ignore)?n.extract.ignore:n.extract?.ignore?[n.extract?.ignore]:[]],o=n.types?.input||[];t.watch(o,{ignored:e,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),i()})}}),p.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=await o.ensureConfig();await c.runSyncer(e)}),p.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async e=>{await s.runMigrator(e)}),p.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(l.runInit),p.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async e=>{const r=async()=>{let e=await o.loadConfig();if(!e){console.log(n.blue("No config file found. Attempting to detect project structure..."));const t=await i.detectConfig();t||(console.error(n.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${n.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(n.green("Project structure detected successfully!")),e=t}await u.runLinter(e)};if(await r(),e.watch){console.log("\nWatching for changes...");const e=await o.loadConfig();if(e?.extract?.input){const n=[/node_modules/,...Array.isArray(e.extract.ignore)?e.extract.ignore:e.extract.ignore?[e.extract.ignore]:[]],o=e.extract.input||[];t.watch(o,{ignored:n,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),r()})}}}),p.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async e=>{const t=await o.ensureConfig();await d.runLocizeSync(t,e)}),p.command("locize-download").description("Download all translations from your locize project.").action(async e=>{const t=await o.ensureConfig();await d.runLocizeDownload(t,e)}),p.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async e=>{const t=await o.ensureConfig();await d.runLocizeMigrate(t,e)}),p.parse(process.argv);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("./ast-utils.js");exports.extractFromTransComponent=function(t,n){const i=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),r=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),s=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),p=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let o;s||"JSXAttribute"!==p?.type||"JSXExpressionContainer"!==p.value?.type||"ObjectExpression"!==p.value.expression.type||(o=e.getObjectProperty(p.value.expression,"count"));const l=!!s||!!o,a=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),u="JSXAttribute"===a?.type&&"JSXExpressionContainer"===a.value?.type&&"ObjectExpression"===a.value.expression.type?a.value.expression:void 0,y=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),f=!!y,c=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let S="JSXAttribute"===c?.type&&"JSXExpressionContainer"===c.value?.type?c.value.expression:"JSXAttribute"===c?.type&&"StringLiteral"===c.value?.type?c.value:void 0;const
|
|
1
|
+
"use strict";var e=require("./ast-utils.js");exports.extractFromTransComponent=function(t,n){const i=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),r=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),s=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),p=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let o;s||"JSXAttribute"!==p?.type||"JSXExpressionContainer"!==p.value?.type||"ObjectExpression"!==p.value.expression.type||(o=e.getObjectProperty(p.value.expression,"count"));const l=!!s||!!o,a=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),u="JSXAttribute"===a?.type&&"JSXExpressionContainer"===a.value?.type&&"ObjectExpression"===a.value.expression.type?a.value.expression:void 0,y=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),f=!!y,c=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let S="JSXAttribute"===c?.type&&"JSXExpressionContainer"===c.value?.type?c.value.expression:"JSXAttribute"===c?.type&&"StringLiteral"===c.value?.type?c.value:void 0;const g=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let v;if(v="JSXAttribute"===g?.type&&"StringLiteral"===g.value?.type?g.value.value:void 0,u&&(void 0===v&&(v=e.getObjectPropValue(u,"ns")),void 0===S)){const t=e.getObjectProperty(u,"context");t?.value&&(S=t.value)}const x=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),i=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(e,t,s=!1){if(!e||!e.length)return;let p=0,o=e.length-1;for(;p<=o&&i(e[p]);)p++;for(;o>=p&&i(e[o]);)o--;const l=p<=o?e.slice(p,o+1):[],a=l.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<l.length;e++){const p=l[e];if(p)if("JSXText"!==p.type)if("JSXExpressionContainer"!==p.type){if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&n.has(e)){!(!p.opening||!p.opening.selfClosing)&&t.push(p),r(p.children||[],t,!1)}else t.push(p),r(p.children||[],t,!0);continue}"JSXFragment"!==p.type||r(p.children||[],t,s)}else{if(s&&!a&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}if("StringLiteral"===e){const e=String(p.expression.value||"");if(!(/^\s*$/.test(e)&&!e.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}if(p.expression&&"StringLiteral"===p.expression.type){const n=String(p.expression.value||""),r=/^\s*$/.test(n)&&!n.includes("\n"),s=l[e-1],o=l[e+1];if(r){const n=l[e+2];if(o&&"JSXText"===o.type&&i(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=l[e-1],n=l[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue;const r=!o||"JSXText"===o.type&&!i(o);if(s&&"JSXText"===s.type&&r){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue}}t.push(p)}else{if(s&&!a)continue;if(s&&i(p))continue;if(i(p)){const n=l[e-1],i=l[e+1];if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const r=t[t.length-1],s=l[e-1];if(r){if(s&&"JSXExpressionContainer"===s.type)continue;if("JSXText"===r.type&&s&&"JSXText"===s.type){r.value=String(r.value)+p.value;continue}}}if(s&&a&&0===e)continue;t.push(p)}}}const s=[];r(e,s,!1);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function o(e,t){if(!e||0===e.length)return"";let r="",l=!1;for(let a=0;a<e.length;a++){const u=e[a];if(u)if("JSXText"!==u.type){if("JSXExpressionContainer"===u.type){const e=u.expression;if(!e)continue;if("StringLiteral"===e.type)r+=e.value;else if("Identifier"===e.type)r+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?r+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?r+=`{{${t.value}}}`:r+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?r+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?r+=`{{${e.callee.value}}}`:r+="{{value}}";l=!1;continue}if("JSXElement"===u.type){let i;if(u.opening&&u.opening.name&&"Identifier"===u.opening.name.type&&(i=u.opening.name.value),i&&n.has(i)){const n=o(u.children||[],t),s=!(!u.opening||!u.opening.selfClosing),p=""!==String(n).trim();if(s||!p){const t=e[a-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(r=r.replace(/\s+$/,"")),r+=`<${i}/>`,l=!0}else r+=`<${i}>${n}</${i}>`,l=!1}else{const e=u.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==s.indexOf(e))){const t=s.indexOf(u),n=o(e,void 0);r+=`<${t}>${p(n)}</${t}>`,l=!1}else{const i=new Map;let a=0;for(const t of e)if(t&&"JSXElement"===t.type){const e=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(e&&n.has(e)){!(!t.opening||!t.opening.selfClosing)&&i.set(t,a++)}else i.set(t,a++)}const y=t&&t.has(u)?t.get(u):s.indexOf(u),f=o(e,i.size?i:void 0);r+=`<${y}>${p(f)}</${y}>`,l=!1}}continue}"JSXFragment"!==u.type||(r+=o(u.children||[]),l=!1)}else{if(i(u))continue;l?(r+=u.value.replace(/^\s+/,""),l=!1):r+=u.value}}return r}const l=o(e);return String(l).replace(/\s+/g," ").trim()}(t.children,n);let d,J,X;if("JSXAttribute"===r?.type&&"StringLiteral"===r.value?.type)d=r.value.value;else{const e=n.extract.defaultValue;d="string"==typeof e?e:""}if("JSXAttribute"===i?.type){if("StringLiteral"===i.value?.type){if(J=i.value,X=J.value,!X||""===X.trim())return null;if(v&&"StringLiteral"===J.type){const e=n.extract.nsSeparator??":",t=J.value;if(e&&t.startsWith(`${v}${e}`)){if(X=t.slice(`${v}${e}`.length),!X||""===X.trim())return null;J={...J,value:X}}}}else"JSXExpressionContainer"===i.value?.type&&"JSXEmptyExpression"!==i.value.expression.type&&(J=i.value.expression);if(!J)return null}return r||!X||x.trim()?!r&&x.trim()&&(d=x):d=X,{keyExpression:J,serializedChildren:x,ns:v,defaultValue:d,hasCount:l,isOrdinal:f,contextExpression:S,optionsNode:u,explicitDefault:Boolean(r&&"JSXAttribute"===r.type&&"StringLiteral"===r.value?.type||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(u))}};
|
package/dist/esm/cli.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{Command as t}from"commander";import e from"chokidar";import
|
|
2
|
+
import{Command as t}from"commander";import e from"chokidar";import o from"chalk";import{ensureConfig as n,loadConfig as i}from"./config.js";import{detectConfig as a}from"./heuristic-config.js";import{runExtractor as r}from"./extractor/core/extractor.js";import"glob";import"node:path";import"node:fs/promises";import"jiti";import{runTypesGenerator as c}from"./types-generator.js";import{runSyncer as s}from"./syncer.js";import{runMigrator as l}from"./migrator.js";import{runInit as p}from"./init.js";import{runLinter as d}from"./linter.js";import{runStatus as m}from"./status.js";import{runLocizeSync as g,runLocizeDownload as u,runLocizeMigrate as f}from"./locize.js";const y=new t;y.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.11.13"),y.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async t=>{try{const o=await n(),i=async()=>{const e=await r(o,{isWatchMode:!!t.watch,isDryRun:!!t.dryRun,syncPrimaryWithDefaults:!!t.syncPrimary});return t.ci&&!e?(console.log("✅ No files were updated."),process.exit(0)):t.ci&&e&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),e};if(await i(),t.watch){console.log("\nWatching for changes...");const t=[/node_modules/,...Array.isArray(o.extract.ignore)?o.extract.ignore:o.extract.ignore?[o.extract.ignore]:[]],n=o.extract.input||[];e.watch(n,{ignored:t,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),i()})}}catch(t){console.error("Error running extractor:",t),process.exit(1)}}),y.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(t,e)=>{let n=await i();if(!n){console.log(o.blue("No config file found. Attempting to detect project structure..."));const t=await a();t||(console.error(o.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${o.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(o.green("Project structure detected successfully!")),n=t}await m(n,{detail:t,namespace:e.namespace})}),y.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async t=>{const o=await n(),i=()=>c(o);if(await i(),t.watch){console.log("\nWatching for changes...");const t=[/node_modules/,...Array.isArray(o.extract?.ignore)?o.extract.ignore:o.extract?.ignore?[o.extract?.ignore]:[]],n=o.types?.input||[];e.watch(n,{ignored:t,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),i()})}}),y.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const t=await n();await s(t)}),y.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async t=>{await l(t)}),y.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(p),y.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async t=>{const n=async()=>{let t=await i();if(!t){console.log(o.blue("No config file found. Attempting to detect project structure..."));const e=await a();e||(console.error(o.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${o.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(o.green("Project structure detected successfully!")),t=e}await d(t)};if(await n(),t.watch){console.log("\nWatching for changes...");const t=await i();if(t?.extract?.input){const o=[/node_modules/,...Array.isArray(t.extract.ignore)?t.extract.ignore:t.extract.ignore?[t.extract.ignore]:[]],i=t.extract.input||[];e.watch(i,{ignored:o,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),n()})}}}),y.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async t=>{const e=await n();await g(e,t)}),y.command("locize-download").description("Download all translations from your locize project.").action(async t=>{const e=await n();await u(e,t)}),y.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async t=>{const e=await n();await f(e,t)}),y.parse(process.argv);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getObjectProperty as e,getObjectPropValue as t}from"./ast-utils.js";function n(n,i){const r=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),s=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),o=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let l;p||"JSXAttribute"!==o?.type||"JSXExpressionContainer"!==o.value?.type||"ObjectExpression"!==o.value.expression.type||(l=e(o.value.expression,"count"));const a=!!p||!!l,u=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),y="JSXAttribute"===u?.type&&"JSXExpressionContainer"===u.value?.type&&"ObjectExpression"===u.value.expression.type?u.value.expression:void 0,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),c=!!f,S=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===S?.type&&"JSXExpressionContainer"===S.value?.type?S.value.expression:"JSXAttribute"===S?.type&&"StringLiteral"===S.value?.type?S.value:void 0;const g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let x;if(x="JSXAttribute"===g?.type&&"StringLiteral"===g.value?.type?g.value.value:void 0,y&&(void 0===x&&(x=t(y,"ns")),void 0===v)){const t=e(y,"context");t?.value&&(v=t.value)}const d=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),i=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(e,t,s=!1){if(!e||!e.length)return;let p=0,o=e.length-1;for(;p<=o&&i(e[p]);)p++;for(;o>=p&&i(e[o]);)o--;const l=p<=o?e.slice(p,o+1):[],a=l.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<l.length;e++){const p=l[e];if(p)if("JSXText"!==p.type)if("JSXExpressionContainer"!==p.type){if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&n.has(e)){!(!p.opening||!p.opening.selfClosing)&&t.push(p),r(p.children||[],t,!1)}else t.push(p),r(p.children||[],t,!0);continue}"JSXFragment"!==p.type||r(p.children||[],t,s)}else{if(s&&!a&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}if("StringLiteral"===e){const e=String(p.expression.value||"");if(!(/^\s*$/.test(e)&&!e.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}if(p.expression&&"StringLiteral"===p.expression.type){const n=String(p.expression.value||""),r=/^\s*$/.test(n)&&!n.includes("\n"),s=l[e-1],o=l[e+1];if(r){const n=l[e+2];if(o&&"JSXText"===o.type&&i(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=l[e-1],n=l[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue;const r=!o||"JSXText"===o.type&&!i(o);if(s&&"JSXText"===s.type&&r){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue}}t.push(p)}else{if(s&&!a)continue;if(s&&i(p))continue;if(i(p)){const n=t[t.length-1],
|
|
1
|
+
import{getObjectProperty as e,getObjectPropValue as t}from"./ast-utils.js";function n(n,i){const r=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),s=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),o=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let l;p||"JSXAttribute"!==o?.type||"JSXExpressionContainer"!==o.value?.type||"ObjectExpression"!==o.value.expression.type||(l=e(o.value.expression,"count"));const a=!!p||!!l,u=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),y="JSXAttribute"===u?.type&&"JSXExpressionContainer"===u.value?.type&&"ObjectExpression"===u.value.expression.type?u.value.expression:void 0,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),c=!!f,S=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===S?.type&&"JSXExpressionContainer"===S.value?.type?S.value.expression:"JSXAttribute"===S?.type&&"StringLiteral"===S.value?.type?S.value:void 0;const g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let x;if(x="JSXAttribute"===g?.type&&"StringLiteral"===g.value?.type?g.value.value:void 0,y&&(void 0===x&&(x=t(y,"ns")),void 0===v)){const t=e(y,"context");t?.value&&(v=t.value)}const d=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),i=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(e,t,s=!1){if(!e||!e.length)return;let p=0,o=e.length-1;for(;p<=o&&i(e[p]);)p++;for(;o>=p&&i(e[o]);)o--;const l=p<=o?e.slice(p,o+1):[],a=l.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<l.length;e++){const p=l[e];if(p)if("JSXText"!==p.type)if("JSXExpressionContainer"!==p.type){if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&n.has(e)){!(!p.opening||!p.opening.selfClosing)&&t.push(p),r(p.children||[],t,!1)}else t.push(p),r(p.children||[],t,!0);continue}"JSXFragment"!==p.type||r(p.children||[],t,s)}else{if(s&&!a&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}if("StringLiteral"===e){const e=String(p.expression.value||"");if(!(/^\s*$/.test(e)&&!e.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}if(p.expression&&"StringLiteral"===p.expression.type){const n=String(p.expression.value||""),r=/^\s*$/.test(n)&&!n.includes("\n"),s=l[e-1],o=l[e+1];if(r){const n=l[e+2];if(o&&"JSXText"===o.type&&i(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=l[e-1],n=l[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue;const r=!o||"JSXText"===o.type&&!i(o);if(s&&"JSXText"===s.type&&r){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue}}t.push(p)}else{if(s&&!a)continue;if(s&&i(p))continue;if(i(p)){const n=l[e-1],i=l[e+1];if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const r=t[t.length-1],s=l[e-1];if(r){if(s&&"JSXExpressionContainer"===s.type)continue;if("JSXText"===r.type&&s&&"JSXText"===s.type){r.value=String(r.value)+p.value;continue}}}if(s&&a&&0===e)continue;t.push(p)}}}const s=[];r(e,s,!1);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function o(e,t){if(!e||0===e.length)return"";let r="",l=!1;for(let a=0;a<e.length;a++){const u=e[a];if(u)if("JSXText"!==u.type){if("JSXExpressionContainer"===u.type){const e=u.expression;if(!e)continue;if("StringLiteral"===e.type)r+=e.value;else if("Identifier"===e.type)r+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?r+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?r+=`{{${t.value}}}`:r+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?r+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?r+=`{{${e.callee.value}}}`:r+="{{value}}";l=!1;continue}if("JSXElement"===u.type){let i;if(u.opening&&u.opening.name&&"Identifier"===u.opening.name.type&&(i=u.opening.name.value),i&&n.has(i)){const n=o(u.children||[],t),s=!(!u.opening||!u.opening.selfClosing),p=""!==String(n).trim();if(s||!p){const t=e[a-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(r=r.replace(/\s+$/,"")),r+=`<${i}/>`,l=!0}else r+=`<${i}>${n}</${i}>`,l=!1}else{const e=u.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==s.indexOf(e))){const t=s.indexOf(u),n=o(e,void 0);r+=`<${t}>${p(n)}</${t}>`,l=!1}else{const i=new Map;let a=0;for(const t of e)if(t&&"JSXElement"===t.type){const e=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(e&&n.has(e)){!(!t.opening||!t.opening.selfClosing)&&i.set(t,a++)}else i.set(t,a++)}const y=t&&t.has(u)?t.get(u):s.indexOf(u),f=o(e,i.size?i:void 0);r+=`<${y}>${p(f)}</${y}>`,l=!1}}continue}"JSXFragment"!==u.type||(r+=o(u.children||[]),l=!1)}else{if(i(u))continue;l?(r+=u.value.replace(/^\s+/,""),l=!1):r+=u.value}}return r}const l=o(e);return String(l).replace(/\s+/g," ").trim()}(n.children,i);let J,X,m;if("JSXAttribute"===s?.type&&"StringLiteral"===s.value?.type)J=s.value.value;else{const e=i.extract.defaultValue;J="string"==typeof e?e:""}if("JSXAttribute"===r?.type){if("StringLiteral"===r.value?.type){if(X=r.value,m=X.value,!m||""===m.trim())return null;if(x&&"StringLiteral"===X.type){const e=i.extract.nsSeparator??":",t=X.value;if(e&&t.startsWith(`${x}${e}`)){if(m=t.slice(`${x}${e}`.length),!m||""===m.trim())return null;X={...X,value:m}}}}else"JSXExpressionContainer"===r.value?.type&&"JSXEmptyExpression"!==r.value.expression.type&&(X=r.value.expression);if(!X)return null}s||!m||d.trim()?!s&&d.trim()&&(J=d):J=m;return{keyExpression:X,serializedChildren:d,ns:x,defaultValue:J,hasCount:a,isOrdinal:c,contextExpression:v,optionsNode:y,explicitDefault:Boolean(s&&"JSXAttribute"===s.type&&"StringLiteral"===s.value?.type||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(y))}}export{n as extractFromTransComponent};
|
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
import { Command } from 'commander'
|
|
4
4
|
import chokidar from 'chokidar'
|
|
5
|
-
import { glob } from 'glob'
|
|
6
5
|
import chalk from 'chalk'
|
|
7
6
|
import { loadConfig, ensureConfig } from './config'
|
|
8
7
|
import { detectConfig } from './heuristic-config'
|
|
@@ -21,7 +20,7 @@ const program = new Command()
|
|
|
21
20
|
program
|
|
22
21
|
.name('i18next-cli')
|
|
23
22
|
.description('A unified, high-performance i18next CLI.')
|
|
24
|
-
.version('1.11.
|
|
23
|
+
.version('1.11.13')
|
|
25
24
|
|
|
26
25
|
program
|
|
27
26
|
.command('extract')
|
|
@@ -58,8 +57,14 @@ program
|
|
|
58
57
|
// If in watch mode, set up the chokidar watcher
|
|
59
58
|
if (options.watch) {
|
|
60
59
|
console.log('\nWatching for changes...')
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
// Respect configured ignore patterns and pass the original glob(s) to chokidar
|
|
61
|
+
const ignoredPatterns = [
|
|
62
|
+
/node_modules/,
|
|
63
|
+
...(Array.isArray(config.extract.ignore) ? config.extract.ignore : (config.extract.ignore ? [config.extract.ignore] : [])),
|
|
64
|
+
]
|
|
65
|
+
const watchTargets = config.extract.input || []
|
|
66
|
+
const watcher = chokidar.watch(watchTargets, {
|
|
67
|
+
ignored: ignoredPatterns,
|
|
63
68
|
persistent: true,
|
|
64
69
|
})
|
|
65
70
|
watcher.on('change', path => {
|
|
@@ -105,7 +110,14 @@ program
|
|
|
105
110
|
|
|
106
111
|
if (options.watch) {
|
|
107
112
|
console.log('\nWatching for changes...')
|
|
108
|
-
|
|
113
|
+
// Use the configured input patterns and respect extract.ignore if present
|
|
114
|
+
const ignoredPatterns = [
|
|
115
|
+
/node_modules/,
|
|
116
|
+
...(Array.isArray(config.extract?.ignore) ? config.extract.ignore : (config.extract?.ignore ? [config.extract?.ignore] : [])),
|
|
117
|
+
]
|
|
118
|
+
const watchTargets = config.types?.input || []
|
|
119
|
+
const watcher = chokidar.watch(watchTargets, {
|
|
120
|
+
ignored: ignoredPatterns,
|
|
109
121
|
persistent: true,
|
|
110
122
|
})
|
|
111
123
|
watcher.on('change', path => {
|
|
@@ -166,8 +178,13 @@ program
|
|
|
166
178
|
// Re-load the config to get the correct input paths for the watcher
|
|
167
179
|
const config = await loadConfig()
|
|
168
180
|
if (config?.extract?.input) {
|
|
169
|
-
const
|
|
170
|
-
|
|
181
|
+
const ignoredPatterns = [
|
|
182
|
+
/node_modules/,
|
|
183
|
+
...(Array.isArray(config.extract.ignore) ? config.extract.ignore : (config.extract.ignore ? [config.extract.ignore] : [])),
|
|
184
|
+
]
|
|
185
|
+
const watchTargets = config.extract.input || []
|
|
186
|
+
const watcher = chokidar.watch(watchTargets, {
|
|
187
|
+
ignored: ignoredPatterns,
|
|
171
188
|
persistent: true,
|
|
172
189
|
})
|
|
173
190
|
watcher.on('change', path => {
|
|
@@ -323,6 +323,20 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
323
323
|
|
|
324
324
|
// Otherwise, preserve previous behavior for boundary/formatting merging.
|
|
325
325
|
if (isFormattingWhitespace(n)) {
|
|
326
|
+
// If this formatting whitespace sits between two element/fragment nodes
|
|
327
|
+
// (e.g. <br />\n <small>...), treat it as layout-only and skip it so it
|
|
328
|
+
// doesn't become its own slot and shift subsequent element indexes.
|
|
329
|
+
const prevOrig = meaningfulNodes[i - 1]
|
|
330
|
+
const nextOrig = meaningfulNodes[i + 1]
|
|
331
|
+
if (
|
|
332
|
+
prevOrig &&
|
|
333
|
+
(prevOrig.type === 'JSXElement' || prevOrig.type === 'JSXFragment') &&
|
|
334
|
+
nextOrig &&
|
|
335
|
+
(nextOrig.type === 'JSXElement' || nextOrig.type === 'JSXFragment')
|
|
336
|
+
) {
|
|
337
|
+
continue
|
|
338
|
+
}
|
|
339
|
+
|
|
326
340
|
const prevSlot = slots[slots.length - 1]
|
|
327
341
|
const prevOriginal = meaningfulNodes[i - 1]
|
|
328
342
|
|
|
@@ -504,7 +518,16 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
504
518
|
const globalSlots: any[] = []
|
|
505
519
|
collectSlots(children, globalSlots, false)
|
|
506
520
|
|
|
507
|
-
|
|
521
|
+
// Trim only newline-only indentation at the edges of serialized inner text.
|
|
522
|
+
// This preserves single leading/trailing spaces which are meaningful between inline placeholders.
|
|
523
|
+
const trimFormattingEdges = (s: string) =>
|
|
524
|
+
String(s)
|
|
525
|
+
// remove leading newline-only indentation
|
|
526
|
+
.replace(/^\s*\n\s*/g, '')
|
|
527
|
+
// remove trailing newline-only indentation
|
|
528
|
+
.replace(/\s*\n\s*$/g, '')
|
|
529
|
+
|
|
530
|
+
function visitNodes (nodes: any[], localIndexMap?: Map<any, number>): string {
|
|
508
531
|
if (!nodes || nodes.length === 0) return ''
|
|
509
532
|
let out = ''
|
|
510
533
|
let lastWasSelfClosing = false
|
|
@@ -559,7 +582,7 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
559
582
|
}
|
|
560
583
|
|
|
561
584
|
if (tag && allowedTags.has(tag)) {
|
|
562
|
-
const inner = visitNodes(node.children || [])
|
|
585
|
+
const inner = visitNodes(node.children || [], localIndexMap)
|
|
563
586
|
// consider element self-closing for rendering when AST marks it so or it has no meaningful children
|
|
564
587
|
const isAstSelfClosing = !!(node.opening && (node.opening as any).selfClosing)
|
|
565
588
|
const hasMeaningfulChildren = String(inner).trim() !== ''
|
|
@@ -579,14 +602,49 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
579
602
|
lastWasSelfClosing = false
|
|
580
603
|
}
|
|
581
604
|
} else {
|
|
582
|
-
//
|
|
583
|
-
//
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
//
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
605
|
+
// Decide whether to use local (restarted) indexes for this element's
|
|
606
|
+
// immediate children or fall back to the global pre-order indexes.
|
|
607
|
+
// If the element's children contain non-element slots (JSXText /
|
|
608
|
+
// JSXExpressionContainer) that were collected as global slots, we must
|
|
609
|
+
// use the global indexes so those text slots keep their positions.
|
|
610
|
+
const children = node.children || []
|
|
611
|
+
const hasNonElementGlobalSlots = children.some((ch: any) =>
|
|
612
|
+
ch && (ch.type === 'JSXText' || ch.type === 'JSXExpressionContainer') && globalSlots.indexOf(ch) !== -1
|
|
613
|
+
)
|
|
614
|
+
|
|
615
|
+
// If there are non-element global slots among the children, render using
|
|
616
|
+
// global indexes. Otherwise build a compact local index map so nested
|
|
617
|
+
// placeholders restart at 0 inside this parent element.
|
|
618
|
+
if (hasNonElementGlobalSlots) {
|
|
619
|
+
const idx = globalSlots.indexOf(node)
|
|
620
|
+
const inner = visitNodes(children, undefined)
|
|
621
|
+
out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
|
|
622
|
+
lastWasSelfClosing = false
|
|
623
|
+
} else {
|
|
624
|
+
const childrenLocalMap = new Map<any, number>()
|
|
625
|
+
let localIdxCounter = 0
|
|
626
|
+
for (const ch of children) {
|
|
627
|
+
if (!ch) continue
|
|
628
|
+
if (ch.type === 'JSXElement') {
|
|
629
|
+
const chTag = ch.opening && ch.opening.name && ch.opening.name.type === 'Identifier'
|
|
630
|
+
? ch.opening.name.value
|
|
631
|
+
: undefined
|
|
632
|
+
if (chTag && allowedTags.has(chTag)) {
|
|
633
|
+
const isChSelfClosing = !!(ch.opening && (ch.opening as any).selfClosing)
|
|
634
|
+
if (isChSelfClosing) {
|
|
635
|
+
childrenLocalMap.set(ch, localIdxCounter++)
|
|
636
|
+
}
|
|
637
|
+
} else {
|
|
638
|
+
childrenLocalMap.set(ch, localIdxCounter++)
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
const idx = localIndexMap && localIndexMap.has(node) ? localIndexMap.get(node) : globalSlots.indexOf(node)
|
|
644
|
+
const inner = visitNodes(children, childrenLocalMap.size ? childrenLocalMap : undefined)
|
|
645
|
+
out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
|
|
646
|
+
lastWasSelfClosing = false
|
|
647
|
+
}
|
|
590
648
|
}
|
|
591
649
|
continue
|
|
592
650
|
}
|