pwaLUPMIS2/vite.config.js
ekke 8d1d874ad7 Shapefile export: write the format ourselves instead of shp-write
Choosing Shapefile in the Export GIS modal produced no file. shp-write@0.3.2
cannot run in this build, in three separate ways: its dbf dependency assigns an
undeclared variable, which was fine in the browserify bundle it was written for
but is a ReferenceError under the strict mode ES modules always use; its zip.js
branches on process.browser, which does not exist in a browser; and its
download.js delivers the archive by navigating to a data:application/zip URL,
which Chromium blocks in the top frame.

- src/shapefile-write.js writes .shp/.shx/.dbf/.prj/.cpg itself: geometries
  split by type into up to three layers, polygon rings re-wound clockwise as
  the format requires, DBF column types inferred per column, names sanitised
  to DBF's 10 characters and de-duplicated, UTF-8 declared via .cpg.
- src/zip-store.js holds the STORED-ZIP writer that was inside analysis/xlsx.js,
  so both writers share it. Pure-ASCII archives are byte-for-byte as before.
- shp-write, and with it dbf and JSZip 2.5.0, leave the dependency tree; the
  build no longer warns that node's "assert" was externalised.
- Service Worker v26 for the new bundle.

Verified in a browser: the Export GIS modal, driven through its own controls
with Web Mercator features, produced area_analysis.zip holding both layers with
WGS84 coordinates, file code 9994 and the right shape types. The archive reads
back through the application's own shpjs importer with geometries, numeric,
text and boolean attributes intact. The zonal-statistics .xlsx export, which now
shares the zip writer, still opens and reads correctly.

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

86 lines
3.1 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';
// 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, so this chunk is only
// fetched when the user imports a shapefile. (Writing shapefiles
// needs no dependency at all; see src/shapefile-write.js.)
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'],
},
});