@nckrtl/launch-ui 0.0.31 → 0.0.33

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
- const RESOLVED_VIRTUAL_MODULE_ID = "\0virtual:launch-i18n";
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 = "\0virtual:launch-i18n";
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
- export function initI18n(options) {
25
- locale = options.locale ?? "en";
26
- fallbackLocale = options.fallbackLocale ?? "en";
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
- return useSyncExternalStore(
69
+ useSyncExternalStore(
50
70
  (cb) => { listeners.add(cb); return () => listeners.delete(cb); },
51
- () => locale,
52
- () => locale,
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(/resources\/js\/app\.(tsx|ts|jsx|js)$/)) return null;
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.d.mts CHANGED
@@ -82,7 +82,7 @@ type Environment = Record<string, string | undefined>;
82
82
  /**
83
83
  * Assemble all Vite plugins for the Launch React stack
84
84
  */
85
- declare function getPlugins(options: LaunchConfigOptions, env?: Environment): Promise<PluginOption[]>;
85
+ export declare function getPlugins(options: LaunchConfigOptions, env?: Environment): Promise<PluginOption[]>;
86
86
  //#endregion
87
87
  //#region src/vite/index.d.ts
88
88
  /**
@@ -95,10 +95,10 @@ declare function getPlugins(options: LaunchConfigOptions, env?: Environment): Pr
95
95
  * import { defineLaunchConfig } from '@nckrtl/launch-ui/vite';
96
96
  * export default defineLaunchConfig();
97
97
  */
98
- declare function defineLaunchConfig(options?: LaunchConfigOptions): Promise<(env: import("vite").ConfigEnv) => Promise<UserConfig>>;
98
+ export declare function defineLaunchConfig(options?: LaunchConfigOptions): Promise<(env: import("vite").ConfigEnv) => Promise<UserConfig>>;
99
99
  /**
100
100
  * @deprecated Use `defineLaunchConfig` instead.
101
101
  */
102
- declare const defineCraftConfig: typeof defineLaunchConfig;
102
+ export declare const defineCraftConfig: typeof defineLaunchConfig;
103
103
  //#endregion
104
- export { type CraftConfigOptions, type LaunchConfigOptions, defineCraftConfig, defineLaunchConfig, getPlugins };
104
+ export type { CraftConfigOptions, LaunchConfigOptions };
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 EVENT = "launch:php-update";
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-6AdekIa0.mjs");
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.31",
3
+ "version": "0.0.33",
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",
@@ -30,32 +30,47 @@
30
30
  "test": "node --test tests/*.test.ts && vp test src/vite/config.test.ts",
31
31
  "dev": "storybook dev -p 6006",
32
32
  "build": "shadcn build",
33
- "build:lib": "vp pack src/vite/index.ts --dts --format esm --out-dir dist",
33
+ "build:lib": "vp pack src/vite/index.ts --dts --format esm --out-dir dist --deps.neverBundle vite",
34
34
  "build:storybook": "storybook build && rsync -a storybook-static/ public/",
35
35
  "serve": "npx http-server public -p 4100 -c-1 --cors"
36
36
  },
37
37
  "devDependencies": {
38
+ "@base-ui/react": "^1.3.0",
38
39
  "@chromatic-com/storybook": "^5.3.0",
39
40
  "@inertiajs/react": "^3.0.0",
40
- "@storybook/addon-docs": "10.5.6",
41
- "@storybook/addon-themes": "10.5.6",
42
- "@storybook/react-vite": "10.5.6",
41
+ "@storybook/addon-docs": "10.6.1",
42
+ "@storybook/addon-mcp": "10.6.1",
43
+ "@storybook/addon-themes": "10.6.1",
44
+ "@storybook/react-vite": "10.6.1",
43
45
  "@tailwindcss/vite": "^4.2.1",
44
46
  "@types/node": "^24.13.5",
45
47
  "@types/react": "^19.1.0",
46
48
  "@types/react-dom": "^19.1.0",
47
49
  "class-variance-authority": "^0.7.1",
48
50
  "clsx": "^2.1.1",
51
+ "cmdk": "^1.1.1",
52
+ "date-fns": "^4.1.0",
53
+ "embla-carousel-react": "^8.6.0",
54
+ "input-otp": "^1.4.2",
49
55
  "lucide-react": "^0.577.0",
56
+ "next-themes": "^0.4.6",
50
57
  "react": "^19.1.0",
58
+ "react-day-picker": "^9.14.0",
51
59
  "react-dom": "^19.1.0",
60
+ "react-is": "^19.2.4",
61
+ "react-resizable-panels": "^4.7.5",
62
+ "recharts": "^3.8.0",
52
63
  "reselect": "^5.2.0",
53
64
  "shadcn": "^4.1.0",
54
- "storybook": "10.5.6",
65
+ "sonner": "^2.0.7",
66
+ "storybook": "10.6.1",
55
67
  "tailwind-merge": "^3.5.0",
56
68
  "tailwindcss": "^4.2.1",
69
+ "tw-animate-css": "^1.4.0",
57
70
  "typescript": "^5.9.3",
58
- "vite-plus": "^0.2.9"
71
+ "vaul": "^1.1.2",
72
+ "vite": "npm:@voidzero-dev/vite-plus-core@0.3.3",
73
+ "vite-plus": "^0.3.3"
59
74
  },
60
75
  "peerDependencies": {
61
76
  "@inertiajs/vite": "^3.0.0",
@@ -63,29 +78,18 @@
63
78
  "@tailwindcss/vite": "^4.2.0",
64
79
  "@vitejs/plugin-react": "^6.0.0",
65
80
  "laravel-vite-plugin": "^3.0.0",
66
- "vite-plus": "^0.2.1"
81
+ "vite-plus": "^0.2.1 || ^0.3.0"
67
82
  },
68
83
  "peerDependenciesMeta": {
69
84
  "vite-plugin-run": {
70
85
  "optional": true
71
86
  }
72
87
  },
73
- "dependencies": {
74
- "@base-ui/react": "^1.3.0",
75
- "@storybook/addon-mcp": "^0.4.2",
76
- "cmdk": "^1.1.1",
77
- "date-fns": "^4.1.0",
78
- "embla-carousel-react": "^8.6.0",
79
- "input-otp": "^1.4.2",
80
- "next-themes": "^0.4.6",
81
- "react-day-picker": "^9.14.0",
82
- "react-is": "^19.2.4",
83
- "react-resizable-panels": "^4.7.5",
84
- "recharts": "^3.8.0",
85
- "sonner": "^2.0.7",
86
- "tw-animate-css": "^1.4.0",
87
- "vaul": "^1.1.2",
88
- "vite": "^8.0.2"
89
- },
90
- "packageManager": "npm@11.12.1"
88
+ "packageManager": "npm@11.12.1",
89
+ "overrides": {
90
+ "vite": "npm:@voidzero-dev/vite-plus-core@0.3.3",
91
+ "storybook": {
92
+ "vite-plus": "$vite-plus"
93
+ }
94
+ }
91
95
  }