vite-bundled-i18n 0.6.0 → 0.6.2
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/README.md +114 -5
- package/dist/cli/index.js +1 -1
- package/dist/core/config.d.ts +22 -0
- package/dist/data-DtpMCpiZ.js.map +1 -1
- package/dist/plugin.js +164 -8
- package/dist/plugin.js.map +1 -1
- package/dist/{scope-registration-ACWKuj8f.js → scope-registration-xea-PWbh.js} +2 -2
- package/dist/{scope-registration-ACWKuj8f.js.map → scope-registration-xea-PWbh.js.map} +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,8 +30,13 @@ The Vite plugin parses your import graph, maps every `t()` / `useI18n()` / `i18n
|
|
|
30
30
|
|
|
31
31
|
- **Scope bundles** — each route loads only its own translations in one HTTP request
|
|
32
32
|
- **Page scope map** — framework-neutral `scope-map.json` + typed `PAGE_SCOPE_MAP` const, so any router can parallelize scope loads with component resolution ([router integration](#router-integration))
|
|
33
|
+
- **Readiness gate** — framework-agnostic `i18n.gate` tracks outstanding `loadScope()` calls; React `<GateBoundary>` / `useGate()` and Vue `useGate()` consume it directly. One primitive, zero consumer counters
|
|
34
|
+
- **`createScopeMapClient()`** — typed runtime client for the emitted scope-map, with in-flight dedup and generation-safe invalidation. Works in any router context
|
|
33
35
|
- **Cross-namespace packing** (opt-in) — inline a route's cross-namespace keys, tree-shaken, into its scope bundle. 1–3 foreign keys don't force a whole dictionary to load globally
|
|
34
36
|
- **Persistent extraction cache** — per-file AST results survive between runs. Warm dev starts in ~250 ms instead of 3–10 s; repeat builds skip the walk when nothing changed
|
|
37
|
+
- **Lean dev bundles** — dev responses are tree-shaken by default (matches prod shape). ~10–20× smaller on large apps; opt-out via `bundling.dev.leanBundles: false`
|
|
38
|
+
- **`t.dynamic()` + `bundling.dynamicKeys`** — loose-typed escape hatch for runtime-computed keys, paired with a config list so declared dynamic keys actually ship
|
|
39
|
+
- **`vite-bundled-i18n/testing`** — `createTestI18n`, `I18nTestProvider`, `createI18nTestPlugin` — synchronous, network-free, ready-by-default for unit tests
|
|
35
40
|
- **Progressive autocomplete** — generated types let the IDE suggest one key segment at a time
|
|
36
41
|
|
|
37
42
|
### Also included
|
|
@@ -42,6 +47,7 @@ The Vite plugin parses your import graph, maps every `t()` / `useI18n()` / `i18n
|
|
|
42
47
|
- **Multi-framework** — React, Vue, vanilla JS share a single core
|
|
43
48
|
- **SSR** — server rendering with automatic client hydration via `window.__I18N_RESOURCES__`
|
|
44
49
|
- **AST extraction** — `t()`, `useI18n()`, `i18nKey()`, `defineI18nData()`, configurable `keyFields`, `as const` objects, string enums, helper functions that receive `t` as a parameter
|
|
50
|
+
- **Scope registration audit** — `bundling.strictScopeRegistration: 'off' | 'warn' | 'error'` catches routes that extract zero keys at build time
|
|
45
51
|
- **Dev toolbar** — scope-aware missing-key panel (filters by current route, resets on locale change and HMR), per-namespace bundle efficiency, live updates on navigation
|
|
46
52
|
|
|
47
53
|
## Performance
|
|
@@ -51,7 +57,8 @@ The library is a performance play end-to-end. Representative numbers for a 50-pa
|
|
|
51
57
|
| | Traditional i18n | vite-bundled-i18n |
|
|
52
58
|
|---|---|---|
|
|
53
59
|
| Keys per page load | 3,000 (all) | 25–40 (route-extracted) |
|
|
54
|
-
|
|
|
60
|
+
| Scope bundle size (prod) | N/A (monolithic) | ~97% smaller than the source namespace |
|
|
61
|
+
| Scope bundle size (dev) | — | same as prod by default (lean bundles) |
|
|
55
62
|
| HTTP requests per route | many (or one huge) | 1 scope bundle + cached dictionaries |
|
|
56
63
|
| Cold dev start | — | ~200 ms |
|
|
57
64
|
| Warm dev start | — | ~250 ms (extraction cache keeps prior AST parses) |
|
|
@@ -63,6 +70,8 @@ The extraction cache persists to `.i18n/cache/` and auto-invalidates on plugin u
|
|
|
63
70
|
|
|
64
71
|
Compiled mode replaces JSON parsing with pre-resolved flat `Map` modules. Faster than JSON at the browser level (no parse on first lookup), smaller gzipped (shared string pool), cacheable by the browser's module loader. Falls back to JSON automatically when compiled modules aren't available.
|
|
65
72
|
|
|
73
|
+
Lean dev bundles tree-shake each scope response to the keys the route actually extracted — same shape as the production build. On a 50-page admin, dev scope responses drop from ~140 kB to ~8 kB. Opt out via `bundling.dev.leanBundles: false` if you want the v0.6.0 full-namespace behavior.
|
|
74
|
+
|
|
66
75
|
## Setup
|
|
67
76
|
|
|
68
77
|
Three files wire everything together:
|
|
@@ -250,6 +259,53 @@ i18nPlugin(config, {
|
|
|
250
259
|
|
|
251
260
|
Whatever your function returns becomes the key in `scope-map.json` (served at `/__i18n/scope-map.json` in dev, emitted at build under `assetsDir`) and the `I18nPageIdentifier` union in generated types. Routers that want the data without types can fetch the JSON directly.
|
|
252
261
|
|
|
262
|
+
For pure runtime access without tying into the plugin's type generation, `createScopeMapClient()` is the framework-agnostic fetcher:
|
|
263
|
+
|
|
264
|
+
```ts
|
|
265
|
+
import { createScopeMapClient } from 'vite-bundled-i18n'
|
|
266
|
+
|
|
267
|
+
const scopeMap = createScopeMapClient()
|
|
268
|
+
await scopeMap.load()
|
|
269
|
+
scopeMap.getSync('products/show') // ['products.show']
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
Dedupes concurrent `load()` calls, invalidation during an in-flight fetch is generation-safe, and the default URL honors the plugin-injected base path so Laravel / subdirectory deploys work without wiring.
|
|
273
|
+
|
|
274
|
+
## Readiness gate
|
|
275
|
+
|
|
276
|
+
`i18n.gate` is a framework-agnostic primitive that tracks outstanding `loadScope()` calls. Every scope load auto-registers; when all settle, the gate flips back to ready. React and Vue adapters read it directly — no consumer counter needed.
|
|
277
|
+
|
|
278
|
+
```tsx
|
|
279
|
+
import { GateBoundary, useGate } from 'vite-bundled-i18n/react'
|
|
280
|
+
|
|
281
|
+
// Children always mounted; fallback overlays while loading.
|
|
282
|
+
<GateBoundary fallback={<LoadingBar />}>
|
|
283
|
+
<App />
|
|
284
|
+
</GateBoundary>
|
|
285
|
+
|
|
286
|
+
// Or read the state manually.
|
|
287
|
+
function Indicator() {
|
|
288
|
+
const { ready, pendingCount } = useGate()
|
|
289
|
+
if (ready) return null
|
|
290
|
+
return <Spinner label={`Loading ${pendingCount} scope(s)…`} />
|
|
291
|
+
}
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Vue exposes the same shape:
|
|
295
|
+
|
|
296
|
+
```ts
|
|
297
|
+
import { useGate } from 'vite-bundled-i18n/vue'
|
|
298
|
+
const { ready, pendingCount } = useGate()
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
**Breaking in v0.6.0:** `loadScope()` now auto-tracks the gate by default. Apps that kept their own readiness counters should remove them. Per-call opt-out:
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
await i18n.loadScope(locale, scope, { trackReadiness: false })
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
The primitive is designed to be authoritative — no feature-flag softening. See [API docs — Readiness gate](./docs/api.md#readiness-gate).
|
|
308
|
+
|
|
253
309
|
## Vue
|
|
254
310
|
|
|
255
311
|
```ts
|
|
@@ -292,6 +348,23 @@ translations.tryGet('shared.ok') // undefined on miss (no fal
|
|
|
292
348
|
translations.require('shared.ok') // throws on miss
|
|
293
349
|
translations.namespace('global').get('nav.home') // namespace-scoped access
|
|
294
350
|
translations.forLocale('bg').get('shared.ok') // cross-locale lookup
|
|
351
|
+
|
|
352
|
+
t.dynamic(`status.${state}`) // escape hatch — loose-typed
|
|
353
|
+
t.dynamic(`status.${state}`, 'Unknown') // same overloads as t()
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
`t.dynamic(key)` accepts any string, bypassing the typed `TranslationKey` union. Identical runtime to `t()` — only the type contract differs. Use for keys assembled from variables. Pair with `bundling.dynamicKeys` so declared runtime keys actually ship:
|
|
357
|
+
|
|
358
|
+
```ts
|
|
359
|
+
defineI18nConfig({
|
|
360
|
+
localesDir: 'locales',
|
|
361
|
+
bundling: {
|
|
362
|
+
// Each key is injected into every route whose scope primary namespace
|
|
363
|
+
// matches. Dictionary-owned keys are skipped; orphans (no matching
|
|
364
|
+
// route, no dictionary) emit a build warning.
|
|
365
|
+
dynamicKeys: ['status.active', 'status.pending', 'status.failed'],
|
|
366
|
+
},
|
|
367
|
+
})
|
|
295
368
|
```
|
|
296
369
|
|
|
297
370
|
## Dictionaries
|
|
@@ -442,13 +515,49 @@ cache: {
|
|
|
442
515
|
}
|
|
443
516
|
```
|
|
444
517
|
|
|
518
|
+
## Testing
|
|
519
|
+
|
|
520
|
+
Unit tests and integration tests import from `vite-bundled-i18n/testing`. `createTestI18n` returns a synchronous instance seeded with translations you supply — no network, no pending promises, gate starts ready:
|
|
521
|
+
|
|
522
|
+
```ts
|
|
523
|
+
import { createTestI18n, I18nTestProvider } from 'vite-bundled-i18n/testing'
|
|
524
|
+
import { render, screen } from '@testing-library/react'
|
|
525
|
+
|
|
526
|
+
const i18n = createTestI18n({
|
|
527
|
+
translations: {
|
|
528
|
+
shared: { ok: 'OK' },
|
|
529
|
+
products: { show: { title: 'Details' } },
|
|
530
|
+
},
|
|
531
|
+
})
|
|
532
|
+
|
|
533
|
+
render(
|
|
534
|
+
<I18nTestProvider instance={i18n}>
|
|
535
|
+
<MyComponent />
|
|
536
|
+
</I18nTestProvider>,
|
|
537
|
+
)
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
Options:
|
|
541
|
+
|
|
542
|
+
- `locale` — default `'en'`
|
|
543
|
+
- `defaultLocale` — default: same as `locale`
|
|
544
|
+
- `supportedLocales` — default: `[locale]`
|
|
545
|
+
- `passthroughMissing` — default `true`. Set `false` to have missing keys throw, turning "I forgot to seed" into a hard test failure instead of a silent key-as-value.
|
|
546
|
+
|
|
547
|
+
Vue equivalent:
|
|
548
|
+
|
|
549
|
+
```ts
|
|
550
|
+
import { createI18nTestPlugin } from 'vite-bundled-i18n/testing'
|
|
551
|
+
app.use(createI18nTestPlugin(i18n))
|
|
552
|
+
```
|
|
553
|
+
|
|
445
554
|
## Package Entries
|
|
446
555
|
|
|
447
556
|
| Entry | Purpose |
|
|
448
557
|
|-------|---------|
|
|
449
|
-
| `vite-bundled-i18n` | Core runtime, config helpers,
|
|
450
|
-
| `vite-bundled-i18n/react` | `I18nProvider`, `useI18n`, `I18nBoundary`, `DevToolbar` |
|
|
451
|
-
| `vite-bundled-i18n/vue` | `createI18nPlugin`, `useI18n` |
|
|
558
|
+
| `vite-bundled-i18n` | Core runtime, config helpers, `createReadinessGate`, `createScopeMapClient` |
|
|
559
|
+
| `vite-bundled-i18n/react` | `I18nProvider`, `useI18n`, `I18nBoundary`, `GateBoundary`, `useGate`, `DevToolbar` |
|
|
560
|
+
| `vite-bundled-i18n/vue` | `createI18nPlugin`, `useI18n`, `useGate` |
|
|
452
561
|
| `vite-bundled-i18n/vanilla` | `getTranslations`, `initI18n` |
|
|
453
562
|
| `vite-bundled-i18n/server` | `initServerI18n` (SSR) |
|
|
454
563
|
| `vite-bundled-i18n/plugin` | Vite plugin (`i18nPlugin`) |
|
|
@@ -463,7 +572,7 @@ cache: {
|
|
|
463
572
|
|
|
464
573
|
## Releases
|
|
465
574
|
|
|
466
|
-
Current release: **v0.6.
|
|
575
|
+
Current release: **v0.6.1** — lean dev bundles (dev responses tree-shaken by default, ~95% smaller on large apps). Built on v0.6.0's framework-agnostic readiness gate (`i18n.gate`, `<GateBoundary>`, `useGate()`), runtime `createScopeMapClient()`, `t.dynamic()` escape hatch, `bundling.dynamicKeys` + `strictScopeRegistration`, `vite-bundled-i18n/testing` subpackage. Breaking in v0.6.0: `loadScope` auto-registers with the gate by default — opt out via `{ trackReadiness: false }`.
|
|
467
576
|
|
|
468
577
|
Version history lives in the [git log](https://github.com/nicolasvlachos/vite-bundled-i18n/commits/main) — each release is a `feat: v{version}` commit with a summary of the changes.
|
|
469
578
|
|
package/dist/cli/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { _ as writeRuntimeConst, a as PLUGIN_VERSION, b as generateBundles, c as createExtractionCache, f as flattenLocaleKeys, g as runtimePathFromTypesPath, i as buildScopeMap, m as generateReports, n as compileAll, o as resolveCacheConfig, r as applyDynamicKeys, s as computeConfigHash, t as checkScopeRegistration, u as walkAll, v as writeTypes } from "../scope-registration-
|
|
2
|
+
import { _ as writeRuntimeConst, a as PLUGIN_VERSION, b as generateBundles, c as createExtractionCache, f as flattenLocaleKeys, g as runtimePathFromTypesPath, i as buildScopeMap, m as generateReports, n as compileAll, o as resolveCacheConfig, r as applyDynamicKeys, s as computeConfigHash, t as checkScopeRegistration, u as walkAll, v as writeTypes } from "../scope-registration-xea-PWbh.js";
|
|
3
3
|
import fs from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
//#region src/cli/commands.ts
|
package/dist/core/config.d.ts
CHANGED
|
@@ -90,6 +90,28 @@ export interface I18nSharedConfig {
|
|
|
90
90
|
* ```
|
|
91
91
|
*/
|
|
92
92
|
dynamicKeys?: readonly string[];
|
|
93
|
+
/** Dev-server bundle shape. Build output is always tree-shaken. */
|
|
94
|
+
dev?: {
|
|
95
|
+
/**
|
|
96
|
+
* Tree-shake dev bundles using the walker's extraction data.
|
|
97
|
+
*
|
|
98
|
+
* When `true` (default), each dev scope-bundle response contains only
|
|
99
|
+
* the keys the route's AST extraction found — same shape as the
|
|
100
|
+
* production build. Matches prod semantics and keeps per-scope payloads
|
|
101
|
+
* small on large apps (typically 10× to 20× smaller than the full
|
|
102
|
+
* namespace).
|
|
103
|
+
*
|
|
104
|
+
* When `false`, falls back to the v0.6.0 behavior of shipping the
|
|
105
|
+
* whole namespace (plus any full cross-ns extras). Kept for escape
|
|
106
|
+
* hatch — properly-structured apps should leave the default on.
|
|
107
|
+
*
|
|
108
|
+
* Falls back to full namespaces automatically when `options.pages`
|
|
109
|
+
* isn't configured (no analysis available).
|
|
110
|
+
*
|
|
111
|
+
* @default true
|
|
112
|
+
*/
|
|
113
|
+
leanBundles?: boolean;
|
|
114
|
+
};
|
|
93
115
|
};
|
|
94
116
|
}
|
|
95
117
|
/**
|