Files
Media-download-telegram-bot/web/miniapp/vite.config.ts
T
MichilisandClaude Fable 5.2 4f11f939a0 Phase 6: Mini App
A Telegram Mini App served by the core under /app/, embedded in the binary.

- API under /app/api/ with initData validated (HMAC, age) on every call
- Pages: Home, Queue, Buy (target, product, invoice), History, Groups,
  group detail (Overview, Settings, Wallet, Usage) and Me
- Every action runs through the same service, billing and scheduler code
  as the chat, so limits, prices and admin checks are decided on the server
- Texts come from the catalog over the API; layout mirrors for RTL
- Front end in web/miniapp (Vite, React, TypeScript); the build is
  committed and guarded by a source hash test; the Docker image rebuilds it
- MINIAPP_ENABLED with an https PUBLIC_URL; entry points from /app, group
  settings and the over-limit offer
- Browser test (make miniapp-e2e) against a core in test mode
- Fix: each download attempt gets its own scratch directory, so a retry
  cannot lose its file to the cleanup of the failed attempt

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 10:27:20 +00:00

58 lines
1.9 KiB
TypeScript

import { createHash } from "node:crypto";
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { defineConfig, type Plugin } from "vite";
import react from "@vitejs/plugin-react";
// sourceStamp writes a hash of everything the build is made from next to
// the build. The Go test in this directory computes the same hash, so a
// change under src that was committed without a rebuild fails the tests.
const stamped = ["index.html", "package.json", "package-lock.json", "tsconfig.json", "vite.config.ts"];
function walk(dir: string): string[] {
return readdirSync(dir, { withFileTypes: true }).flatMap((e) =>
e.isDirectory() ? walk(join(dir, e.name)) : [join(dir, e.name)],
);
}
function sourceStamp(): Plugin {
return {
name: "source-stamp",
apply: "build",
closeBundle() {
const hash = createHash("sha256");
const files = [...stamped, ...walk("src")].map((f) => f.split("\\").join("/")).sort();
for (const f of files) {
hash.update(f + "\0");
hash.update(readFileSync(f));
hash.update("\0");
}
writeFileSync("dist/source.sha256", hash.digest("hex") + "\n");
},
};
}
// The app is served by the core under /app/ and embedded in its binary.
// File names are stable so a rebuild changes as few files as possible;
// the core sends an ETag per file, so caches still notice a new build.
export default defineConfig({
base: "/app/",
plugins: [react(), sourceStamp()],
build: {
outDir: "dist",
emptyOutDir: true,
target: "es2020",
rollupOptions: {
output: {
entryFileNames: "assets/app.js",
chunkFileNames: "assets/[name].js",
assetFileNames: "assets/[name][extname]",
},
},
},
server: {
// npm run dev: API calls go to a core running locally.
proxy: { "/app/api": "http://localhost:8080", "/pay": "http://localhost:8080" },
},
});