remove-sourcemaps.mjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env node
  2. /**
  3. * 清除 src/ 下所有 .ts/.tsx 文件中的 //# sourceMappingURL= 行
  4. * 用法: node scripts/remove-sourcemaps.mjs [--dry-run]
  5. */
  6. import { readdir, readFile, writeFile } from "fs/promises";
  7. import { join, extname } from "path";
  8. const SRC_DIR = new URL("../src", import.meta.url).pathname;
  9. const DRY_RUN = process.argv.includes("--dry-run");
  10. const EXTENSIONS = new Set([".ts", ".tsx"]);
  11. const PATTERN = /^\s*\/\/# sourceMappingURL=.*$/gm;
  12. async function* walk(dir) {
  13. for (const entry of await readdir(dir, { withFileTypes: true })) {
  14. const full = join(dir, entry.name);
  15. if (entry.isDirectory()) {
  16. yield* walk(full);
  17. } else if (EXTENSIONS.has(extname(entry.name))) {
  18. yield full;
  19. }
  20. }
  21. }
  22. let total = 0;
  23. for await (const file of walk(SRC_DIR)) {
  24. const content = await readFile(file, "utf8");
  25. if (!PATTERN.test(content)) continue;
  26. // reset lastIndex after test
  27. PATTERN.lastIndex = 0;
  28. const cleaned = content.replace(PATTERN, "").replace(/\n{3,}/g, "\n\n");
  29. if (DRY_RUN) {
  30. console.log(`[dry-run] ${file}`);
  31. } else {
  32. await writeFile(file, cleaned, "utf8");
  33. }
  34. total++;
  35. }
  36. console.log(`\n${DRY_RUN ? "[dry-run] " : ""}Processed ${total} files.`);