SPB Git

Repository Public

vite.ts 4.1 KB 104 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : KA Maps Engine8 * File             : sdk/web/src/vite.ts9 * Module           : sdk10 * Repository       : https://git.spboucher.ai/ka-maps-engine11 * Created          : 2026-12-0112 * Last Modified    : 2026-12-0113 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: support@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// @ka/maps/vite — greffon Vite : sert les actifs du moteur (`km_wasm_bg.wasm`, `ka-maps-worker.js`)43// sous `/ka-maps/<version>/` en développement et les émet dans le build, sans rien copier à la main.44//45//   import kaMaps from '@ka/maps/vite';46//   export default defineConfig({ plugins: [react(), kaMaps()] });4748import { createReadStream, existsSync, readFileSync, statSync } from 'node:fs';49import { join } from 'node:path';50import { ASSET_FILES, assetsDir, VERSION } from './assets-node.js';5152export interface KaMapsViteOptions {53  /** Chemin public des actifs, relatif à la base du site. Défaut : `ka-maps/<version>`. */54  path?: string;55}5657const TYPES: Record<string, string> = { wasm: 'application/wasm', js: 'text/javascript; charset=utf-8' };5859// Type structurel (le paquet ne dépend pas de Vite) : assignable à `Plugin` de Vite 5, 6 et 7.60/* eslint-disable @typescript-eslint/no-explicit-any */61export interface KaMapsVitePlugin {62  name: string;63  config(): { optimizeDeps: { exclude: string[] } };64  configureServer(server: any): void;65  configurePreviewServer(server: any): void;66  generateBundle(this: any): void;67}6869export default function kaMaps(options: KaMapsViteOptions = {}): KaMapsVitePlugin {70  const sub = (options.path ?? `ka-maps/${VERSION}`).replace(/^\/+|\/+$/g, '');71  const dir = assetsDir();72  const serve = (req: { url?: string }, res: any, next: () => void) => {73    const url = (req.url ?? '').split('?')[0];74    const i = url.indexOf(`/${sub}/`);75    if (i < 0) return next();76    const name = url.slice(i + sub.length + 2);77    if (!(ASSET_FILES as readonly string[]).includes(name)) return next();78    const file = join(dir, name);79    if (!existsSync(file)) return next();80    res.setHeader('Content-Type', TYPES[name.split('.').pop() ?? ''] ?? 'application/octet-stream');81    res.setHeader('Content-Length', String(statSync(file).size));82    res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');83    createReadStream(file).pipe(res);84  };85  return {86    name: 'ka-maps-assets',87    config() {88      // La colle wasm-bindgen est un module ES simple : pas de pré-empaquetage nécessaire.89      return { optimizeDeps: { exclude: ['@ka/maps'] } };90    },91    configureServer(server) {92      server.middlewares.use(serve);93    },94    configurePreviewServer(server) {95      server.middlewares.use(serve);96    },97    generateBundle() {98      for (const f of ASSET_FILES) this.emitFile({ type: 'asset', fileName: `${sub}/${f}`, source: readFileSync(join(dir, f)) });99    },100  };101}102103export { kaMaps };104
Files main
Menu

Theme