import { cpSync, existsSync, mkdirSync, readFileSync } from 'node:fs' import { createRequire } from 'node:module' import { basename, dirname, join, resolve } from 'node:path' import { compileEarthRuntime } from './compile-earth-runtime.mjs' const MEDIAPIPE_MIME_TYPES = { '.js': 'text/javascript; charset=utf-8', '.mjs': 'text/javascript; charset=utf-8', '.wasm': 'application/wasm', } // Earth remains a standalone public page with its pinned import map. Bundle // only project modules, retaining the /earth/js/ base for assets and workers. export function earthRuntimePlugin(root) { const earth = join(root, 'public/earth') const require = createRequire(join(root, 'package.json')) const mediaPipePkgDir = dirname(require.resolve('@mediapipe/tasks-vision/package.json')) let outDir = 'dist' let compilation = null async function compile() { if (compilation) return compilation compilation = compileEarthRuntime({ earthDir: earth }).catch((error) => { compilation = null throw error }) return compilation } return { name: 'earth-runtime', configResolved(config) { outDir = config.build?.outDir || 'dist' }, configureServer(server) { server.watcher.on('change', (file) => { if (file.startsWith(earth) && (file.endsWith('.js') || file === join(earth, 'index.html'))) { compilation = null } }) server.middlewares.use(async (req, res, next) => { const url = (req.url || '').split('?')[0] if (url === '/earth/vendor/mediapipe/vision_bundle.mjs') { const filePath = join(mediaPipePkgDir, 'vision_bundle.mjs') if (!existsSync(filePath)) return next() res.setHeader('Cache-Control', 'no-cache') res.setHeader('Content-Type', 'text/javascript; charset=utf-8') res.end(readFileSync(filePath)) return } if (url.startsWith('/earth/vendor/mediapipe/wasm/')) { const relName = basename(url) const filePath = join(mediaPipePkgDir, 'wasm', relName) if (!relName || !existsSync(filePath)) return next() const ext = relName.slice(relName.lastIndexOf('.')) res.setHeader('Cache-Control', 'no-cache') res.setHeader('Content-Type', MEDIAPIPE_MIME_TYPES[ext] || 'application/octet-stream') res.end(readFileSync(filePath)) return } const isPage = url === '/earth/index.html' || url === '/earth/' const isRuntime = /^\/earth\/js\/runtime-[\w-]+\.js$/.test(url) if (!isPage && !isRuntime) return next() try { const { assets, html } = await compile() const asset = assets.get(basename(url)) if (!isPage && !asset) return next() res.setHeader('Cache-Control', 'no-cache') if (asset) { res.setHeader('ETag', asset.etag) if (req.headers['if-none-match'] === asset.etag) { res.statusCode = 304 res.end() return } } res.setHeader( 'Content-Type', isPage ? 'text/html; charset=utf-8' : 'text/javascript; charset=utf-8', ) res.end(isPage ? html : asset.source) } catch (error) { next(error) } }) }, async generateBundle() { const { assets, html } = await compile() for (const [name, asset] of assets) { this.emitFile({ type: 'asset', fileName: `earth/js/${name}`, source: asset.source }) } this.emitFile({ type: 'asset', fileName: 'earth/index.html', source: html }) }, closeBundle() { const targetMediaPipeDir = resolve(root, outDir, 'earth/vendor/mediapipe') mkdirSync(targetMediaPipeDir, { recursive: true }) const bundleSrc = join(mediaPipePkgDir, 'vision_bundle.mjs') if (existsSync(bundleSrc)) { cpSync(bundleSrc, join(targetMediaPipeDir, 'vision_bundle.mjs')) } const wasmSrcDir = join(mediaPipePkgDir, 'wasm') if (existsSync(wasmSrcDir)) { cpSync(wasmSrcDir, join(targetMediaPipeDir, 'wasm'), { recursive: true }) } }, } }