Files
wehub-resource-sync e30e75b5d4
Code Quality / Oxlint + Oxfmt (push) Waiting to run
Code Quality / Template Sync (push) Waiting to run
Code Quality / Build Changed Packages (push) Waiting to run
Code Quality / Test Changed Packages (push) Waiting to run
Deploy Expo Example / Deploy Production (push) Waiting to run
Deploy Ink Example / Deploy Production (push) Waiting to run
Python Tests / pytest (assistant-stream, 3.10) (push) Waiting to run
Python Tests / pytest (assistant-stream, 3.12) (push) Waiting to run
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Waiting to run
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Waiting to run
Deploy Shadcn Registry / Deploy Production (push) Waiting to run
Template Metrics / LOC + Bundle Size (push) Waiting to run
Changesets / Create Version PR (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:40:13 +08:00

79 lines
2.4 KiB
JavaScript

/**
* Post-export script that flattens deeply nested pnpm asset paths
* in the Expo static export output. This fixes 404s on Vercel where
* the __node_modules paths are too long or get blocked.
*
* It copies asset files to /assets/<filename> and rewrites the JS
* bundle references accordingly.
*/
import fs from "node:fs";
import path from "node:path";
const DIST = path.resolve(import.meta.dirname, "../dist");
const ASSETS_DIR = path.join(DIST, "assets");
const NODE_MODULES_DIR = path.join(ASSETS_DIR, "__node_modules");
if (!fs.existsSync(NODE_MODULES_DIR)) {
console.log("No __node_modules assets to flatten.");
process.exit(0);
}
// 1. Collect all asset files from __node_modules
const assetFiles = [];
function walk(dir) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
walk(full);
} else {
assetFiles.push(full);
}
}
}
walk(NODE_MODULES_DIR);
// 2. Copy each asset to /assets/<filename> and build a replacement map
const replacements = new Map();
for (const file of assetFiles) {
const filename = path.basename(file);
const relOld = `/assets/${path.relative(ASSETS_DIR, file)}`;
const relNew = `/assets/${filename}`;
const dest = path.join(ASSETS_DIR, filename);
if (!fs.existsSync(dest)) {
fs.copyFileSync(file, dest);
}
replacements.set(relOld, relNew);
}
// 3. Rewrite references in JS bundles and HTML files
function rewriteFiles(dir, extensions) {
if (!fs.existsSync(dir)) return;
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
rewriteFiles(full, extensions);
} else if (extensions.some((ext) => entry.name.endsWith(ext))) {
let content = fs.readFileSync(full, "utf8");
let changed = false;
for (const [oldPath, newPath] of replacements) {
if (content.includes(oldPath)) {
content = content.replaceAll(oldPath, newPath);
changed = true;
}
}
if (changed) {
fs.writeFileSync(full, content);
console.log(`Rewrote asset paths in ${path.relative(DIST, full)}`);
}
}
}
}
rewriteFiles(DIST, [".js", ".html", ".css"]);
// 4. Clean up __node_modules directory
fs.rmSync(NODE_MODULES_DIR, { recursive: true, force: true });
console.log(`Flattened ${replacements.size} assets.`);