|
| 1 | +import { cpSync, createReadStream, existsSync, statSync } from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +import type { Plugin, ResolvedConfig } from "vite"; |
| 5 | + |
| 6 | +const MIME_TYPES: Record<string, string> = { |
| 7 | + ".js": "application/javascript", |
| 8 | + ".mjs": "application/javascript", |
| 9 | + ".json": "application/json", |
| 10 | + ".wasm": "application/wasm", |
| 11 | + ".png": "image/png", |
| 12 | + ".jpg": "image/jpeg", |
| 13 | + ".gif": "image/gif", |
| 14 | + ".svg": "image/svg+xml", |
| 15 | + ".xml": "application/xml", |
| 16 | + ".glb": "model/gltf-binary", |
| 17 | + ".bin": "application/octet-stream" |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * Serves Cesium Workers, Assets, and ThirdParty from the terriajs-cesium |
| 22 | + * package and terriajs wwwroot static assets during dev mode. |
| 23 | + * |
| 24 | + * At runtime, Cesium resolves assets relative to `cesiumBaseUrl` which is |
| 25 | + * set to `build/TerriaJS/build/Cesium/build/` by the app's index.js. |
| 26 | + */ |
| 27 | +export function cesiumPlugin(options: { |
| 28 | + terriaJSBasePath: string; |
| 29 | + cesiumDir: string; |
| 30 | +}): Plugin { |
| 31 | + const { terriaJSBasePath, cesiumDir } = options; |
| 32 | + const terriaJSWwwroot = path.join(terriaJSBasePath, "wwwroot"); |
| 33 | + let outDir = ""; |
| 34 | + |
| 35 | + return { |
| 36 | + name: "cesium-assets", |
| 37 | + |
| 38 | + configResolved(config: ResolvedConfig) { |
| 39 | + outDir = path.resolve(config.root, config.build.outDir); |
| 40 | + }, |
| 41 | + |
| 42 | + closeBundle() { |
| 43 | + // Copy terriajs wwwroot → outDir/TerriaJS/ (mirrors gulp copy-terriajs-assets) |
| 44 | + const destPath = path.join(outDir, "TerriaJS"); |
| 45 | + cpSync(terriaJSWwwroot, destPath, { recursive: true }); |
| 46 | + }, |
| 47 | + |
| 48 | + configureServer(server) { |
| 49 | + // Serve terriajs wwwroot at /build/TerriaJS/ |
| 50 | + // This mirrors the gulp `copy-terriajs-assets` task |
| 51 | + server.middlewares.use((req, res, next) => { |
| 52 | + if (!req.url) return next(); |
| 53 | + |
| 54 | + const url = decodeURIComponent(req.url.split("?")[0]); |
| 55 | + |
| 56 | + // Serve terriajs wwwroot assets at /build/TerriaJS/ |
| 57 | + if (url.startsWith("/build/TerriaJS/")) { |
| 58 | + const assetPath = url.slice("/build/TerriaJS/".length); |
| 59 | + const filePath = path.join(terriaJSWwwroot, assetPath); |
| 60 | + return serveFile(filePath, res, next); |
| 61 | + } |
| 62 | + |
| 63 | + // Serve WASM with correct MIME type |
| 64 | + if (url.endsWith(".wasm")) { |
| 65 | + const wasmPath = url.startsWith("/build/") |
| 66 | + ? path.join(terriaJSWwwroot, url.slice(1)) |
| 67 | + : null; |
| 68 | + if (wasmPath && existsSync(wasmPath)) { |
| 69 | + return serveFile(wasmPath, res, next); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + next(); |
| 74 | + }); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + function serveFile( |
| 79 | + filePath: string, |
| 80 | + res: import("http").ServerResponse, |
| 81 | + next: () => void |
| 82 | + ) { |
| 83 | + if (!existsSync(filePath) || statSync(filePath).isDirectory()) { |
| 84 | + return next(); |
| 85 | + } |
| 86 | + const ext = path.extname(filePath); |
| 87 | + const mime = MIME_TYPES[ext] || "application/octet-stream"; |
| 88 | + res.setHeader("Content-Type", mime); |
| 89 | + res.setHeader("Cache-Control", "no-cache"); |
| 90 | + createReadStream(filePath).pipe(res); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Strip Cesium debug pragmas in production builds. |
| 96 | + * Removes code between //>>includeStart('debug') and //>>includeEnd('debug'). |
| 97 | + */ |
| 98 | +export function cesiumDebugStripPlugin(cesiumDir: string): Plugin { |
| 99 | + const pragmaRegex = |
| 100 | + /\/\/>>includeStart\('debug', pragmas\.debug\);?[^]*?\/\/>>includeEnd\('debug'\);?/g; |
| 101 | + |
| 102 | + return { |
| 103 | + name: "cesium-strip-debug", |
| 104 | + apply: "build", |
| 105 | + transform(code, id) { |
| 106 | + if (!id.endsWith(".js") || !id.includes(cesiumDir)) return; |
| 107 | + return { code: code.replace(pragmaRegex, ""), map: null }; |
| 108 | + } |
| 109 | + }; |
| 110 | +} |
0 commit comments