#!/usr/bin/env node import { readdir, realpath, unlink } from "node:fs/promises"; import { isAbsolute, join, relative, resolve } from "node:path"; const workspace = await realpath(process.cwd()); const distDir = resolve(workspace, process.env.NEXT_DIST_DIR?.trim() || ".next"); function requireInsideWorkspace(path) { const location = relative(workspace, path); if (!location || location === ".." || location.startsWith(`..${process.platform === "win32" ? "\\" : "/"}`) || isAbsolute(location)) { throw new Error("Source map cleanup requires a build directory inside the workspace."); } } requireInsideWorkspace(distDir); let staticDir; try { staticDir = await realpath(join(distDir, "static")); } catch (error) { if (error.code !== "ENOENT") throw error; } let removed = 0; async function clean(directory) { for (const entry of await readdir(directory, { withFileTypes: true })) { const path = join(directory, entry.name); if (entry.isDirectory()) { await clean(path); } else if (entry.isFile() && entry.name.endsWith(".map")) { await unlink(path); removed++; } } } if (staticDir) { requireInsideWorkspace(staticDir); await clean(staticDir); } console.log(`Removed ${removed} public source map files.`);