worker.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { a as resolve, c as distDir } from './chunk-constants.71e8a211.mjs';
  2. import { c as createBirpc, M as ModuleCacheMap } from './chunk-vite-node-client.da0a17ff.mjs';
  3. import { workerId } from 'tinypool';
  4. import { g as getWorkerState } from './chunk-mock-date.2917be60.mjs';
  5. import { e as executeInViteNode } from './chunk-runtime-mocker.4bbb070f.mjs';
  6. import { r as rpc } from './chunk-runtime-rpc.00a890d2.mjs';
  7. import 'tty';
  8. import 'url';
  9. import 'path';
  10. import 'module';
  11. import 'vm';
  12. import './chunk-vite-node-utils.473cd0b2.mjs';
  13. import 'fs';
  14. import 'assert';
  15. import 'util';
  16. import 'debug';
  17. import 'local-pkg';
  18. import 'vite';
  19. import './chunk-utils-timers.b48455ed.mjs';
  20. let _viteNode;
  21. const moduleCache = new ModuleCacheMap();
  22. const mockMap = /* @__PURE__ */ new Map();
  23. async function startViteNode(ctx) {
  24. if (_viteNode)
  25. return _viteNode;
  26. const processExit = process.exit;
  27. process.on("beforeExit", (code) => {
  28. rpc().onWorkerExit(code);
  29. });
  30. process.exit = (code = process.exitCode || 0) => {
  31. rpc().onWorkerExit(code);
  32. return processExit(code);
  33. };
  34. process.on("unhandledRejection", (err) => {
  35. rpc().onUnhandledRejection(err);
  36. });
  37. const { config } = ctx;
  38. const { run: run2 } = (await executeInViteNode({
  39. files: [
  40. resolve(distDir, "entry.mjs")
  41. ],
  42. fetchModule(id) {
  43. return rpc().fetch(id);
  44. },
  45. resolveId(id, importer) {
  46. return rpc().resolveId(id, importer);
  47. },
  48. moduleCache,
  49. mockMap,
  50. interopDefault: config.deps.interopDefault ?? true,
  51. root: config.root,
  52. base: config.base
  53. }))[0];
  54. _viteNode = { run: run2 };
  55. return _viteNode;
  56. }
  57. function init(ctx) {
  58. if (typeof __vitest_worker__ !== "undefined" && ctx.config.threads && ctx.config.isolate)
  59. throw new Error(`worker for ${ctx.files.join(",")} already initialized by ${getWorkerState().ctx.files.join(",")}. This is probably an internal bug of Vitest.`);
  60. const { config, port, workerId: workerId$1 } = ctx;
  61. process.env.VITEST_WORKER_ID = String(workerId$1);
  62. process.env.VITEST_POOL_ID = String(workerId);
  63. globalThis.__vitest_worker__ = {
  64. ctx,
  65. moduleCache,
  66. config,
  67. mockMap,
  68. rpc: createBirpc(
  69. {},
  70. {
  71. eventNames: ["onUserConsoleLog", "onFinished", "onCollected", "onWorkerExit"],
  72. post(v) {
  73. port.postMessage(v);
  74. },
  75. on(fn) {
  76. port.addListener("message", fn);
  77. }
  78. }
  79. )
  80. };
  81. if (ctx.invalidates) {
  82. ctx.invalidates.forEach((fsPath) => {
  83. moduleCache.delete(fsPath);
  84. moduleCache.delete(`mock:${fsPath}`);
  85. });
  86. }
  87. ctx.files.forEach((i) => moduleCache.delete(i));
  88. }
  89. async function run(ctx) {
  90. init(ctx);
  91. const { run: run2 } = await startViteNode(ctx);
  92. return run2(ctx.files, ctx.config);
  93. }
  94. export { run };