pwaLUPMIS2/vite.config.js
ekke adbf9ba211 Merge the Shapefile export fix and the startup-loading fix
Two branches from parallel sessions, merged in that order because the first
removes shp-write and with it JSZip, which the second was working around.

Conflicts resolved by hand in vite.config.js: the JSZip chunk rule the second
branch added is dropped, since nothing reaches JSZip any more — shapefiles are
written by src/shapefile-write.js and shpjs 6 unzips with but-unzip. Vite's
preload-helper rule stays, which is what keeps the geotiff chunk out of the
startup preload. dist/ was rebuilt from scratch rather than merged, its hashed
file names being a conflict on every file.

Service Worker v26 now records both changes.

Verified on the merged tree: a production build preloads only the entry,
vite-helpers, bootstrap, openlayers and ol-ext — geotiff, shpjs and pako are
gone from startup, and Analyse still fetches its chunk and turf on first use.
Shapefile export, driven through the Export GIS modal, writes a zip holding the
point and polygon sets with their .prj and .cpg.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-23 13:48:02 +02:00

95 lines
3.8 KiB
JavaScript

/**
* Vite Configuration
*
* Configures:
* - Cross-origin isolation headers (required for OPFS/SQLite)
* - Worker bundling
*/
import { defineConfig } from 'vite';
export default defineConfig({
// Development server configuration
server: {
headers: {
// Required for OPFS (Origin Private File System) access
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
},
},
// Preview server (npm run preview) configuration
preview: {
headers: {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
},
},
// Build configuration
build: {
// Generate source maps for debugging
sourcemap: true,
// Target modern browsers that support OPFS
target: 'esnext',
// Raise the chunk-size warning threshold.
// Rationale: the two largest chunks are unavoidable —
// • openlayers (~535 kB / ~152 kB gzipped) — the OL library itself,
// used app-wide; further splitting just creates more HTTP round-trips
// • sqlite3.wasm (~856 kB) — a runtime WASM binary, not a JS chunk;
// code-splitting does not apply
// 900 kB silences the noise without hiding genuine regressions.
chunkSizeWarningLimit: 900,
rollupOptions: {
output: {
manualChunks(id) {
// pako (zlib) is pulled in by BOTH geotiff and jspdf (via fast-png).
// Without its own chunk, Rollup assigns it to one of them and the two
// chunks reference each other ("Circular chunk: jspdf -> geotiff").
if (id.includes('node_modules/pako/')) return 'pako';
// Vite's preload helper (the `__vitePreload` used by every dynamic
// import()) is a virtual module with no node_modules path, so Rollup
// is free to park it in whichever chunk it likes — it used to land in
// the geotiff chunk. The entry needs the helper at startup, so that
// made the browser preload all ~257 kB of geotiff.js on every visit,
// defeating the lazy rule below. Give the helper a chunk of its own
// (~1 kB) so the entry pulls in only that.
if (id.includes('vite/preload-helper')) return 'vite-helpers';
// Turf.js — only needed when the Analysis panel is opened, which
// main.js imports dynamically. Keep it out of the eager bundle.
if (id.includes('node_modules/@turf/')) return 'turf';
// geotiff.js + the OL modules that use it — only needed when the user
// opens a Cloud-Optimized GeoTIFF layer, which MapView.addCOGLayer()
// imports dynamically. Must be tested BEFORE the generic ol/ rule so
// it isn't swallowed into the eagerly-loaded openlayers chunk.
if (id.includes('node_modules/geotiff/') ||
id.includes('node_modules/ol/source/GeoTIFF') ||
id.includes('node_modules/ol/layer/WebGLTile')) return 'geotiff';
if (id.includes('node_modules/ol/')) return 'openlayers';
if (id.includes('node_modules/ol-ext/')) return 'ol-ext';
if (id.includes('node_modules/bootstrap/')) return 'bootstrap';
// shpjs — dynamically imported at runtime (main.js), so this chunk is
// only fetched when the user imports a shapefile. Writing shapefiles
// needs no dependency at all (src/shapefile-write.js), and shpjs 6
// unzips with but-unzip, so nothing here reaches JSZip any more.
if (id.includes('node_modules/shpjs/')) return 'shpjs';
if (id.includes('node_modules/jspdf')) return 'jspdf';
},
},
},
},
// Worker configuration
worker: {
format: 'es',
},
// Dependency optimization
optimizeDeps: {
exclude: ['sqlocal'],
},
});