Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269const path = require('node:path')const fs = require('node:fs')const {execFileSync} = require('node:child_process')
const projectRoot = path.join(__dirname, '..')const distDir = path.join(projectRoot, 'dist')const bskywebStatic = path.join(projectRoot, 'bskyweb', 'static')const templatesDir = path.join(projectRoot, 'bskyweb', 'templates')const templateFile = path.join(templatesDir, 'scripts.html')const fontsTemplateFile = path.join(templatesDir, 'fonts.html')
// Parse dist/index.html for entrypoint scripts and stylesheets injected by Metro.// Metro injects <link rel="preload/stylesheet"> and <script> tags referencing// /_expo/static/ paths, optionally prefixed by `experiments.baseUrl` (e.g. /static).// We capture from /_expo/static/ onward so the captured path is independent of baseUrl.//// We preserve the _expo/static/ structure when copying to bskyweb/static, because// Metro's runtime chunk loader and source maps embed these paths. Rewriting them// would mean rewriting the chunk loader and every .map file's references; not worth it.const indexHtml = fs.readFileSync(path.join(distDir, 'index.html'), 'utf8')
// Find all CSS stylesheet links pointing to _expo (skips preload links).const cssEntries = []const cssRegex = /<link\b[^>]*href="[^"]*?(\/_expo\/static\/css\/[^"]+)"[^>]*>/glet matchwhile ((match = cssRegex.exec(indexHtml)) !== null) { if (match[0].includes('rel="stylesheet"')) { cssEntries.push(match[1]) }}
// Find all script src entries pointing to _expoconst jsEntries = []const jsRegex = /<script\b[^>]*src="[^"]*?(\/_expo\/static\/js\/[^"]+)"[^>]*>/gwhile ((match = jsRegex.exec(indexHtml)) !== null) { jsEntries.push(match[1])}
if (jsEntries.length === 0) { // Fail loudly: an empty scripts.html silently breaks the deployed app. throw new Error( 'No JS entrypoints found in dist/index.html. ' + 'Metro may have changed its output format; update post-web-build.js.', )}
console.log(`Found ${jsEntries.length} script entrypoints`)console.log(`Found ${cssEntries.length} CSS entrypoints`)
// Generate scripts.html template.// Map /_expo/static/... to {{ staticCDNHost }}/static/_expo/static/... so the// path matches what we copy into bskyweb/static below.const outputLines = []
for (const href of cssEntries) { const cdnPath = href.replace(/^\//, '{{ staticCDNHost }}/static/') outputLines.push(`<link rel="stylesheet" href="${cdnPath}">`)}
for (const src of jsEntries) { const cdnPath = src.replace(/^\//, '{{ staticCDNHost }}/static/') outputLines.push(`<script defer="defer" src="${cdnPath}"></script>`)}
console.log(`Writing ${templateFile}`)fs.writeFileSync(templateFile, outputLines.join('\n'))
// Generate fonts.html — preload + @font-face for the splash, using the same// content-hashed paths Metro emits for the bundle. Avoids shipping a duplicate// font copy under /static/media/ and ensures the preload is actually used.const fontsRoot = '/assets/assets/fonts/'const splashFonts = [ { family: 'InterVariable', file: 'inter/InterVariable.woff2', format: 'woff2', weight: '300 1000', style: 'normal', preload: true, }, { family: 'InterVariableItalic', file: 'inter/InterVariable-Italic.woff2', format: 'woff2', weight: '300 1000', style: 'italic', }, { family: 'BasteB', file: 'baste_b/BasteB-Regular.woff2', format: 'woff2', weight: 'normal', style: 'normal', }, { family: 'AzeretMonoVariable', file: 'azeret_mono/AzeretMono-VariableFont_wght.ttf', format: 'truetype', weight: '100 900', style: 'normal', }, { family: 'RubikVariable', file: 'rubik/Rubik-VariableFont_wght.ttf', format: 'truetype', weight: '100 900', style: 'normal', },]
function findHashedFont(file) { const dir = path.join( distDir, 'assets', 'assets', 'fonts', path.dirname(file), ) const ext = path.extname(file) const prefix = `${path.basename(file, ext)}.` const hashed = fs.existsSync(dir) && fs .readdirSync(dir) .find(name => name.startsWith(prefix) && name.endsWith(ext)) if (!hashed) { throw new Error( `Could not find ${file} in ${dir}. ` + 'Update post-web-build.js if the font emit path changed.', ) } return `${fontsRoot}${path.dirname(file)}/${hashed}`}
for (const font of splashFonts) { font.hashedPath = findHashedFont(font.file)}
/* * The static index.html references the fonts by their unhashed names, but * Metro emits them content-hashed. Rewrite dist/index.html in place so the * font preload and @font-face URLs resolve when serving dist/ directly. */const fixedIndexHtml = splashFonts.reduce( (html, font) => html.replaceAll(`${fontsRoot}${font.file}`, font.hashedPath), indexHtml,)if (fixedIndexHtml !== indexHtml) { console.log('Rewriting font paths in dist/index.html to hashed filenames') fs.writeFileSync(path.join(distDir, 'index.html'), fixedIndexHtml)}
const cdnFontPath = font => `{{ staticCDNHost }}/static${font.hashedPath}`
const fontsHtml = `${splashFonts .filter(font => font.preload) .map( font => `<link rel="preload" as="font" type="font/${font.format}" href="${cdnFontPath(font)}" crossorigin>`, ) .join('\n')}<style>${splashFonts .map( font => ` @font-face { font-family: '${font.family}'; src: url("${cdnFontPath(font)}") format('${font.format}'); font-weight: ${font.weight}; font-style: ${font.style}; font-display: swap; }`, ) .join('\n')}</style>`console.log(`Writing ${fontsTemplateFile}`)fs.writeFileSync(fontsTemplateFile, fontsHtml)
// Clean previous build output to avoid stale filesfunction cleanDir(dir) { if (fs.existsSync(dir)) { fs.rmSync(dir, {recursive: true}) console.log(`Cleaned ${dir}`) }}
cleanDir(path.join(bskywebStatic, '_expo'))cleanDir(path.join(bskywebStatic, 'assets'))
// Recursively copy a directory.function copyDir(sourceDir, targetDir) { if (!fs.existsSync(sourceDir)) { console.log(`Skipping ${sourceDir} (does not exist)`) return } fs.mkdirSync(targetDir, {recursive: true}) const entries = fs.readdirSync(sourceDir, {withFileTypes: true}) for (const entry of entries) { const sourcePath = path.join(sourceDir, entry.name) const targetPath = path.join(targetDir, entry.name) if (entry.isDirectory()) { copyDir(sourcePath, targetPath) } else { fs.copyFileSync(sourcePath, targetPath) console.log(`Copied ${sourcePath} to ${targetPath}`) } }}
// Copy Metro's _expo/ tree wholesale so JS chunks (including lazy chunks// referenced by hash) and source maps resolve at the same paths the runtime expects.copyDir(path.join(distDir, '_expo'), path.join(bskywebStatic, '_expo'))
// Copy assets (fonts, images, icons, etc.) referenced as /static/assets/... by the bundle.copyDir(path.join(distDir, 'assets'), path.join(bskywebStatic, 'assets'))
// Upload source maps to Sentryif ( process.env.SENTRY_AUTH_TOKEN && process.env.SENTRY_AUTH_TOKEN != 'unknown') { const releasePackage = 'community.blacksky.app' const release = process.env.SENTRY_RELEASE || `${releasePackage}@${require(path.join(projectRoot, 'package.json')).version}` /* * The runtime reports `community.blacksky.app@` + (EXPO_PUBLIC_RELEASE_VERSION * || package.json version) (SENTRY_RELEASE in src/env/common.ts), so maps * uploaded under a bogus release never match incoming events. "null" and * "undefined" catch `jq -r '.version'` stringifying a missing package.json * version field, and the empty case catches an unset * EXPO_PUBLIC_RELEASE_VERSION in the Dockerfile. */ const version = release.startsWith(`${releasePackage}@`) ? release.slice(releasePackage.length + 1) : '' if (!version || version === 'null' || version === 'undefined') { throw new Error( `Refusing to upload source maps under invalid Sentry release ${JSON.stringify(release)}. ` + `Set SENTRY_RELEASE to "${releasePackage}@<version>" or restore the "version" field in package.json.`, ) } const dist = process.env.SENTRY_DIST || undefined const sourceMapDir = path.join(distDir, '_expo', 'static')
console.log(`Uploading source maps to Sentry (release: ${release})`) const args = [ 'sourcemaps', 'upload', '--org', 'blacksky-algorithms', '--project', 'social-app', '--release', release, ...(dist ? ['--dist', dist] : []), sourceMapDir, ] try { execFileSync('sentry-cli', args, {stdio: 'inherit', cwd: projectRoot}) console.log('Sentry source map upload complete') } catch (e) { console.error('Sentry source map upload failed:', e.message) process.exit(1) }} else { console.log('Skipping Sentry source map upload (SENTRY_AUTH_TOKEN not set)')}