import fs from 'node:fs' import path from 'node:path' /** * Vite 插件:把 Cesium 的预构建静态资源挂到 `/cesium/`(票 27)。 * * 为什么不用 `vite-plugin-cesium`(决策地图票 06 的调研结论):社区封装会改 index.html 并注入 * 脚本,与本仓库的 Fes HTML 注入、`@originjs/vite-plugin-federation`、qiankun 注入冲突。 * 这里只做官方 npm 配方必需的两件事: * · dev / pre.dev / pre.dev.one → 中间件把 `cesium/*` 映射到 node_modules 里的预构建目录 * (`pre.dev.one` 关掉了 `copyPublicDir`,走 public 目录那条路不通); * · build → 把 Workers / ThirdParty / Assets / Widgets 四个目录拷到产物 `cesium/`。 * * 运行时用 `CESIUM_BASE_URL`(见 services/cesiumAssets.js)拼这些目录的地址; * Worker 也是从这里按 `Workers/<模块>.js` 加载的,不能省。 */ // 只搬这四个目录:Build/Cesium 根下还有 Cesium.js / index.js / index.cjs 三个入口包, // 我们是 ESM 打包引入,不需要它们,搬过去只是白占体积。 const STATIC_DIRS = ['Workers', 'ThirdParty', 'Assets', 'Widgets'] const MIME = { '.js': 'text/javascript; charset=utf-8', '.mjs': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8', '.json': 'application/json; charset=utf-8', '.wasm': 'application/wasm', '.xml': 'application/xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml', '.ktx2': 'image/ktx2', '.glb': 'model/gltf-binary', '.bin': 'application/octet-stream', } /** 把请求路径解析成源目录内的真实文件;越界、非白名单目录、目录、不存在一律返回 null。 */ function resolveStaticFile(sourceDir, requestUrl) { const clean = decodeURIComponent((requestUrl || '').split('?')[0].split('#')[0]) const relative = path.posix.normalize(clean).replace(/^\/+/, '') if (!relative || relative.startsWith('..')) return null const [top] = relative.split('/') if (!STATIC_DIRS.includes(top)) return null const file = path.join(sourceDir, relative) if (!file.startsWith(sourceDir + path.sep)) return null try { return fs.statSync(file).isFile() ? file : null } catch { return null } } /** * @param {object} [options] * @param {string} [options.sourceDir] 相对项目根的 Cesium 预构建目录 * @param {string} [options.targetDir] 产物与请求路径下的目录名 */ export default function cesiumStatic({ sourceDir = 'node_modules/cesium/Build/Cesium', targetDir = 'cesium' } = {}) { let root = process.cwd() let outDir = '' let command = 'serve' const absoluteSource = () => path.resolve(root, sourceDir) return { name: 'tvt:cesium-static', configResolved(config) { root = config.root || root command = config.command const resolved = (config.build && config.build.outDir) || 'dist' outDir = path.isAbsolute(resolved) ? resolved : path.resolve(root, resolved) }, configureServer(server) { const dir = absoluteSource() server.middlewares.use(`/${targetDir}`, (req, res, next) => { const file = resolveStaticFile(dir, req.url) if (!file) return next() res.setHeader('Content-Type', MIME[path.extname(file).toLowerCase()] || 'application/octet-stream') res.setHeader('Cache-Control', 'no-cache') fs.createReadStream(file).pipe(res) }) }, // dev 关闭服务器时 Vite 也会调这个钩子,只认构建。 closeBundle() { if (command !== 'build') return const from = absoluteSource() if (!fs.existsSync(from)) { this.warn(`Cesium 预构建静态资源不存在:${from}(先安装 cesium 依赖)`) return } const to = path.join(outDir || path.resolve(root, 'dist'), targetDir) for (const dir of STATIC_DIRS) { const source = path.join(from, dir) if (!fs.existsSync(source)) { this.warn(`Cesium 静态资源目录缺失:${source}`) continue } fs.cpSync(source, path.join(to, dir), { recursive: true }) console.log(`✔ Copied: ${source} → ${path.join(to, dir)}`) } }, } }