import fetch from 'node-fetch' import { config } from './config.js' import { getRedisKey, viteManifestToDeps, viteToOxManifest } from './util.js' import { logger } from './logger.js' import * as cache from './cache.js' export async function getViteManifests ({ version }) { const manifests = await cache.get(getRedisKey({ version, name: 'viteManifests' })) if (manifests) return JSON.parse(manifests) await config.load() // vite manifests contains a set of objects with the vite-manifests // from the corresponding registered services const viteManifests = await Promise.all(config.urls.map(async baseUrl => { // fetch the manifests const result = await fetch(new URL('manifest.json', baseUrl)) if (!result.ok) throw new Error(`Failed to load manifest for url ${result.url} (Status: ${result.status}: ${result.statusText})`) try { const manifest = await result.json() for (const file in manifest) { logger.debug(`retrieved ${file} from ${baseUrl}`) manifest[file].meta = manifest[file].meta || {} manifest[file].meta.baseUrl = baseUrl } return manifest } catch (err) { throw new Error(`Failed to load manifest for url ${result.url}: ${err}`) } })) // combine all manifests by keys. With duplicates, last wins const newManifests = viteManifests.reduce((memo, manifest) => Object.assign(memo, manifest), {}) await cache.set(getRedisKey({ version, name: 'viteManifests' }), JSON.stringify(newManifests)) return newManifests } export async function getOxManifests ({ version }) { const manifests = await cache.get(getRedisKey({ version, name: 'oxManifests' })) if (manifests) return JSON.parse(manifests) const viteManifests = await getViteManifests({ version }) const newManifests = viteToOxManifest(viteManifests) await cache.set(getRedisKey({ version, name: 'oxManifests' }), JSON.stringify(newManifests)) return newManifests } export async function getDependencies ({ version }) { const deps = await cache.get(getRedisKey({ version, name: 'dependencies' })) if (deps) return JSON.parse(deps) const viteManifests = await getViteManifests({ version }) const newDeps = viteManifestToDeps(viteManifests) await cache.set(getRedisKey({ version, name: 'dependencies' }), JSON.stringify(newDeps)) return newDeps } export async function getCSSDependenciesFor ({ file, version }) { const allDependencies = await getDependencies({ version }) const dependencies = allDependencies[file] || [] return dependencies.filter(dep => /\.css/i.test(dep)) }