Repository navigation
perf(i18n): every core component renders with its own English slice #7358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5e4c993
dcb9666
3a4142e
0ac8edf
7df8e75
e3f5f14
58003b9
17e3421
db49087
80d6ebf
d99cf19
d359e1b
011b8ae
33cfa85
ed99b8d
fab7ed1
627acf0
c44090d
25a4e58
88c4340
b0588ff
df7c3f5
8da012c
0de5c8e
f9267e0
8500a50
7833176
d09aa31
3be050f
6aafdbc
d576f18
df5d915
c904b68
12980ed
fa9550d
fa9e9e5
f4f1f69
37c215b
9d495a7
f96ddb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@astryxdesign/cli': patch | ||
| --- | ||
|
|
||
| [docs] The i18n, RTL and testing guide keeps to what holds for a caller: `useTranslator()` resolves every `@astryx.*` key; how a core component reads its strings moves to the contributing guide. | ||
|
|
||
| @vjeux |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@astryxdesign/core': patch | ||
| '@astryxdesign/cli': patch | ||
| --- | ||
|
|
||
| [perf] A component carries only its own English strings. Every core component now renders with its own namespace slice of the English catalog instead of the whole catalog, so an app that renders components without mounting `InternationalizationProvider`, calling `useTranslator()` or importing `InternationalizationContext` (each of which carries the whole catalog), and that does not render `PowerSearch` (whose caller-supplied operator keys keep the whole catalog as their fallback), bundles only the strings of the components it renders: a page importing `Button`, `Dialog`, `DateInput` or `List` loads 6.2 to 6.8 KB (gzip) less JavaScript. An app that does mount the provider still carries the whole catalog, now assembled from the same slices so no string ships twice; such a page loads 1.2 to 1.9 KB (gzip) more, because the catalog spread over its slice modules compresses worse than one module (about 0.9 KB more when the provider is imported before the components). `useTranslator()` is unchanged. `astryx swizzle` points an ejected component's strings at the public `useTranslator()`: the internal translator import, the slice imports and a module-level merge of slices are rewritten, so the copy compiles in the app. | ||
|
|
||
| @vjeux |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@astryxdesign/cli': patch | ||
| --- | ||
|
|
||
| [feat] `astryx upgrade` runs `migrate-internationalization-context`, which rewrites `<InternationalizationContext value={…}>` (or its `.Provider`) to `<InternationalizationProvider locale dir messages overrides>`: a variable maps `locale`, `direction` (as `dir`) and `messages` — the fields every context value has — and its TODO names `overrides` and `translate`, which the variable's type may lack and the provider has no input for; an inline object literal maps key by key, `overrides` included, with a TODO for `translate` (a literal's `translate` key is kept beside it). Any other value — a call, a spread into a literal, a conditional — is left unchanged behind a TODO saying why. It rewrites JSX whose name resolves, unshadowed, to an Astryx import (named, aliased, or namespace), reuses the file's own provider import name, and leaves a reader of the context alone. | ||
|
|
||
| @vjeux | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@astryxdesign/core': minor | ||
| --- | ||
|
|
||
| [breaking] An app that sets `InternationalizationContext` directly — rendering `<InternationalizationContext value={…}>` (or its `.Provider`) instead of `InternationalizationProvider` — no longer configures Astryx: it drives neither `useLocale`, `useDirection` and `useCollator`, nor the components that read the locale or direction through them (the date, date-time, date-range and number inputs, Calendar, Chat, PowerSearch, Table sorting and filtering, Timestamp), nor any core component's strings — every component now renders with its own English slice through an internal translator, and `DateTimeInput`'s touch field reads `useLocale()`. One deliberate exception: a `PowerSearch` operator `i18nKey` from outside PowerSearch's own namespace (a key of another Astryx component, or of the app's catalog) still resolves through the public context's `translate`, since a caller may supply any key there; the shipped default operators are PowerSearch's own and read the slice like the rest of the component. Migration: mount `InternationalizationProvider`; its `locale`, `messages` and `overrides` take the value's fields of the same name, and its `dir` takes the value's `direction`. The provider has no `translate` input: an app that supplied its own `translate` expresses those strings as `messages` (a catalog per locale) or `overrides` (sparse, per locale) and the provider resolves them in the documented order. Run `astryx upgrade`: its `migrate-internationalization-context` transform rewrites a variable value to the provider's `locale`, `dir` and `messages` with a TODO for its `overrides` and `translate`, and an inline object literal key by key with a TODO for `translate`; any other value, and any element that spreads props, is left as written behind a TODO saying why. The provider now writes an internal runtime context that those hooks read. Reading the public context is unchanged, so a component that reads it with `useContext` and re-mounts `InternationalizationProvider` with a derived value keeps working: `locale`, `direction`, `messages`, `overrides` and `translate`, which still resolves every `@astryx.*` key against the whole shipped English catalog, with or without a provider; `useTranslator()` is unchanged. The English catalog is also emitted one namespace at a time, the resolver takes its English fallback per call, and an internal `useComponentTranslator(slice)` is what a core component renders with. The slices are not importable. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, there is nothing to change in the entry itself. It stays refused until an engineering owner rules on the raw-context retirement and the release owner declares 0.8.0 on main. The description's Authority section names both decisions. Checked at 144793f. |
||
|
|
||
| @vjeux | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@astryxdesign/build': patch | ||
| --- | ||
|
|
||
| [fix] `astryxStylex()`'s alias for `@astryxdesign/core/locales/<locale>.generated.js` takes one path segment, so the per-namespace English slices under `locales/en/` — which the package's exports map keeps out of reach — no longer resolve through the source tree in a Vite app either. | ||
|
|
||
| @vjeux |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,11 @@ | |
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
| import {resolveComponentReplacement, resolveCore} from '../_adapter.mjs'; | ||
| import {assertWithin, sanitizeName, PathSafetyError} from '../../../foundation/fs/path-safety.mjs'; | ||
| import { | ||
| assertWithin, | ||
| sanitizeName, | ||
| PathSafetyError, | ||
| } from '../../../foundation/fs/path-safety.mjs'; | ||
| import {checkGhCli} from '../_github.mjs'; | ||
| import {Project} from '../../../foundation/config/project.mjs'; | ||
| import { | ||
|
|
@@ -32,6 +36,81 @@ import {AstryxError, writeFailed} from '../../error.mjs'; | |
| /** Default issue tracker for maintainer feedback after swizzling. */ | ||
| const DEFAULT_ISSUES_URL = 'https://github.com/facebook/astryx/issues/new'; | ||
|
|
||
| /** | ||
| * A core component reads its strings with the internal | ||
| * `useComponentTranslator(<its English slice>)`; neither the hook nor the | ||
| * slices are public (the slices are `null` in the exports map). An ejected | ||
| * copy lives in the app, whose public translator is `useTranslator()` — it | ||
| * resolves every `@astryx.*` key against the whole English catalog — so the | ||
| * copy reads through that instead: the hook import becomes the public one, | ||
| * the slice imports and a module-level merge of slices go, and each call | ||
| * becomes `useTranslator()`. A type-only `TranslatorFn` import rides along. | ||
| * The internal `useCatalogTranslator` (caller-supplied keys) is the public | ||
| * `useTranslator` under another name, so its import becomes an alias of it. | ||
| * | ||
| * @param {string} content | ||
| * @param {string} ownerPackage | ||
| */ | ||
| export function rewriteComponentTranslator(content, ownerPackage) { | ||
| if ( | ||
| !content.includes('useComponentTranslator') && | ||
| !content.includes('useCatalogTranslator') | ||
| ) { | ||
| return content; | ||
| } | ||
| const i18n = `${ownerPackage}/i18n`; | ||
| return ( | ||
| content | ||
| // import {useCatalogTranslator} from '../i18n/useCatalogTranslator'; | ||
| // The internal whole-catalog translator for caller-supplied keys is the | ||
| // public `useTranslator` under another name; the alias keeps both call | ||
| // sites (`t` and `tCaller`) as written. | ||
| .replace( | ||
| /import\s*\{([^}]*)\}\s*from\s+['"](?:\.\.\/)+i18n\/useCatalogTranslator['"];?/g, | ||
| (_m, names) => { | ||
| const kept = names | ||
| .split(',') | ||
| .map((/** @type {string} */ n) => n.trim()) | ||
| .filter(Boolean) | ||
| .map((/** @type {string} */ n) => | ||
| n === 'useCatalogTranslator' | ||
| ? 'useTranslator as useCatalogTranslator' | ||
| : n, | ||
| ); | ||
| return `import {${kept.join(', ')}} from '${i18n}';`; | ||
| }, | ||
| ) | ||
| // import {useComponentTranslator[, type TranslatorFn]} from '../i18n/useComponentTranslator'; | ||
| .replace( | ||
| /import\s*\{([^}]*)\}\s*from\s+['"](?:\.\.\/)+i18n\/useComponentTranslator['"];?/g, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This handles
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed at this head (8a08a54): the copy step rewrites |
||
| (_m, names) => { | ||
| const kept = names | ||
| .split(',') | ||
| .map((/** @type {string} */ n) => n.trim()) | ||
| .filter(Boolean) | ||
| .map((/** @type {string} */ n) => | ||
| n === 'useComponentTranslator' ? 'useTranslator' : n, | ||
| ); | ||
| return `import {${kept.join(', ')}} from '${i18n}';`; | ||
| }, | ||
| ) | ||
| // import xMessages from '../i18n/generated-locales/en/x.generated'; | ||
| .replace( | ||
| /^import\s+\w+\s+from\s+['"](?:\.\.\/)+i18n\/generated-locales\/[^'"]+['"];?\r?\n/gm, | ||
| '', | ||
| ) | ||
| // const messages = {...aMessages, ...bMessages}; | ||
| .replace( | ||
| /^const\s+\w+\s*=\s*\{(?:\s*\.\.\.\w+Messages\s*,?)+\s*\};?\r?\n/gm, | ||
| '', | ||
| ) | ||
| // useComponentTranslator(xMessages) -> useTranslator() | ||
| .replace(/\buseComponentTranslator\(\s*[\w.]+\s*\)/g, 'useTranslator()') | ||
| // The file header's `@input … i18n (useComponentTranslator)` note. | ||
| .replace(/\buseComponentTranslator\b/g, 'useTranslator') | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Rewrite relative imports that point outside the component directory to use | ||
| * the OWNER package's subpaths. Imports within the copied directory (./x) are | ||
|
|
@@ -58,6 +137,7 @@ const DEFAULT_ISSUES_URL = 'https://github.com/facebook/astryx/issues/new'; | |
| * @param {string} [ownerPackage] | ||
| */ | ||
| export function rewriteImports(content, ownerPackage = CORE_PACKAGE) { | ||
| content = rewriteComponentTranslator(content, ownerPackage); | ||
| /** @param {string} importPath */ | ||
| const mapTarget = importPath => { | ||
| // Strip ALL leading `../` so a two-levels-up path never yields `<pkg>/..`. | ||
|
|
@@ -270,14 +350,20 @@ export async function swizzleCopy(component, options = {}) { | |
| if (!owner) { | ||
| throw new AstryxError( | ||
| `Component "${dirName}" is not provided by package "${pkg}".`, | ||
| allOwners.map(o => ({name: o.package, reason: 'provides this component'})), | ||
| allOwners.map(o => ({ | ||
| name: o.package, | ||
| reason: 'provides this component', | ||
| })), | ||
| ERROR_CODES.ERR_UNKNOWN_COMPONENT, | ||
| ); | ||
| } | ||
| } else if (allOwners.length > 1) { | ||
| throw new AstryxError( | ||
| `Component "${dirName}" is provided by multiple packages. Re-run with --package <pkg> to choose one.`, | ||
| allOwners.map(o => ({name: o.package, reason: 'provides this component'})), | ||
| allOwners.map(o => ({ | ||
| name: o.package, | ||
| reason: 'provides this component', | ||
| })), | ||
| ERROR_CODES.ERR_AMBIGUOUS_COMPONENT, | ||
| ); | ||
| } else { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
astryx upgradewould not run this transform at this revision:packages/cli/assets/codemods/transforms/next/index.mjsstill exports[], and release promotion copies that manifest into the version folder the upgrade registry loads. The original codemod commit added the manifest entry; the split into this stacked pull request dropped it. Importing the transform and itsmetathere, and exporting{name: 'migrate-internationalization-context', transform, meta}, with a test that the manifest lists it, makes this sentence true.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registered in #7969's bb41827 (this branch is rebased on it): the manifest exports the transform, with a test that it is listed.