mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Review of Task 0: - build/initialChunkGuard.js, a Rollup plugin in the browser build, fails the build when an initial chunk (an entry and what it imports statically) holds a module from node_modules/three, @navaramap/* or postprocessing. Checked by forcing it on satellite.js, which the main chunk does hold: the build fails naming the chunk and module. navaraAssets.test now pins both build plugins. - scripts/check-main-chunk.mjs is one script for both main-chunk checks: the composition (text markers in the initial chunks, sizes) and, when scripts/main-chunk-baseline.json exists, the phase-4 branch's gzip-9 budget, with its exports (checkMainChunk, measureMainChunk) so the two branches merge into this file. The composition test moved to src/tooling/mainChunkComposition.test.mjs to leave phase 4's mainChunk.test.mjs its name. - CI runs npm run check:main-chunk after each npm run build (unit, browser-gate and Windows jobs).
143 lines
6.2 KiB
JavaScript
143 lines
6.2 KiB
JavaScript
import { applicationHtmlPlugin } from './application-html.js';
|
|
import cesium from 'vite-plugin-cesium';
|
|
import { DEFAULT_ALLOWED_HOSTS, hostCheckPlugin } from './allowedHosts.js';
|
|
import { embedFramingPlugin } from './embed-framing.js';
|
|
import { panelBuildPlugin } from './panel.js';
|
|
import { navaraChunkFileNames, navaraWorkerAssets } from './navaraAssets.js';
|
|
import { initialChunkGuard } from './initialChunkGuard.js';
|
|
|
|
/**
|
|
* Content-Security-Policy for every document the dev/preview server serves.
|
|
* No inline script is permitted, and the only foreign script origins are the
|
|
* three that the Bhote Koshi event's embedded media needs (YouTube, Facebook
|
|
* and X), which frame-src matches for their players and posts. 'unsafe-eval' is
|
|
* required: Knockout (bundled inside @cesium/widgets) resolves the global
|
|
* object with `(0, eval)("this")` at module load, and without it the Cesium
|
|
* widget never initializes (verified in headless Chrome). It also covers
|
|
* Cesium's WASM decoders. `blob:` is required in the built app: Cesium's
|
|
* bundled workers bootstrap through `importScripts(blob:...)`, which a worker
|
|
* checks against script-src (the dev server loads them by URL instead).
|
|
* Widen any other directive only for a real violation.
|
|
*/
|
|
export const BROWSER_CSP = [
|
|
"default-src 'self'",
|
|
// The Bhote Koshi event's embedded media: the YouTube player API, the
|
|
// Facebook video SDK and X's post widget (src/data/bhoteKoshiEmbeddedMedia.js);
|
|
// and the Maps JavaScript API that Street Level's Google Street View runs on.
|
|
"script-src 'self' 'unsafe-eval' blob: https://www.youtube.com https://connect.facebook.net https://platform.twitter.com https://maps.googleapis.com",
|
|
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
|
|
"font-src 'self' data: https://fonts.gstatic.com",
|
|
"img-src 'self' data: blob: https:",
|
|
"media-src 'self' blob: https:",
|
|
"connect-src 'self' blob: data: https: wss: ws:",
|
|
"worker-src 'self' blob:",
|
|
"child-src 'self' blob:",
|
|
// The same media's players and posts, which load in frames.
|
|
"frame-src 'self' blob: https://www.youtube-nocookie.com https://www.youtube.com https://www.facebook.com https://platform.twitter.com",
|
|
"manifest-src 'self'",
|
|
"object-src 'none'",
|
|
"base-uri 'self'",
|
|
"form-action 'self'",
|
|
"frame-ancestors 'none'",
|
|
].join('; ');
|
|
|
|
/** Response headers shared by the dev and preview servers. */
|
|
export const BROWSER_HEADERS = Object.freeze({
|
|
'X-Frame-Options': 'DENY',
|
|
'Content-Security-Policy': BROWSER_CSP,
|
|
// Responses are only ever used as the type the server declares.
|
|
'X-Content-Type-Options': 'nosniff',
|
|
});
|
|
|
|
/** Build browser assets with explicit inputs; never load environment or providers. */
|
|
export function createBrowserViteConfig({
|
|
plugins = [],
|
|
publicDir,
|
|
googleApiKey,
|
|
cesiumToken,
|
|
mapillaryToken,
|
|
host = 'localhost',
|
|
port = 4173,
|
|
allowedHosts = DEFAULT_ALLOWED_HOSTS,
|
|
command,
|
|
} = {}) {
|
|
return {
|
|
plugins: [
|
|
// First, so no provider route answers a Host the server does not allow.
|
|
hostCheckPlugin(),
|
|
cesium(),
|
|
applicationHtmlPlugin(),
|
|
...plugins,
|
|
embedFramingPlugin(),
|
|
panelBuildPlugin(),
|
|
],
|
|
...(publicDir === undefined ? {} : { publicDir }),
|
|
// A production build must not clean the dependency cache a running dev
|
|
// server is still serving optimized module URLs from.
|
|
...(command === 'build' ? { cacheDir: 'node_modules/.vite-build' } : {}),
|
|
optimizeDeps: {
|
|
// First reached through the SDR worker or a dynamic import. Pre-bundle
|
|
// them at startup so first use cannot invalidate already-transformed
|
|
// URLs with Vite's "Outdated Optimize Dep" 504 response.
|
|
include: [
|
|
'@jtarrio/signals/demod/demodulator.js',
|
|
'@jtarrio/signals/demod/modes.js',
|
|
'@jtarrio/webrtlsdr/rtlsdr.js',
|
|
'egm96-universal',
|
|
// Loaded on demand when the first street-level image opens.
|
|
'mapillary-js',
|
|
],
|
|
// Navara's wasm-bindgen glue loads new URL('x_bg.wasm', import.meta.url)
|
|
// and its workers by URL; both must resolve next to the package's own
|
|
// files, not inside .vite/deps. Dev server only: the experimental
|
|
// Navara engine is a dynamic import outside the Cesium-mode bundle.
|
|
exclude: [
|
|
'@navaramap/three',
|
|
'@navaramap/three-default-plugin',
|
|
'@navaramap/engine',
|
|
'@navaramap/engine-api',
|
|
'@navaramap/engine-worker',
|
|
'@navaramap/engine-font-worker',
|
|
],
|
|
},
|
|
server: {
|
|
host: host || 'localhost',
|
|
port: parseInt(port, 10) || 4173,
|
|
// A wildcard bind only chooses interfaces; it never disables Vite's
|
|
// Host-header check. LAN hostnames are added explicitly (see
|
|
// build/allowedHosts.js); IP addresses are always accepted.
|
|
allowedHosts: [...allowedHosts],
|
|
fs: {
|
|
deny: ['.env', '.env.*', '*.{crt,pem}', '**/.git/**', '**/ENVIRONMENT'],
|
|
},
|
|
// These headers protect the document containing Provider Settings and
|
|
// give the whole page a real Content-Security-Policy (BROWSER_CSP).
|
|
// Embed-mode documents are framable instead: embed-framing.js rewrites
|
|
// only the frame-ancestors directive and keeps the rest of the policy.
|
|
headers: BROWSER_HEADERS,
|
|
},
|
|
// The preview server serves the same documents, so it carries the same
|
|
// framing + CSP hardening (one constant, no drift).
|
|
preview: { headers: BROWSER_HEADERS },
|
|
define: {
|
|
'import.meta.env.GOOGLE_MAPS_API_KEY': JSON.stringify(googleApiKey),
|
|
'import.meta.env.CESIUM_ION_TOKEN': JSON.stringify(cesiumToken),
|
|
// Mapillary client tokens are public by design (see SECURITY.md).
|
|
'import.meta.env.MAPILLARY_CLIENT_TOKEN': JSON.stringify(
|
|
mapillaryToken ?? '',
|
|
),
|
|
},
|
|
build: {
|
|
chunkSizeWarningLimit: 1500,
|
|
// Build only: the wasm Navara's workers load by name (the Navara
|
|
// engine is a separate chunk loaded on ?engine=navara).
|
|
// The lazy chunk with the Navara engine is named navara-engine.
|
|
rollupOptions: {
|
|
// initialChunkGuard: no three/Navara module in the initial chunks.
|
|
plugins: [navaraWorkerAssets(), initialChunkGuard()],
|
|
output: { chunkFileNames: navaraChunkFileNames },
|
|
},
|
|
},
|
|
};
|
|
}
|