Files
ukmesh/frontend/vite.config.ts

120 lines
6.1 KiB
TypeScript

import { defaultClientConditions, defineConfig, transformWithOxc } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'node:path';
import fs from 'node:fs';
import { prepareMapLibreSource } from './src/plugins/maplibre-source.js';
import { deduplicateLegalComments } from './src/plugins/legal-comments.js';
const mapLibreSource = prepareMapLibreSource();
import viteSeoPlugin from './src/plugins/vite-seo.js';
const productionTarget = process.env.VITE_BUILD_TARGET || (
process.env.VITE_SITE === 'ukmesh' && process.env.VITE_NETWORK === 'ukmesh' ? 'app'
: process.env.VITE_SITE === 'dev' || process.env.VITE_NETWORK === 'test' ? 'dev-website' : 'public-website'
);
const localApiTarget = process.env.UKMESH_DEV_API_TARGET || 'http://localhost:3000';
export default defineConfig(({ command }) => ({
define: command === 'build' ? { 'import.meta.env.VITE_BUILD_TARGET': JSON.stringify(productionTarget) } : {},
plugins: [{
name: 'maplibre-upstream-typescript-semantics', enforce: 'pre',
async transform(code, id) {
const filename = id.split('?', 1)[0];
if (!filename.startsWith(mapLibreSource) || !filename.endsWith('.ts')) return;
return transformWithOxc(code, filename, {
lang: 'ts', target: 'es2020', tsconfig: false,
assumptions: { setPublicClassFields: true },
typescript: { removeClassFieldsWithoutInitializer: true },
});
},
}, {
name: 'distinct-chunk-legal-notices',
apply: 'build',
renderChunk: { order: 'post', handler(code, chunk) {
const compact = deduplicateLegalComments(code, chunk.fileName);
return compact === null ? null : { code: compact, map: null };
} },
}, react(), viteSeoPlugin(), {
name: 'maplibre-shared-module-worker',
apply: 'build',
resolveId(id) { if (id === 'virtual:maplibre-worker-url') return '\0maplibre-worker-url'; },
load(id) {
if (id !== '\0maplibre-worker-url') return;
const entry = path.join(mapLibreSource, 'src/source/worker.ts');
const reference = this.emitFile({ type: 'chunk', id: entry, name: 'maplibre-gl-worker' });
return `export default import.meta.ROLLUP_FILE_URL_${reference};`;
},
generateBundle() {
this.emitFile({ type: 'asset', fileName: 'build-target.json', source: JSON.stringify({ target: productionTarget }) });
this.emitFile({ type: 'asset', fileName: 'assets/maplibre-LICENSE.txt', source: fs.readFileSync(path.resolve('node_modules/maplibre-gl/LICENSE.txt'), 'utf8') });
},
}, {
name: 'maplibre-worker-dev', apply: 'serve',
resolveId(id) { if (id === 'virtual:maplibre-worker-url') return '\0maplibre-worker-dev'; },
load(id) { if (id === '\0maplibre-worker-dev') return 'import url from "maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url";export default url;'; },
}],
// Production shares the renderer/worker source graph. Development uses the
// upstream ESM bundle to avoid hundreds of cold source-module requests.
resolve: {
// MapLibre and its deck overlays use WebGL2. Select the upstream 9.4
// build that omits WebGPU branches while preserving the same public API.
conditions: ['visgl:webgl-only', ...defaultClientConditions],
alias: command === 'build'
? [{ find: /^maplibre-gl$/, replacement: path.join(mapLibreSource, 'src/index.ts') }]
: [] },
optimizeDeps: {
// maplibre-gl v6 loads its worker via new URL(..., import.meta.url) —
// vite's dep pre-bundling rewrites that to node_modules/.vite/deps/maplibre-gl-worker.mjs
// which does not exist, killing the worker (and custom raster protocols) in dev.
exclude: ['maplibre-gl'],
// The excluded upstream source graph reaches CommonJS packages directly.
// Prebundle these in dev so their default exports have the required interop.
include: ['murmurhash-js', 'earcut', '@maplibre/mlt'],
},
server: {
proxy: {
'/api': localApiTarget,
'/ws': { target: localApiTarget.replace(/^http/, 'ws'), ws: true },
},
},
build: {
// Bound worker memory on local machines while parallelizing compression.
minify: 'terser',
terserOptions: {
maxWorkers: 2, module: true, ecma: 2020,
compress: { passes: 2 },
format: { comments: /@license|@preserve|^!/ },
},
manifest: true,
rollupOptions: {
output: {
// Keep Rolldown's compact final code generation after parallel Terser.
minify: true,
comments: { legal: true },
entryFileNames: 'assets/[name]-[hash].js',
chunkFileNames: 'assets/[name]-[hash].js',
assetFileNames: 'assets/[name]-[hash].[ext]',
codeSplitting: {
includeDependenciesRecursively: false,
groups: [
{ name: 'map-math', test: /\/node_modules\/(?:gl-matrix|earcut|@mapbox|pbf|quickselect|tinyqueue|kdbush|@maplibre)\//, priority: 50, includeDependenciesRecursively: true },
// maplibre 6.19's map state imports zustand/vanilla at module scope, and
// the module worker shares the renderer chunk graph. Keep the vanilla
// store in a chunk of its own: folded into the lazily imported
// `components/Map/maplibre.ts` facade it creates a facade<->core cycle
// that runs setWorkerUrl() before `config` exists, and folded into the
// React chunk it drags the app entry (and its `document` access) into
// the worker.
{ name: 'zustand', test: /\/node_modules\/zustand\/(?:esm\/)?vanilla\./, priority: 30 },
{ name: 'react', test: /\/node_modules\/(?:react|react-dom)\//, priority: 40 },
{ name: 'maplibre', test: /ukmesh-maplibre-source/, priority: 20 },
{ name: 'deck', test: /\/node_modules\/(?:@deck\.gl|@luma\.gl|@math\.gl|@loaders\.gl)\//, priority: 18, includeDependenciesRecursively: true },
{ name: 'recharts', test: /\/node_modules\/recharts\//, priority: 15, includeDependenciesRecursively: true },
{ name: 'vite-preload', test: /vite\/preload-helper/, priority: 100 },
{ name: 'react', test: /\/node_modules\/(?:react|react-dom)\//, priority: 40 },
],
},
},
},
},
}));