@nckrtl/launch-ui 0.0.32 → 0.1.0
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.
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { posix, sep } from "node:path";
|
|
1
3
|
//#region src/vite/i18n-plugin.ts
|
|
2
4
|
const VIRTUAL_MODULE_ID = "virtual:launch-i18n";
|
|
3
5
|
const RESOLVED_VIRTUAL_MODULE_ID = "\0" + VIRTUAL_MODULE_ID;
|
|
6
|
+
const ENTRY_PATTERN = /resources\/js\/app\.(tsx|ts|jsx|js)$/;
|
|
7
|
+
const UPDATE_EVENT = "launch:i18n-update";
|
|
4
8
|
/**
|
|
5
9
|
* The i18n runtime — a self-contained module.
|
|
6
10
|
*
|
|
@@ -10,29 +14,45 @@ const RESOLVED_VIRTUAL_MODULE_ID = "\0" + VIRTUAL_MODULE_ID;
|
|
|
10
14
|
* - getLocale() — get current locale
|
|
11
15
|
* - useLocale() — React hook that re-renders on locale change
|
|
12
16
|
* - initI18n() — load translations (called automatically by the Vite plugin)
|
|
17
|
+
* - updateTranslations() — replace one translation file (called on hot update)
|
|
13
18
|
*/
|
|
14
19
|
const I18N_RUNTIME = `
|
|
15
20
|
import { useSyncExternalStore } from "react";
|
|
16
21
|
|
|
17
22
|
let locale = "en";
|
|
18
23
|
let fallbackLocale = "en";
|
|
24
|
+
let version = 0;
|
|
25
|
+
const sources = {};
|
|
19
26
|
const messages = {};
|
|
20
27
|
const listeners = new Set();
|
|
21
28
|
|
|
22
|
-
function notify() { listeners.forEach(fn => fn()); }
|
|
29
|
+
function notify() { version++; listeners.forEach(fn => fn()); }
|
|
23
30
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
for (const [path, mod] of Object.entries(options.files)) {
|
|
31
|
+
function rebuildMessages() {
|
|
32
|
+
for (const lang of Object.keys(messages)) delete messages[lang];
|
|
33
|
+
for (const [path, translations] of Object.entries(sources)) {
|
|
28
34
|
const match = path.match(/\\/([a-z]{2}(?:[-_][a-zA-Z]+)?)\\.json$/);
|
|
29
35
|
if (!match) continue;
|
|
30
36
|
const lang = match[1].replace("-", "_");
|
|
31
|
-
const translations = "default" in mod ? mod.default : mod;
|
|
32
37
|
messages[lang] = { ...(messages[lang] || {}), ...translations };
|
|
33
38
|
}
|
|
34
39
|
}
|
|
35
40
|
|
|
41
|
+
export function initI18n(options) {
|
|
42
|
+
locale = options.locale ?? "en";
|
|
43
|
+
fallbackLocale = options.fallbackLocale ?? "en";
|
|
44
|
+
for (const [path, mod] of Object.entries(options.files)) {
|
|
45
|
+
sources[path] = "default" in mod ? mod.default : mod;
|
|
46
|
+
}
|
|
47
|
+
rebuildMessages();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function updateTranslations(path, translations) {
|
|
51
|
+
sources[path] = translations;
|
|
52
|
+
rebuildMessages();
|
|
53
|
+
notify();
|
|
54
|
+
}
|
|
55
|
+
|
|
36
56
|
export function setLocale(lang) {
|
|
37
57
|
locale = lang;
|
|
38
58
|
if (typeof localStorage !== "undefined") localStorage.setItem("locale", lang);
|
|
@@ -46,11 +66,12 @@ export function setLocale(lang) {
|
|
|
46
66
|
export function getLocale() { return locale; }
|
|
47
67
|
|
|
48
68
|
export function useLocale() {
|
|
49
|
-
|
|
69
|
+
useSyncExternalStore(
|
|
50
70
|
(cb) => { listeners.add(cb); return () => listeners.delete(cb); },
|
|
51
|
-
() =>
|
|
52
|
-
() =>
|
|
71
|
+
() => version,
|
|
72
|
+
() => version,
|
|
53
73
|
);
|
|
74
|
+
return locale;
|
|
54
75
|
}
|
|
55
76
|
|
|
56
77
|
export function __(key, replacements) {
|
|
@@ -70,19 +91,27 @@ export function __(key, replacements) {
|
|
|
70
91
|
* 1. A virtual module "virtual:launch-i18n" with __, setLocale, getLocale, useLocale, initI18n
|
|
71
92
|
* 2. An alias so "@nckrtl/launch-ui/i18n" (and legacy "@nckrtl/craft-ui-react/i18n") resolves to the virtual module
|
|
72
93
|
* 3. Auto-injection of initI18n() into the app entry point
|
|
94
|
+
* 4. Hot updates for translation files: the browser swaps in the new messages instead of
|
|
95
|
+
* reloading the page. With `reloadEvent`, the plugin also sends that event afterwards, so
|
|
96
|
+
* components that call __() without useLocale() render again.
|
|
73
97
|
*/
|
|
74
|
-
function launchI18nPlugin(i18n) {
|
|
98
|
+
function launchI18nPlugin(i18n, { reloadEvent } = {}) {
|
|
75
99
|
const opts = typeof i18n === "object" ? i18n : {};
|
|
76
100
|
const locale = opts.locale ?? "en";
|
|
77
101
|
const fallbackLocale = opts.fallbackLocale ?? "en";
|
|
78
102
|
const langPath = opts.langPath ?? "/lang/*.json";
|
|
79
103
|
const initCode = `
|
|
80
|
-
import { initI18n } from "virtual:launch-i18n";
|
|
104
|
+
import { initI18n, updateTranslations as __launchUpdateTranslations } from "virtual:launch-i18n";
|
|
81
105
|
initI18n({
|
|
82
106
|
locale: ${JSON.stringify(locale)},
|
|
83
107
|
fallbackLocale: ${JSON.stringify(fallbackLocale)},
|
|
84
108
|
files: import.meta.glob(${JSON.stringify(langPath)}, { eager: true }),
|
|
85
109
|
});
|
|
110
|
+
if (import.meta.hot) {
|
|
111
|
+
import.meta.hot.on(${JSON.stringify(UPDATE_EVENT)}, ({ path, translations }) => {
|
|
112
|
+
__launchUpdateTranslations(path, translations);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
86
115
|
`;
|
|
87
116
|
return {
|
|
88
117
|
name: "launch-i18n",
|
|
@@ -100,13 +129,47 @@ initI18n({
|
|
|
100
129
|
if (id === RESOLVED_VIRTUAL_MODULE_ID) return I18N_RUNTIME;
|
|
101
130
|
},
|
|
102
131
|
transform(code, id) {
|
|
103
|
-
if (!id.match(
|
|
132
|
+
if (!id.match(ENTRY_PATTERN)) return null;
|
|
104
133
|
return {
|
|
105
134
|
code: initCode + code,
|
|
106
135
|
map: null
|
|
107
136
|
};
|
|
137
|
+
},
|
|
138
|
+
hotUpdate({ file, modules, timestamp }) {
|
|
139
|
+
if (this.environment.name !== "client" || !file.endsWith(".json")) return;
|
|
140
|
+
const entry = modules.flatMap((module) => [...module.importers]).find((importer) => importer.file && ENTRY_PATTERN.test(importer.file));
|
|
141
|
+
if (!entry?.file) return;
|
|
142
|
+
let translations;
|
|
143
|
+
try {
|
|
144
|
+
translations = JSON.parse(readFileSync(file, "utf-8"));
|
|
145
|
+
} catch {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
for (const module of modules) this.environment.moduleGraph.invalidateModule(module, /* @__PURE__ */ new Set(), timestamp, true);
|
|
149
|
+
const path = translationKey(file, entry.file, this.environment.config.root, langPath);
|
|
150
|
+
this.environment.hot.send({
|
|
151
|
+
type: "custom",
|
|
152
|
+
event: UPDATE_EVENT,
|
|
153
|
+
data: {
|
|
154
|
+
path,
|
|
155
|
+
translations
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
if (reloadEvent) this.environment.hot.send({
|
|
159
|
+
type: "custom",
|
|
160
|
+
event: reloadEvent,
|
|
161
|
+
data: { file }
|
|
162
|
+
});
|
|
163
|
+
return [];
|
|
108
164
|
}
|
|
109
165
|
};
|
|
110
166
|
}
|
|
167
|
+
/** The key import.meta.glob gives the file, so the update replaces the right source. */
|
|
168
|
+
function translationKey(file, entry, root, langPath) {
|
|
169
|
+
const toPosix = (path) => path.split(sep).join("/");
|
|
170
|
+
if (langPath.startsWith("/")) return "/" + posix.relative(toPosix(root), toPosix(file));
|
|
171
|
+
const relative = posix.relative(posix.dirname(toPosix(entry)), toPosix(file));
|
|
172
|
+
return relative.startsWith(".") ? relative : "./" + relative;
|
|
173
|
+
}
|
|
111
174
|
//#endregion
|
|
112
175
|
export { launchI18nPlugin };
|
package/dist/index.mjs
CHANGED
|
@@ -25,7 +25,8 @@ function inertiaOptions(options, env) {
|
|
|
25
25
|
}
|
|
26
26
|
//#endregion
|
|
27
27
|
//#region src/vite/php-reload-plugin.ts
|
|
28
|
-
const
|
|
28
|
+
const PHP_UPDATE_EVENT = "launch:php-update";
|
|
29
|
+
const EVENT = PHP_UPDATE_EVENT;
|
|
29
30
|
const CLIENT_CODE = `
|
|
30
31
|
import { router as __launchRouter } from "@inertiajs/react";
|
|
31
32
|
|
|
@@ -130,8 +131,8 @@ async function getPlugins(options, env = process.env) {
|
|
|
130
131
|
}));
|
|
131
132
|
}
|
|
132
133
|
if (options.i18n) {
|
|
133
|
-
const { launchI18nPlugin } = await import("./i18n-plugin-
|
|
134
|
-
plugins.push(launchI18nPlugin(options.i18n));
|
|
134
|
+
const { launchI18nPlugin } = await import("./i18n-plugin-COS6dy2U.mjs");
|
|
135
|
+
plugins.push(launchI18nPlugin(options.i18n, { reloadEvent: phpReload ? PHP_UPDATE_EVENT : void 0 }));
|
|
135
136
|
}
|
|
136
137
|
plugins.push(launchPublicDevServerUrlPlugin(env));
|
|
137
138
|
const runners = await getArtisanRunners();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nckrtl/launch-ui",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "A React component library and Vite toolchain for building Laravel + React + Inertia apps. Built on Base UI and shadcn.",
|
|
5
5
|
"author": "nckrtl <nick.retel@gmail.com>",
|
|
6
6
|
"license": "MIT",
|