Files
poli0981andClaude Opus 5.5 0f600a99aa build(web): migrate to SvelteKit 3 and adapter-static 4
@sveltejs/kit 2.70.3 -> 3.1.0, @sveltejs/adapter-static 3.0.10 -> 4.0.0.
Closes GHSA-27mw-82g8-phjj and GHSA-pfhg-q3qv-9q6r (dismissed in v4.4.1
as not applicable), and drops the cookie override kit 2 needed: npm audit
reports nothing.

- Config: svelte.config.js is refused by kit 3. Its options are now
  sveltekitConfig(tauri) in build/sveltekit-config.ts, passed by
  vite.config.ts as sveltekit(sveltekitConfig(IS_TAURI)), with tests for
  both CSP flavours, paths.relative, the sitemap entry and the prerender
  404 handler. The unused, now-deprecated "@" alias and typescript.config
  hook are gone; tsconfig.json extends $app/tsconfig and owns `include`.
- $lib -> #lib (package.json imports), with .js on module imports.
  tsconfig repeats the mapping in `paths`: svelte-check --tsgo only maps
  .svelte imports through `paths`, and without it every #lib component
  fell back to the *.svelte shim (props unchecked; proven with a planted
  error).
- Param matchers: src/params/legaldoc.ts -> src/params.ts (defineParams).
  vite build imports that file with Node's own import(), so it imports
  #lib/legal.ts and legal.ts imports ./schema.ts by their real names.
- PWA: @vite-pwa/sveltekit never supported kit 3 and wrote the worker when
  the SERVER build closed, which kit 3's environment build does first.
  build/pwa.ts wires vite-plugin-pwa in a buildApp hook after kit's
  prerender and before its adapter, with the same globs and URL mapping.
