|
| 1 | +/** |
| 2 | + * Repackage a web export so Cloudflare Pages can actually serve it. |
| 3 | + * |
| 4 | + * Metro emits every asset that lives inside a dependency under |
| 5 | + * `dist/assets/node_modules/…` — the icon font, Bloom's four `.woff2` faces, |
| 6 | + * expo-router's chrome, and `wa-sqlite.wasm`, which IS the storage engine on |
| 7 | + * web. Wrangler's Pages upload walks the directory and skips any `node_modules` |
| 8 | + * segment outright: measured on this project, 27 of 45 files never left the |
| 9 | + * machine, and a control file in a sibling directory did. |
| 10 | + * |
| 11 | + * Nothing errors when that happens. The SPA rewrite in `public/_redirects` |
| 12 | + * catches every one of those paths and answers `200 text/html`, so the fonts |
| 13 | + * silently fall back, the wasm fetch gets an HTML body, and the deployment |
| 14 | + * looks perfect from the outside. |
| 15 | + * |
| 16 | + * So the tree moves to `assets/vendor/…` — a name the uploader has no opinion |
| 17 | + * about — and `_redirects` rewrites `/assets/node_modules/*` onto it, which |
| 18 | + * leaves the emitted bundles untouched. Then this script proves the result: |
| 19 | + * nothing under a `node_modules` path survives in the output, and every asset |
| 20 | + * path the bundles reference resolves to a real file. |
| 21 | + * |
| 22 | + * Run after `expo export`, on the deploy path only. The Electron build serves |
| 23 | + * the export straight off disk through its own `app://` protocol, with no |
| 24 | + * rewrite engine in front of it, so it must keep the paths Metro emitted. |
| 25 | + */ |
| 26 | + |
| 27 | +import { existsSync, readFileSync, renameSync, statSync } from "node:fs"; |
| 28 | +import { readdir } from "node:fs/promises"; |
| 29 | +import { join, resolve, sep } from "node:path"; |
| 30 | + |
| 31 | +const REFERENCE_PATTERN = /assets\/node_modules\/([A-Za-z0-9@._/-]+)/g; |
| 32 | +const SCANNED_EXTENSIONS = [".js", ".css", ".html", ".json"]; |
| 33 | + |
| 34 | +const distDir = resolve(process.argv[2] ?? "dist"); |
| 35 | +const vendored = join(distDir, "assets", "vendor"); |
| 36 | +const emitted = join(distDir, "assets", "node_modules"); |
| 37 | + |
| 38 | +async function filesUnder(directory: string): Promise<string[]> { |
| 39 | + const entries = await readdir(directory, { withFileTypes: true }); |
| 40 | + const found: string[] = []; |
| 41 | + for (const entry of entries) { |
| 42 | + const path = join(directory, entry.name); |
| 43 | + if (entry.isDirectory()) { |
| 44 | + found.push(...(await filesUnder(path))); |
| 45 | + } else { |
| 46 | + found.push(path); |
| 47 | + } |
| 48 | + } |
| 49 | + return found; |
| 50 | +} |
| 51 | + |
| 52 | +function fail(message: string): never { |
| 53 | + console.error(`pack-pages: ${message}`); |
| 54 | + process.exit(1); |
| 55 | +} |
| 56 | + |
| 57 | +if (!existsSync(join(distDir, "index.html"))) { |
| 58 | + fail(`${distDir} holds no index.html — run \`expo export --platform web\` first.`); |
| 59 | +} |
| 60 | + |
| 61 | +if (existsSync(emitted)) { |
| 62 | + if (existsSync(vendored)) { |
| 63 | + fail( |
| 64 | + `both ${emitted} and ${vendored} exist — a previous run was interrupted, ` + |
| 65 | + "so delete the export and rebuild rather than merging two trees.", |
| 66 | + ); |
| 67 | + } |
| 68 | + renameSync(emitted, vendored); |
| 69 | +} else if (!existsSync(vendored)) { |
| 70 | + fail( |
| 71 | + "the export carries no dependency assets at all, under either name — the " + |
| 72 | + "asset layout changed, and this script's assumption with it.", |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +const files = await filesUnder(distDir); |
| 77 | + |
| 78 | +const stragglers = files.filter((file) => |
| 79 | + file.slice(distDir.length).split(sep).includes("node_modules"), |
| 80 | +); |
| 81 | +if (stragglers.length > 0) { |
| 82 | + fail( |
| 83 | + `${stragglers.length} file(s) still sit under a node_modules path and would ` + |
| 84 | + `never be uploaded, starting with ${stragglers[0]}`, |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +const referenced = new Set<string>(); |
| 89 | +for (const file of files) { |
| 90 | + if (!SCANNED_EXTENSIONS.some((extension) => file.endsWith(extension))) continue; |
| 91 | + const contents = readFileSync(file, "utf8"); |
| 92 | + for (const match of contents.matchAll(REFERENCE_PATTERN)) { |
| 93 | + referenced.add(match[1]); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +if (referenced.size === 0) { |
| 98 | + fail( |
| 99 | + "no bundle references an `assets/node_modules/` path — either the export " + |
| 100 | + "stopped emitting them (making this step and the rewrite rule dead) or the " + |
| 101 | + "reference format changed, which would make this check vacuous.", |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +const unresolved = [...referenced].filter((relative) => { |
| 106 | + const target = join(vendored, relative); |
| 107 | + return !existsSync(target) || !statSync(target).isFile(); |
| 108 | +}); |
| 109 | +if (unresolved.length > 0) { |
| 110 | + fail( |
| 111 | + `${unresolved.length} referenced asset(s) have no file under assets/vendor, ` + |
| 112 | + `starting with ${unresolved[0]}`, |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +const redirects = join(distDir, "_redirects"); |
| 117 | +if (!existsSync(redirects)) { |
| 118 | + fail("_redirects is missing from the export — the rewrite cannot be in force."); |
| 119 | +} |
| 120 | +const rules = readFileSync(redirects, "utf8") |
| 121 | + .split("\n") |
| 122 | + .map((line) => line.trim()) |
| 123 | + .filter((line) => line.length > 0 && !line.startsWith("#")); |
| 124 | +const rewriteIndex = rules.findIndex((rule) => |
| 125 | + /^\/assets\/node_modules\/\*\s+\/assets\/vendor\/:splat\s+200$/.test(rule), |
| 126 | +); |
| 127 | +const catchAllIndex = rules.findIndex((rule) => |
| 128 | + /^\/\*\s+\/index\.html\s+200$/.test(rule), |
| 129 | +); |
| 130 | +if (rewriteIndex === -1) { |
| 131 | + fail("_redirects carries no /assets/node_modules/* -> /assets/vendor/:splat rewrite."); |
| 132 | +} |
| 133 | +if (catchAllIndex !== -1 && catchAllIndex < rewriteIndex) { |
| 134 | + fail( |
| 135 | + "the SPA catch-all precedes the asset rewrite in _redirects, so it wins and " + |
| 136 | + "every dependency asset answers with index.html.", |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +console.log( |
| 141 | + `pack-pages: ${referenced.size} referenced dependency assets resolve under ` + |
| 142 | + `assets/vendor, across ${files.length} files.`, |
| 143 | +); |
0 commit comments