feat(frontend): produire un bundle web distinct en transport http (#74 F1)
Le transport est figé au build par Vite : un seul `dist` ne peut pas servir
à la fois le desktop (IPC Tauri) et le navigateur (HTTP+WS). Le serveur
embarqué servait donc un bundle desktop, d'où `__TAURI_INTERNALS__ is
undefined` côté navigateur.
- `vite.config.ts` devient une factory `({ mode })` : le mode par défaut émet
le bundle desktop (`dist`), `--mode web` lit `.env.web` et émet le bundle
navigateur (`dist-web`).
- `.env.web` porte `VITE_TRANSPORT=http` dans un fichier de mode plutôt qu'en
préfixe de commande, syntaxe qui n'existe pas sous Windows (bundle NSIS).
- `transport.ts` extrait le prédicat `transportFromEnv()`, partagé par l'app et
la config de build : le constant `__IDEA_TRANSPORT__` et le transport résolu
dérivent de la même variable via le même prédicat, ils ne peuvent pas diverger.
- `main.tsx` publie `__IDEA_TRANSPORT__` sur `window` : l'affectation est un
effet de bord, elle survit à la minification et rend un bundle identifiable
sans grep d'un symbole minifié. Les deux jeux d'adapters étant présents dans
les deux bundles, leur présence ne prouve rien.
Le chemin desktop est inchangé : le web reste opt-in.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@ -16,6 +16,7 @@ import {
|
||||
shouldUseMock,
|
||||
shouldUseHttp,
|
||||
} from "./di";
|
||||
import { transportFromEnv } from "./transport";
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
@ -66,6 +67,28 @@ describe("resolveTransport / web (HTTP+WS) selection (ticket #13, F1)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("transportFromEnv (ticket #74, F1)", () => {
|
||||
// The build config derives `__IDEA_TRANSPORT__` from this same predicate, so
|
||||
// pinning it here pins both the runtime transport and the build constant.
|
||||
it("selects http only for the exact opt-in value", () => {
|
||||
expect(transportFromEnv("http")).toBe("http");
|
||||
});
|
||||
|
||||
it.each([undefined, "", "tauri", "HTTP", "http ", "web"])(
|
||||
"falls back to tauri for %o",
|
||||
(value) => {
|
||||
expect(transportFromEnv(value)).toBe("tauri");
|
||||
},
|
||||
);
|
||||
|
||||
it("agrees with shouldUseHttp for the value the build reads", () => {
|
||||
vi.stubEnv("VITE_TRANSPORT", "http");
|
||||
expect(shouldUseHttp()).toBe(transportFromEnv("http") === "http");
|
||||
vi.stubEnv("VITE_TRANSPORT", "");
|
||||
expect(shouldUseHttp()).toBe(transportFromEnv("") === "http");
|
||||
});
|
||||
});
|
||||
|
||||
describe("DIProvider / useGateways", () => {
|
||||
it("provides explicit gateways to consumers", () => {
|
||||
const gateways = resolveGatewaysMock();
|
||||
|
||||
@ -18,11 +18,11 @@ import type { Gateways } from "@/ports";
|
||||
import { createTauriGateways } from "@/adapters";
|
||||
import { createMockGateways } from "@/adapters/mock";
|
||||
import { createHttpWsGateways } from "@/adapters/http";
|
||||
import { transportFromEnv, type Transport } from "./transport";
|
||||
|
||||
const GatewaysContext = createContext<Gateways | null>(null);
|
||||
|
||||
/** The selected transport backing the gateways. */
|
||||
export type Transport = "mock" | "http" | "tauri";
|
||||
export type { Transport };
|
||||
|
||||
/** Whether the mock adapters should be used (env-driven, overridable in tests). */
|
||||
export function shouldUseMock(): boolean {
|
||||
@ -36,14 +36,13 @@ export function shouldUseMock(): boolean {
|
||||
* and is never affected (ticket #13, lot F1).
|
||||
*/
|
||||
export function shouldUseHttp(): boolean {
|
||||
return import.meta.env.VITE_TRANSPORT === "http";
|
||||
return transportFromEnv(import.meta.env.VITE_TRANSPORT) === "http";
|
||||
}
|
||||
|
||||
/** Resolves which transport to use. Mock wins, then explicit web, else Tauri. */
|
||||
export function resolveTransport(): Transport {
|
||||
if (shouldUseMock()) return "mock";
|
||||
if (shouldUseHttp()) return "http";
|
||||
return "tauri";
|
||||
return transportFromEnv(import.meta.env.VITE_TRANSPORT);
|
||||
}
|
||||
|
||||
/** Resolves the gateway set for the current environment. */
|
||||
|
||||
@ -19,6 +19,14 @@ if (!root) {
|
||||
// follows the main window's focused project; otherwise the full app.
|
||||
const viewParams = parseViewWindowParams(window.location.search);
|
||||
|
||||
// Ticket #74, F1: publish the transport this bundle was built for. `define`
|
||||
// inlines it as a literal, and assigning it is a side effect, so it survives
|
||||
// minification and tree-shaking — a built bundle can be identified (by QA, or
|
||||
// from devtools) without grepping for a minified symbol. Both adapter sets ship
|
||||
// in either bundle, so their mere presence proves nothing.
|
||||
(window as unknown as { __IDEA_TRANSPORT__?: string }).__IDEA_TRANSPORT__ =
|
||||
__IDEA_TRANSPORT__;
|
||||
|
||||
// Ticket #13, F2: in web (HTTP) transport mode the browser client renders the
|
||||
// pairing-gated, read-only `WebApp`. Desktop (Tauri) is unchanged — it renders
|
||||
// the full `App` (or a detached view window). Detached windows are desktop-only.
|
||||
|
||||
19
frontend/src/app/transport.ts
Normal file
19
frontend/src/app/transport.ts
Normal file
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* The transport signal, shared by the app and the Vite build config.
|
||||
*
|
||||
* `vite.config.ts` imports {@link transportFromEnv} to compute the
|
||||
* `__IDEA_TRANSPORT__` build constant from the very same `VITE_TRANSPORT`
|
||||
* variable that {@link shouldUseHttp} reads (ticket #74, F1). One predicate,
|
||||
* one variable: the greppable constant and the resolved transport cannot drift.
|
||||
*/
|
||||
|
||||
/** The selected transport backing the gateways. */
|
||||
export type Transport = "mock" | "http" | "tauri";
|
||||
|
||||
/**
|
||||
* The transport a raw `VITE_TRANSPORT` value selects, mock aside. Web is opt-in
|
||||
* (`"http"`) so the desktop path stays the default and is never affected.
|
||||
*/
|
||||
export function transportFromEnv(value: string | undefined): "http" | "tauri" {
|
||||
return value === "http" ? "http" : "tauri";
|
||||
}
|
||||
12
frontend/src/vite-env.d.ts
vendored
12
frontend/src/vite-env.d.ts
vendored
@ -9,3 +9,15 @@ interface ImportMetaEnv {
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv;
|
||||
}
|
||||
|
||||
/**
|
||||
* The transport this bundle was built for, inlined as a literal by Vite
|
||||
* `define` (ticket #74, F1). It is derived in `vite.config.ts` from the same
|
||||
* `VITE_TRANSPORT` variable and the same `transportFromEnv()` predicate that
|
||||
* `resolveTransport()` uses, so it always agrees with the adapters actually
|
||||
* wired in — mock aside, which is a separate `VITE_USE_MOCK` switch.
|
||||
*
|
||||
* `main.tsx` publishes it on `window`, which makes it survive minification and
|
||||
* lets a built bundle be identified without grepping for a minified symbol.
|
||||
*/
|
||||
declare const __IDEA_TRANSPORT__: "http" | "tauri";
|
||||
|
||||
Reference in New Issue
Block a user