103 lines
3.7 KiB
JavaScript
103 lines
3.7 KiB
JavaScript
import { cpSync, existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { createRequire } from 'node:module'
|
|
import { dirname, join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
import { compileEarthRuntime } from '../../frontend/scripts/compile-earth-runtime.mjs'
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
const desktopRoot = join(__dirname, '..')
|
|
const repoRoot = join(desktopRoot, '..')
|
|
const earthSourceDir = join(repoRoot, 'frontend/public/earth')
|
|
const bundleEarthDir = join(desktopRoot, '.earth-bundle/earth')
|
|
const require = createRequire(join(desktopRoot, 'package.json'))
|
|
|
|
const VENDOR_PACKAGES = [
|
|
{ id: 'three', outFile: 'three.mjs', resolvePath: 'three/build/three.module.js' },
|
|
{ id: 'pmtiles', outFile: 'pmtiles.mjs' },
|
|
{ id: '@mapbox/vector-tile', outFile: 'mapbox-vector-tile.mjs' },
|
|
{ id: 'pbf', outFile: 'pbf.mjs' },
|
|
{ id: 'simplex-noise', outFile: 'simplex-noise.mjs' },
|
|
{ id: 'satellite.js', outFile: 'satellite-js.mjs', resolvePath: 'satellite.js/dist/satellite.es.js' },
|
|
{ id: 'hls.js', outFile: 'hls.mjs', resolvePath: 'hls.js/dist/hls.mjs' },
|
|
{ id: 'astronomy-engine', outFile: 'astronomy-engine.mjs' },
|
|
]
|
|
|
|
rmSync(bundleEarthDir, { recursive: true, force: true })
|
|
mkdirSync(join(bundleEarthDir, 'js'), { recursive: true })
|
|
mkdirSync(join(bundleEarthDir, 'vendor'), { recursive: true })
|
|
|
|
const importMapOverrides = {}
|
|
|
|
for (const pkg of VENDOR_PACKAGES) {
|
|
const entry = fileURLToPath(import.meta.resolve(pkg.resolvePath || pkg.id))
|
|
const result = await Bun.build({
|
|
entrypoints: [entry],
|
|
target: 'browser',
|
|
format: 'esm',
|
|
minify: true,
|
|
})
|
|
if (!result.success) {
|
|
throw new AggregateError(result.logs, `Failed to bundle vendor package: ${pkg.id}`)
|
|
}
|
|
const source = await result.outputs[0].text()
|
|
writeFileSync(join(bundleEarthDir, 'vendor', pkg.outFile), source, 'utf8')
|
|
importMapOverrides[pkg.id] = `./vendor/${pkg.outFile}`
|
|
}
|
|
|
|
const { assets, html } = await compileEarthRuntime({
|
|
earthDir: earthSourceDir,
|
|
importMapOverrides,
|
|
})
|
|
|
|
writeFileSync(join(bundleEarthDir, 'index.html'), html, 'utf8')
|
|
for (const [name, asset] of assets) {
|
|
writeFileSync(join(bundleEarthDir, 'js', name), asset.source, 'utf8')
|
|
}
|
|
|
|
const workerEntrypoints = [
|
|
join(earthSourceDir, 'js/satellite-position-worker.js'),
|
|
join(earthSourceDir, 'js/surface-line-lod-worker.js'),
|
|
]
|
|
const workerBuild = await Bun.build({
|
|
entrypoints: workerEntrypoints,
|
|
outdir: join(bundleEarthDir, 'js'),
|
|
target: 'browser',
|
|
format: 'esm',
|
|
minify: true,
|
|
naming: '[name].js',
|
|
})
|
|
if (!workerBuild.success) {
|
|
throw new AggregateError(workerBuild.logs, 'Failed to bundle Earth Web Workers')
|
|
}
|
|
|
|
for (const folder of ['css', 'assets', 'data']) {
|
|
const srcFolder = join(earthSourceDir, folder)
|
|
if (existsSync(srcFolder)) {
|
|
cpSync(srcFolder, join(bundleEarthDir, folder), { recursive: true })
|
|
}
|
|
}
|
|
|
|
const zjuEmblemSrc = join(desktopRoot, 'resources/zju-emblem.png')
|
|
if (existsSync(zjuEmblemSrc)) {
|
|
const brandOutDir = join(bundleEarthDir, 'assets/brand')
|
|
mkdirSync(brandOutDir, { recursive: true })
|
|
cpSync(zjuEmblemSrc, join(brandOutDir, 'zju-emblem.png'))
|
|
}
|
|
|
|
const mediaPipePkgDir = dirname(require.resolve('@mediapipe/tasks-vision/package.json'))
|
|
const mediaPipeOutDir = join(bundleEarthDir, 'vendor/mediapipe')
|
|
mkdirSync(mediaPipeOutDir, { recursive: true })
|
|
for (const candidate of ['vision_bundle.mjs', 'vision_bundle.cjs']) {
|
|
const srcFile = join(mediaPipePkgDir, candidate)
|
|
if (existsSync(srcFile)) {
|
|
cpSync(srcFile, join(mediaPipeOutDir, candidate))
|
|
}
|
|
}
|
|
const mediaPipeWasmDir = join(mediaPipePkgDir, 'wasm')
|
|
if (existsSync(mediaPipeWasmDir)) {
|
|
cpSync(mediaPipeWasmDir, join(mediaPipeOutDir, 'wasm'), { recursive: true })
|
|
}
|
|
|
|
console.log(`Built offline Earth bundle at ${bundleEarthDir}`)
|