weifuwu 0.17.0 → 0.17.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 +6 -1
- package/dist/index.js +19 -11
- package/dist/tsx-context.d.ts +1 -0
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1173,11 +1173,13 @@ app.use(preferences({
|
|
|
1173
1173
|
}))
|
|
1174
1174
|
|
|
1175
1175
|
// In handlers: ctx.t('greeting') → "Hello"
|
|
1176
|
+
// ctx.t('tools.uppercase.title') → "Uppercase" (nested key)
|
|
1176
1177
|
// ctx.locale → "en"
|
|
1177
1178
|
// ctx.theme → "light"
|
|
1178
1179
|
// ctx.prefs → { locale: 'en', theme: 'light' }
|
|
1179
1180
|
// ctx.setPref('locale', 'zh') → 302 + cookie
|
|
1180
1181
|
// ctx.setPref('flash', '{"type":"success","message":"Done"}') → flash message
|
|
1182
|
+
// ctx.env → { WEIFUWU_PUBLIC_API_URL: '...' } (public env vars)
|
|
1181
1183
|
|
|
1182
1184
|
// In tsx components:
|
|
1183
1185
|
const { t, locale, theme } = useCtx()
|
|
@@ -1187,6 +1189,9 @@ Locale detection priority: cookie → `Accept-Language` → default.
|
|
|
1187
1189
|
Theme detection: cookie → default (`'system'`).
|
|
1188
1190
|
Flash messages: set via `ctx.setPref('flash', ...)` → auto-read from cookie → cleared after rendering.
|
|
1189
1191
|
|
|
1192
|
+
`ctx.t()` supports dot-path nested keys: `t('tools.uppercase.title')` traverses the JSON structure.
|
|
1193
|
+
`ctx.env` exposes `WEIFUWU_PUBLIC_*` environment variables on both server and client (via `useCtx().env`).
|
|
1194
|
+
|
|
1190
1195
|
---
|
|
1191
1196
|
|
|
1192
1197
|
## Email
|
|
@@ -1234,7 +1239,7 @@ app.get('/stream', (req, ctx) => createSSEStream(events()))
|
|
|
1234
1239
|
|
|
1235
1240
|
| Hook / Component | Description |
|
|
1236
1241
|
|-----------------|-------------|
|
|
1237
|
-
| `useCtx()` | Unified context — `{ prefs, locale, theme, t, params, query }` (requires `preferences` middleware) |
|
|
1242
|
+
| `useCtx()` | Unified context — `{ prefs, locale, theme, t, params, query, env }` (requires `preferences` middleware) |
|
|
1238
1243
|
| `createStore(initial)` | Zustand-compatible shared state — `getState`, `setState`, `subscribe` |
|
|
1239
1244
|
| `useData(url, opts?)` | SWR-style data fetching — cache, dedup, mutate, fallback |
|
|
1240
1245
|
| `useQueryState(key, default)` | URL query param sync — `?page=1` via `useSyncExternalStore` |
|
package/dist/index.js
CHANGED
|
@@ -571,7 +571,7 @@ import { renderToReadableStream } from "react-dom/server";
|
|
|
571
571
|
import * as esbuild from "esbuild";
|
|
572
572
|
import { readdirSync, statSync, existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2, writeFileSync } from "node:fs";
|
|
573
573
|
import { join, relative, resolve as resolve2, sep, dirname, basename } from "node:path";
|
|
574
|
-
import { pathToFileURL
|
|
574
|
+
import { pathToFileURL } from "node:url";
|
|
575
575
|
import { createHash } from "node:crypto";
|
|
576
576
|
import vm from "node:vm";
|
|
577
577
|
import { createRequire } from "node:module";
|
|
@@ -599,7 +599,6 @@ function useCtx() {
|
|
|
599
599
|
}
|
|
600
600
|
|
|
601
601
|
// tsx-instance.ts
|
|
602
|
-
var __dirname = dirname(fileURLToPath(import.meta.url));
|
|
603
602
|
var liveReloadClients = /* @__PURE__ */ new Set();
|
|
604
603
|
function broadcastReload() {
|
|
605
604
|
for (const ws of liveReloadClients) {
|
|
@@ -890,7 +889,8 @@ var TsxInstance = class {
|
|
|
890
889
|
prefs: ctx.prefs,
|
|
891
890
|
locale: ctx.locale,
|
|
892
891
|
theme: ctx.theme,
|
|
893
|
-
t: ctx.t
|
|
892
|
+
t: ctx.t,
|
|
893
|
+
env: ctx.env
|
|
894
894
|
}
|
|
895
895
|
}, createElement(NfComponent, { params: ctx.params, query: ctx.query }));
|
|
896
896
|
for (let i = rootLayouts.length - 1; i >= 0; i--) {
|
|
@@ -992,16 +992,13 @@ ${src}`;
|
|
|
992
992
|
publicEnv[`process.env.${key}`] = JSON.stringify(process.env[key]);
|
|
993
993
|
}
|
|
994
994
|
}
|
|
995
|
-
const weifuwuAlias = {
|
|
996
|
-
"weifuwu/react": resolve2(__dirname, "react.ts")
|
|
997
|
-
};
|
|
998
995
|
const result = await esbuild.build({
|
|
999
996
|
stdin: { contents: code, loader: "tsx", resolveDir: pagesDir },
|
|
1000
997
|
bundle: true,
|
|
1001
998
|
format: "esm",
|
|
1002
999
|
jsx: "automatic",
|
|
1003
1000
|
jsxImportSource: "react",
|
|
1004
|
-
alias:
|
|
1001
|
+
alias: resolveAliases(),
|
|
1005
1002
|
banner: { js: "self.process={env:{}};" },
|
|
1006
1003
|
define: Object.keys(publicEnv).length > 0 ? publicEnv : void 0,
|
|
1007
1004
|
loader: { ".node": "empty" },
|
|
@@ -1067,7 +1064,8 @@ ${src}`;
|
|
|
1067
1064
|
prefs: ctx.prefs,
|
|
1068
1065
|
locale: ctx.locale,
|
|
1069
1066
|
theme: ctx.theme,
|
|
1070
|
-
t: ctx.t
|
|
1067
|
+
t: ctx.t,
|
|
1068
|
+
env: ctx.env
|
|
1071
1069
|
}
|
|
1072
1070
|
}, createElement(Component, allProps))
|
|
1073
1071
|
);
|
|
@@ -1353,6 +1351,15 @@ function buildHeadPayload(opts) {
|
|
|
1353
1351
|
if (ctx.prefs) ctxData.prefs = ctx.prefs;
|
|
1354
1352
|
if (ctx.locale) ctxData.locale = ctx.locale;
|
|
1355
1353
|
if (ctx.theme) ctxData.theme = ctx.theme;
|
|
1354
|
+
const publicEnv = {};
|
|
1355
|
+
for (const key of Object.keys(process.env)) {
|
|
1356
|
+
if (key.startsWith("WEIFUWU_PUBLIC_")) {
|
|
1357
|
+
publicEnv[key] = process.env[key];
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
if (Object.keys(publicEnv).length > 0) {
|
|
1361
|
+
ctxData.env = publicEnv;
|
|
1362
|
+
}
|
|
1356
1363
|
result += `<script>window.__WEIFUWU_CTX=${JSON.stringify(ctxData)}</script>
|
|
1357
1364
|
`;
|
|
1358
1365
|
return result;
|
|
@@ -6784,9 +6791,10 @@ var defaults = {
|
|
|
6784
6791
|
theme: { default: "system", cookie: "theme" }
|
|
6785
6792
|
};
|
|
6786
6793
|
function translate(msgs, key, params) {
|
|
6787
|
-
const msg =
|
|
6788
|
-
if (
|
|
6789
|
-
|
|
6794
|
+
const msg = key.split(".").reduce((o, k) => o?.[k], msgs);
|
|
6795
|
+
if (msg === void 0 || msg === null) return key;
|
|
6796
|
+
if (!params) return String(msg);
|
|
6797
|
+
let result = String(msg);
|
|
6790
6798
|
for (const [k, v] of Object.entries(params)) {
|
|
6791
6799
|
result = result.replace(`{${k}}`, v);
|
|
6792
6800
|
}
|
package/dist/tsx-context.d.ts
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export interface Context {
|
|
|
10
10
|
prefs?: Record<string, string>;
|
|
11
11
|
theme?: string;
|
|
12
12
|
setPref?: (name: string, value: string) => Response;
|
|
13
|
+
env?: Record<string, string>;
|
|
13
14
|
}
|
|
14
15
|
export type Handler = (req: Request, ctx: Context) => Response | Promise<Response>;
|
|
15
16
|
export type Middleware = (req: Request, ctx: Context, next: Handler) => Response | Promise<Response>;
|