- goto options renamed (replace, reset: false, refreshAll); the /games
  filter write is a shallow goto; a legacy /#/ path that matches no route
  falls back to location.replace (kit 3's goto rejects it). Hook types
  from @sveltejs/kit/hooks; the client handleError returns nothing to keep
  kit's message.
- runed 0.35 (bits-ui) peers kit ^2: overridden to our kit, as for
  echarts-wordcloud.

Verified against a kit 2 build of the same commit: same 11,037 files bar
hashed chunks, same non-immutable precache URLs, navigateFallback "/",
identical CSP per page (hashes masked), manifest and sitemap byte-equal,
same visible text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 14:35:49 +07:00

248 lines
12 KiB
TypeScript

/// <reference types="vitest/config" />
import { defineConfig, type Plugin } from "vite";
import { sveltekit } from "@sveltejs/kit/vite";
// Tailwind 4 runs as a Vite plugin instead of a PostCSS plugin;
// tailwind.config.ts and postcss.config.js do not exist.
import tailwindcss from "@tailwindcss/vite";
import { visualizer } from "rollup-plugin-visualizer";
import path from "node:path";
import { readFileSync } from "node:fs";
import { gameSeeds } from "./build/game-seeds";
import { legalVersions } from "./build/legal-versions";
import { svelteKitPWA } from "./build/pwa";
import { sveltekitConfig } from "./build/sveltekit-config";
// Version sourced from package.json (single source of truth, matches the Android
// versionName) and exposed to the bundle as `__APP_VERSION__` for the update check.
const appVersion = JSON.parse(
readFileSync(path.resolve(__dirname, "package.json"), "utf-8"),
).version as string;
/**
* True while the Tauri CLI is driving this build.
*
* Tauri 2 sets TAURI_ENV_* for the process running beforeBuildCommand, so
* this needs no extra npm script and no cross-env - which matters, because
* `VAR=1 vite build` in package.json does not work on Windows.
*/
const IS_TAURI = Boolean(process.env.TAURI_ENV_PLATFORM);
/**
* Prerender one page per game (web only).
*
* Off for Tauri: 3,650 HTML files is real APK weight, and the packaged apps
* resolve /games/<appid> through the SPA fallback anyway. Off when
* F2P_SKIP_GAME_PRERENDER is set, for quick local builds.
*/
const PRERENDER_GAMES = !IS_TAURI && !process.env.F2P_SKIP_GAME_PRERENDER;
/**
* The packaged apps omit the PWA plugin entirely (see below), so its virtual
* modules do not exist there. The app imports them unconditionally, so this
* resolves both to no-ops for that build.
*/
function pwaVirtualStubs(): Plugin {
const modules: Record<string, string> = {
"virtual:pwa-register": "export function registerSW() { return async () => {}; }",
"virtual:pwa-info": "export const pwaInfo = undefined;",
};
return {
name: "f2p-pwa-stubs",
resolveId: (id) => (id in modules ? `\0${id}` : null),
load: (id) => (id.startsWith("\0") ? (modules[id.slice(1)] ?? null) : null),
};
}
export default defineConfig(({ mode }) => ({
// No `base`. SvelteKit owns the base path (its `paths` option), and setting
// Vite's would fight it.
define: {
__APP_VERSION__: JSON.stringify(appVersion),
// Read by routes/games/[appid]/+page.ts. A route file cannot read
// process.env itself - it also ships to the browser.
__PRERENDER_GAMES__: JSON.stringify(PRERENDER_GAMES),
},
plugins: [
// Tailwind before sveltekit: the Svelte plugin needs the CSS transform
// already registered when it processes <style> blocks.
tailwindcss(),
gameSeeds({ enabled: PRERENDER_GAMES, dataDir: path.resolve(__dirname, "../data") }),
// Content hashes for the binding legal documents, so the consent gate can
// show what changed instead of asking for a second full read. Every
// flavour: the packaged apps run the same gate.
legalVersions(),
// Adapter, CSP, paths, prerender: build/sveltekit-config.ts (until
// SvelteKit 3, svelte.config.js).
sveltekit(sveltekitConfig(IS_TAURI)),
/**
* WEB ONLY. A service worker registered at tauri.localhost can never be
* updated - Tauri's custom protocol does not satisfy the update
* algorithm's refetch of the worker script, so the first version a user
* ever ran keeps serving its precache forever.
*
* Measured, not assumed: the 2.0.0 desktop build launched showing the
* React app, because index.html came from a precache written by an
* earlier release. Every future release would have shipped invisible.
*
* The packaged apps lose nothing: their assets are local files already,
* and offline DATA lives in the IndexedDB cache in lib/cache.ts.
* lib/pwa.ts removes any worker left behind by an older build.
*/
IS_TAURI && pwaVirtualStubs(),
// After sveltekit(): build/pwa.ts writes the service worker between
// SvelteKit's prerender and its adapter, and relies on this order.
!IS_TAURI &&
svelteKitPWA({
// "prompt": a new version waits until the reader chooses to reload
// (lib/common/PwaIndicator.svelte), instead of the page reloading
// itself mid-read. injectRegister false: nothing was ever injected
// anyway - SvelteKit has no index.html for the plugin to write a
// <script> into, which is why the worker was built and never
// registered. lib/pwa-state.svelte.ts registers it, after consent.
registerType: "prompt",
injectRegister: false,
manifest: {
name: "Steam F2P Tracker",
short_name: "F2P Tracker",
description:
"Browse and analyse the catalogue of free-to-play Steam games tracked in this repository.",
theme_color: "#14120F",
background_color: "#14120F",
display: "standalone",
scope: "/",
start_url: "/",
// Absolute paths. A relative manifest icon src resolves against the
// MANIFEST's URL, which is fine at / and wrong the moment the manifest
// is requested from a deep route - and the SPA fallback answers the
// miss with index.html at HTTP 200, so it fails silently.
//
// "any" and "maskable" are separate entries, not one "any maskable":
// a maskable icon is padded so Android can crop it to a circle, and
// reusing that same padded art for "any" leaves a visibly small mark
// floating in a large box everywhere else.
icons: [
{ src: "/icon-192.png", sizes: "192x192", type: "image/png", purpose: "any" },
{ src: "/icon-512.png", sizes: "512x512", type: "image/png", purpose: "any" },
{ src: "/icon-maskable-512.png", sizes: "512x512", type: "image/png", purpose: "maskable" },
{ src: "/icon.svg", sizes: "any", type: "image/svg+xml", purpose: "any" },
],
},
workbox: {
// Big enough for the echarts chunk, which is ~700 KB on its own.
maximumFileSizeToCacheInBytes: 6 * 1024 * 1024,
globPatterns: ["**/*.{js,css,html,svg,woff2}"],
// The social card is 145 KB that no VISITOR ever loads - it exists
// for crawlers. build/pwa.ts adds every static asset to the
// precache on its own, regardless of globPatterns, so keeping it
// out of every install takes an explicit ignore.
// The prerendered game pages: ~3,650 HTML files, ~120 MB. The glob
// runs over .svelte-kit/output, not dist, and without this every
// install would precache all of them.
globIgnores: ["**/og.png", "prerendered/pages/games/**", "**/games/*.html"],
// "/" — the prerendered dashboard — and NOT "/200.html", which the
// precache does not contain and cannot contain. adapter-static
// writes 200.html with generateFallback() straight into dist/,
// AFTER workbox has globbed .svelte-kit/output; there is no
// 200.html under .svelte-kit at all. So createHandlerBoundToURL()
// threw `non-precached-url` on every load, taking the rest of the
// worker's top-level setup with it.
//
// "/" is the right target anyway: it is exactly what BOTH hosts
// already serve for an unmatched path (Cloudflare's
// not_found_handling: "single-page-application", and Tauri's
// get_asset() chain), so the offline fallback now behaves like the
// online one and lib/fallback-route.ts re-renders the real route.
// verify-dist.mjs fails the build if this ever names a URL the
// precache manifest does not list.
navigateFallback: "/",
// These are Worker routes, not app navigations. Without the denylist an
// installed service worker answers them from the app shell and they
// never reach Cloudflare - which for /admin means serving the public
// shell where the Access gate is expected.
navigateFallbackDenylist: [/^\/api\//, /^\/img\//, /^\/admin/],
runtimeCaching: [
// NO rule for /api/data/*. It used to be NetworkFirst with a 5 s
// timeout, so a slow index.json came back from this cache and the
// client took an old generation for the current one. The dataset's
// offline copy is IndexedDB, which games-loader.ts writes only once
// every shard has matched index.json's sha256 - and shards requested
// by hash are immutable HTTP responses the browser cache already
// handles. lib/pwa.ts deletes the old "f2p-data-v3" cache.
// Artwork AND the GitHub avatars at /img/gh/*, which share the prefix.
// Steam's ?t= is an asset mtime, so a changed image arrives as a
// different URL and CacheFirst is safe. Status 200 only: caching an
// opaque 0-status placeholder for 30 days is how a transient upstream
// failure becomes permanent.
//
// And X-Img-Final only. /img/s and /img/d answer an AVIF-capable
// browser with Steam's JPEG until the cron has minted the AVIF
// (worker/routes/img.ts); that stand-in must not be kept, and
// CacheFirst ignores Cache-Control. The name stays f2p-img-v1:
// docs/PRIVACY_POLICY.md lists it, and editing that document
// reopens the consent gate for everyone.
//
// Same-origin only. The media host's AVIF (lib/image.ts) has the
// same /img/ path prefix, but it is a cross-origin no-cors load:
// an opaque response this rule can never cache, so matching it
// only put the worker in front of every image. Those objects carry
// a year of immutable Cache-Control; the HTTP cache keeps them.
{
urlPattern: ({ url, sameOrigin }: { url: URL; sameOrigin: boolean }) =>
sameOrigin && url.pathname.startsWith("/img/"),
handler: "CacheFirst",
options: {
cacheName: "f2p-img-v1",
expiration: { maxEntries: 800, maxAgeSeconds: 60 * 60 * 24 * 30 },
cacheableResponse: { statuses: [200], headers: { "X-Img-Final": "1" } },
},
},
],
},
devOptions: { enabled: false },
}),
// `npm run analyze` → dist/stats.html treemap. Vite mode instead of an
// env var so it works cross-platform without cross-env.
mode === "analyze" &&
visualizer({
filename: "dist/stats.html",
gzipSize: true,
brotliSize: true,
template: "treemap",
}),
],
// `vite dev` serves the app shell for any unmatched path, so without this
// /api/data/* and /img/* return text/html: the dataset fetch parses HTML as
// JSON and throws, while every image 404s into the shell. Proxying to the
// deployed Worker keeps `npm run dev` working with real data and real images
// while still hot-reloading app code.
//
// This does NOT exercise worker/ source — changes there must be run with
// `wrangler dev`, which serves the Worker and dist/ together.
server: {
proxy: {
"/api": { target: "https://free-steam-games.win", changeOrigin: true },
"/img": { target: "https://free-steam-games.win", changeOrigin: true },
},
},
test: {
// Off unless asked for (`npm run test:coverage`): sonarcloud.yml sends the
// report to SonarCloud. Files no test loads are listed at 0%, as SonarCloud
// counts them anyway. projectRoot "..": the paths in lcov.info are read
// from the repository root, where the scan runs - web/src/..., not src/....
coverage: {
provider: "v8",
include: ["{src,worker,shared,build,scripts,admin/src,admin/build}/**/*.{ts,js,mjs}"],
exclude: ["**/*.test.ts", "**/*.d.ts", "src/lib/testing/**", "worker/testing/**", "worker/generated/**"],
reporter: [["lcovonly", { projectRoot: ".." }], "text-summary"],
},
},
build: {
// Raised from es2020: Svelte 5's compiled output and SvelteKit 2 both
// assume a modern baseline, and the Android floor is already API 30, whose
// System WebView handles ES2022.
target: "es2022",
sourcemap: false,
chunkSizeWarningLimit: 1200,
},
}));