chunk-vite-node-debug.536c4c5b.mjs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { existsSync, promises } from 'fs';
  2. import { a as resolve, p as picocolors, j as join } from './chunk-constants.71e8a211.mjs';
  3. import 'tty';
  4. import 'url';
  5. import 'path';
  6. function hashCode(s) {
  7. return s.split("").reduce((a, b) => {
  8. a = (a << 5) - a + b.charCodeAt(0);
  9. return a & a;
  10. }, 0);
  11. }
  12. class Debugger {
  13. constructor(root, options) {
  14. this.options = options;
  15. this.externalizeMap = /* @__PURE__ */ new Map();
  16. if (options.dumpModules)
  17. this.dumpDir = resolve(root, options.dumpModules === true ? ".vite-node/dump" : options.dumpModules);
  18. if (this.dumpDir) {
  19. if (options.loadDumppedModules)
  20. console.info(picocolors.exports.gray(`[vite-node] [debug] load modules from ${this.dumpDir}`));
  21. else
  22. console.info(picocolors.exports.gray(`[vite-node] [debug] dump modules to ${this.dumpDir}`));
  23. }
  24. this.initPromise = this.clearDump();
  25. }
  26. async clearDump() {
  27. if (!this.dumpDir)
  28. return;
  29. if (!this.options.loadDumppedModules && existsSync(this.dumpDir))
  30. await promises.rm(this.dumpDir, { recursive: true, force: true });
  31. await promises.mkdir(this.dumpDir, { recursive: true });
  32. }
  33. encodeId(id) {
  34. return `${id.replace(/[^\w@_-]/g, "_").replace(/_+/g, "_")}-${hashCode(id)}.js`;
  35. }
  36. async recordExternalize(id, path) {
  37. if (!this.dumpDir)
  38. return;
  39. this.externalizeMap.set(id, path);
  40. await this.writeInfo();
  41. }
  42. async dumpFile(id, result) {
  43. if (!result || !this.dumpDir)
  44. return;
  45. await this.initPromise;
  46. const name = this.encodeId(id);
  47. return await promises.writeFile(join(this.dumpDir, name), `// ${id.replace(/\0/g, "\\0")}
  48. ${result.code}`, "utf-8");
  49. }
  50. async loadDump(id) {
  51. if (!this.dumpDir)
  52. return null;
  53. await this.initPromise;
  54. const name = this.encodeId(id);
  55. const path = join(this.dumpDir, name);
  56. if (!existsSync(path))
  57. return null;
  58. const code = await promises.readFile(path, "utf-8");
  59. return {
  60. code: code.replace(/^\/\/.*?\n/, ""),
  61. map: void 0
  62. };
  63. }
  64. async writeInfo() {
  65. if (!this.dumpDir)
  66. return;
  67. const info = JSON.stringify({
  68. time: new Date().toLocaleString(),
  69. externalize: Object.fromEntries(this.externalizeMap.entries())
  70. }, null, 2);
  71. return promises.writeFile(join(this.dumpDir, "info.json"), info, "utf-8");
  72. }
  73. }
  74. export { Debugger };