Skip to content

Commit 37e6f1f

Browse files
committed
fix(i18n): keep the English namespace slices off the package's export surface
`./locales/*.generated.js` matched the slices under generated-locales/en/ too, so `@astryxdesign/core/locales/en/button.generated.js` resolved from a consumer after a build. The exports map now gives `./locales/en/*` a null target: the longer pattern wins, the whole catalogs stay importable, and the slices are the package's own. locale-exports.test.mjs resolves both through Node's package resolution from a workspace consumer and fails without the null target. The changeset names the one undocumented path that moves (a value handed straight to InternationalizationContext without the provider).
1 parent 2c23416 commit 37e6f1f

5 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎.changeset/i18n-en-namespace-slices.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
'@astryxdesign/core': patch
33
---
44

5-
[perf] A component carries only its own English strings. The shipped English catalog is now emitted one namespace at a time, and every core component renders with its own slice instead of the whole 408-key catalog, so an app bundles the strings of the components it renders: two rows of buttons and links drop about 5 KB (gzip) from their first load. `useTranslator()` is unchanged and still resolves every `@astryx.*` key; an `InternationalizationProvider`'s `messages` and `overrides` still win over the slice.
5+
[perf] A component carries only its own English strings. The shipped English catalog is now emitted one namespace at a time, and every core component renders with its own slice instead of the whole 408-key catalog, so an app bundles the strings of the components it renders: two rows of buttons and links drop about 5 KB (gzip) from their first load. `useTranslator()` and `InternationalizationContext` are unchanged and still resolve every `@astryx.*` key; an `InternationalizationProvider`'s `messages` and `overrides` still win over the slice; the slices themselves are not importable. One undocumented path moves: a value handed straight to `InternationalizationContext` without the provider no longer reaches core components or `useLocale`, `useDirection` and `useCollator`, which read the provider's runtime context; mount `InternationalizationProvider`.
66

77
@vjeux

‎packages/core/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
"types": "./dist/i18n/generated-locales/*.generated.d.ts",
170170
"default": "./dist/i18n/generated-locales/*.generated.js"
171171
},
172+
"./locales/en/*": null,
172173
"./AlertDialog": {
173174
"source": "./src/AlertDialog/index.ts",
174175
"types": "./dist/AlertDialog/index.d.ts",
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file locale-exports.test.mjs
5+
* @input The package's exports map, read through Node's own package
6+
* resolution from a workspace app that depends on the package
7+
* @output Verifies the public locale surface: every whole catalog under
8+
* `@astryxdesign/core/locales/<locale>.generated.js` resolves, and the
9+
* per-namespace English slices under `locales/en/` do not — they are the
10+
* package's own, rendered through the internal useComponentTranslator
11+
* @position Regression test for the `./locales/en/*` null export
12+
*/
13+
14+
import {createRequire} from 'node:module';
15+
import path from 'node:path';
16+
import {fileURLToPath} from 'node:url';
17+
import {describe, expect, it} from 'vitest';
18+
19+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
20+
const REPO_ROOT = path.resolve(__dirname, '../../../..');
21+
22+
/**
23+
* Resolve a specifier the way a consumer's bundler or Node would: through
24+
* the package's exports map, from a workspace app that depends on the
25+
* package. Only the map is consulted here; a pattern whose target exists
26+
* resolves to that path, and a `null` target or no match throws
27+
* ERR_PACKAGE_PATH_NOT_EXPORTED before any file is read.
28+
*/
29+
const consumerRequire = createRequire(
30+
path.join(REPO_ROOT, 'apps/sandbox/package.json'),
31+
);
32+
33+
function resolveFromConsumer(specifier) {
34+
try {
35+
return {ok: consumerRequire.resolve(specifier)};
36+
} catch (error) {
37+
return {code: error.code};
38+
}
39+
}
40+
41+
describe('the public locale surface of @astryxdesign/core', () => {
42+
it('resolves a whole catalog by locale', () => {
43+
expect(
44+
resolveFromConsumer('@astryxdesign/core/locales/en.generated.js').ok,
45+
).toMatch(/\/dist\/i18n\/generated-locales\/en\.generated\.js$/);
46+
expect(
47+
resolveFromConsumer('@astryxdesign/core/locales/fr-FR.generated.js').ok,
48+
).toMatch(/\/dist\/i18n\/generated-locales\/fr-FR\.generated\.js$/);
49+
});
50+
51+
it('does not resolve a per-namespace English slice', () => {
52+
for (const namespace of ['button', 'pagination']) {
53+
expect(
54+
resolveFromConsumer(
55+
`@astryxdesign/core/locales/en/${namespace}.generated.js`,
56+
),
57+
).toEqual({code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'});
58+
}
59+
});
60+
});

‎scripts/generate-i18n-runtime.mjs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ const GENERATED_LOCALES_DIR = path.join(
3333
);
3434
const EN_FILE = 'en.json';
3535
const PSEUDO_FILE = 'pseudo.json';
36-
/** Where the per-namespace English slices live, under GENERATED_LOCALES_DIR. */
36+
/**
37+
* Where the per-namespace English slices live, under GENERATED_LOCALES_DIR.
38+
* The package's exports map gives `./locales/en/*` a `null` target, so the
39+
* slices are the package's own and never a consumer import path.
40+
*/
3741
const EN_SLICES_DIR = 'en';
3842
const KEY_PREFIX = '@astryx.';
3943

‎scripts/sync-exports.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ const STATIC_EXPORTS = {
188188
types: './dist/i18n/generated-locales/*.generated.d.ts',
189189
default: './dist/i18n/generated-locales/*.generated.js',
190190
},
191+
// The per-namespace English slices under generated-locales/en/ are the
192+
// package's own: a core component renders with its slice through the
193+
// internal useComponentTranslator. The longer pattern wins the match, so
194+
// this keeps them off the public surface while `./locales/en.generated.js`
195+
// and the other whole catalogs stay importable.
196+
'./locales/en/*': null,
191197
};
192198

193199
/**

0 commit comments

Comments
 (0)