Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PWGVn3BQ1T1H7gCv5B89d1
122 lines
5.9 KiB
TypeScript
122 lines
5.9 KiB
TypeScript
import { defineConfig, build as construire, type Plugin } from 'vite';
|
|
import { execSync } from 'node:child_process';
|
|
import { readFileSync } from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { lireEssences, lireFiches, lireCycle, lirePlanches, lireSystemes, lireCarte, verifierDocuments, verifierCadragesCarte, verifierDocumentsSystemes, lireRessources, lierRessources, lireParcelles } from './scripts/contenu.ts';
|
|
import { lireEpingles } from './xl/epingles.ts';
|
|
|
|
// Marqueur de build : hash git court, injecté dans la page et dans /build.json.
|
|
// Un HTTP 200 ne valide rien ; le marqueur, si.
|
|
function hashGit(): string {
|
|
try {
|
|
return execSync('git rev-parse --short HEAD', { stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim();
|
|
} catch {
|
|
return 'sans-git';
|
|
}
|
|
}
|
|
|
|
const BUILD_HASH = process.env.QUARTIER_BUILD_HASH ?? hashGit();
|
|
const ici = (p: string) => fileURLToPath(new URL(p, import.meta.url));
|
|
|
|
// Le contenu que Jules édite (content/*.md) devient un module : `import { CHAPITRES, ESSENCES, CYCLE, PLANCHES, SYSTEMES, CARTE, PARCELLES } from 'virtual:contenu'`.
|
|
// Une faute de saisie arrête le build avec la ligne à corriger (scripts/contenu.ts).
|
|
function contenu(): Plugin {
|
|
const ID = 'virtual:contenu';
|
|
const fichiers = [ici('./content/fiches.md'), ici('./content/essences.md'), ici('./content/cycle-eau.md'), ici('./content/planches.md'), ici('./maquette/documents.json'), ici('./content/systemes.md'), ici('./maquette/carte.json'), ici('./content/carte.md'), ici('./content/ressources.md'), ici('./content/parcelles.md'), ici('./data/scene2035.json')];
|
|
return {
|
|
name: 'quartier-contenu',
|
|
resolveId: (id) => (id === ID ? '\0' + ID : null),
|
|
load(id) {
|
|
if (id !== '\0' + ID) return null;
|
|
fichiers.forEach((f) => this.addWatchFile(f));
|
|
const CHAPITRES = lireFiches(readFileSync(fichiers[0]!, 'utf8'));
|
|
// lot ⑤ : les ressources des fiches, et les parcelles (une ligne par maison du plan)
|
|
lierRessources(CHAPITRES, lireRessources(readFileSync(fichiers[8]!, 'utf8'), CHAPITRES));
|
|
const PARCELLES = lireParcelles(readFileSync(fichiers[9]!, 'utf8'), CHAPITRES, JSON.parse(readFileSync(fichiers[10]!, 'utf8')).maisons);
|
|
const ESSENCES = lireEssences(readFileSync(fichiers[1]!, 'utf8'));
|
|
const CYCLE = lireCycle(readFileSync(fichiers[2]!, 'utf8'));
|
|
const PLANCHES = lirePlanches(readFileSync(fichiers[3]!, 'utf8'), JSON.parse(readFileSync(fichiers[4]!, 'utf8')));
|
|
const SYSTEMES = lireSystemes(readFileSync(fichiers[5]!, 'utf8'), JSON.parse(readFileSync(fichiers[6]!, 'utf8')).p2);
|
|
const CARTE = lireCarte(readFileSync(fichiers[7]!, 'utf8'), PLANCHES);
|
|
verifierDocuments(CHAPITRES, PLANCHES);
|
|
verifierCadragesCarte(CHAPITRES, CARTE);
|
|
verifierDocumentsSystemes(SYSTEMES, PLANCHES);
|
|
return `export const CHAPITRES = ${JSON.stringify(CHAPITRES)};\nexport const ESSENCES = ${JSON.stringify(ESSENCES)};\nexport const CYCLE = ${JSON.stringify(CYCLE)};\nexport const PLANCHES = ${JSON.stringify(PLANCHES)};\nexport const SYSTEMES = ${JSON.stringify(SYSTEMES)};\nexport const CARTE = ${JSON.stringify(CARTE)};\nexport const PARCELLES = ${JSON.stringify(PARCELLES)};`;
|
|
},
|
|
handleHotUpdate(ctx) {
|
|
if (fichiers.some((f) => ctx.file.replace(/\\/g, '/') === f.replace(/\\/g, '/'))) {
|
|
const m = ctx.server.moduleGraph.getModuleById('\0' + ID);
|
|
if (m) ctx.server.moduleGraph.invalidateModule(m);
|
|
ctx.server.ws.send({ type: 'full-reload' });
|
|
return [];
|
|
}
|
|
return undefined;
|
|
},
|
|
};
|
|
}
|
|
|
|
// Seconde entrée : le prototype XL (lot ⑥a, xl-proto.html, liée depuis aucune page). Bâtie dans une passe à part,
|
|
// après la démo : deux entrées HTML dans la même passe font sortir le polyfill modulepreload en morceau partagé,
|
|
// et l'entrée de la démo change (mesuré le 06/11 : 292,31 → 291,65 kB, une requête de plus). Ainsi, maplibre-gl
|
|
// et pmtiles n'entrent que dans dist/xl-proto.html et la démo reste identique à l'octet près.
|
|
function entreeXl(): Plugin {
|
|
let sortie = 'dist';
|
|
return {
|
|
name: 'quartier-entree-xl',
|
|
apply: 'build',
|
|
// checkpoint du 06/11 : une faute dans la table des épingles arrête le build avec la ligne à corriger (AGENTS : le contenu)
|
|
buildStart() {
|
|
lireEpingles(readFileSync(ici('./content/xl-epingles.md'), 'utf8'));
|
|
},
|
|
// la passe XL écrit au même endroit que la démo (vite build --outDir)
|
|
configResolved(c) {
|
|
sortie = c.build.outDir;
|
|
},
|
|
async closeBundle() {
|
|
await construire({
|
|
configFile: false,
|
|
root: ici('./'),
|
|
base: './',
|
|
publicDir: false,
|
|
logLevel: 'warn',
|
|
define: { __BUILD_HASH__: JSON.stringify(BUILD_HASH) },
|
|
plugins: [{ name: 'quartier-xl-hash', transformIndexHtml: (html) => html.replace('__BUILD_HASH__', BUILD_HASH) }],
|
|
build: { outDir: sortie, target: 'es2022', sourcemap: false, emptyOutDir: false, assetsInlineLimit: 0, chunkSizeWarningLimit: 1200, rollupOptions: { input: { 'xl-proto': ici('./xl-proto.html') } } },
|
|
});
|
|
},
|
|
};
|
|
}
|
|
|
|
export default defineConfig({
|
|
// chemins relatifs : la même sortie sert sur quartier.trans-former.fr et en page autonome (lien privé de secours)
|
|
base: './',
|
|
define: { __BUILD_HASH__: JSON.stringify(BUILD_HASH) },
|
|
build: {
|
|
target: 'es2022',
|
|
sourcemap: false,
|
|
// Budget V1 : < 5 Mo transférés en démo, < 1,5 Mo en ambiance (ARCHITECTURE §1 bis).
|
|
chunkSizeWarningLimit: 900,
|
|
assetsInlineLimit: 0,
|
|
rollupOptions: {
|
|
output: {
|
|
manualChunks: { three: ['three'] },
|
|
},
|
|
},
|
|
},
|
|
plugins: [
|
|
contenu(),
|
|
entreeXl(),
|
|
{
|
|
name: 'quartier-build-json',
|
|
transformIndexHtml: (html) => html.replace('__BUILD_HASH__', BUILD_HASH),
|
|
generateBundle() {
|
|
this.emitFile({
|
|
type: 'asset',
|
|
fileName: 'build.json',
|
|
source: JSON.stringify({ hash: BUILD_HASH, date: new Date().toISOString() }),
|
|
});
|
|
},
|
|
},
|
|
],
|
|
});
|