@vitest/web-worker 3.2.4 → 4.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021-Present Vitest Team
3
+ Copyright (c) 2021-Present VoidZero Inc. and Vitest contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { defineWebWorkers } from './pure.js';
2
2
  import 'node:worker_threads';
3
- import 'vitest/execute';
4
- import 'node:fs';
3
+ import 'vitest/internal/module-runner';
5
4
  import 'debug';
6
5
 
7
6
  defineWebWorkers();
package/dist/pure.js CHANGED
@@ -1,21 +1,28 @@
1
1
  import { MessageChannel } from 'node:worker_threads';
2
- import { VitestExecutor } from 'vitest/execute';
3
- import { readFileSync as readFileSync$1 } from 'node:fs';
2
+ import { getWorkerState, VitestModuleEvaluator, startVitestModuleRunner, VITEST_VM_CONTEXT_SYMBOL } from 'vitest/internal/module-runner';
4
3
  import createDebug from 'debug';
5
4
 
6
- class InlineWorkerRunner extends VitestExecutor {
7
- constructor(options, context) {
8
- // share the same mocker as main executor
9
- const mocker = globalThis.__vitest_mocker__;
10
- super(options);
11
- this.context = context;
12
- this.mocker = globalThis.__vitest_mocker__ = mocker;
13
- }
14
- prepareContext(context) {
15
- const ctx = super.prepareContext(context);
16
- // not supported for now, we can't synchronously load modules
17
- return Object.assign(ctx, this.context, { importScripts });
18
- }
5
+ function startWebWorkerModuleRunner(context) {
6
+ const state = getWorkerState();
7
+ const mocker = globalThis.__vitest_mocker__;
8
+ const compiledFunctionArgumentsNames = Object.keys(context);
9
+ const compiledFunctionArgumentsValues = Object.values(context);
10
+ compiledFunctionArgumentsNames.push("importScripts");
11
+ compiledFunctionArgumentsValues.push(importScripts);
12
+ const vm = globalThis[VITEST_VM_CONTEXT_SYMBOL];
13
+ const evaluator = new VitestModuleEvaluator(vm, {
14
+ interopDefault: state.config.deps.interopDefault,
15
+ moduleExecutionInfo: state.moduleExecutionInfo,
16
+ getCurrentTestFilepath: () => state.filepath,
17
+ compiledFunctionArgumentsNames,
18
+ compiledFunctionArgumentsValues
19
+ });
20
+ return startVitestModuleRunner({
21
+ evaluator,
22
+ evaluatedModules: state.evaluatedModules,
23
+ mocker,
24
+ state
25
+ });
19
26
  }
20
27
  function importScripts() {
21
28
  throw new Error("[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.");
@@ -297,13 +304,7 @@ var ponyfillStructuredClone = typeof structuredClone === "function" ?
297
304
  ) :
298
305
  (any, options) => deserialize(serialize(any, options));
299
306
 
300
- // keep the reference in case it was mocked
301
- const readFileSync = readFileSync$1;
302
307
  const debug = createDebug("vitest:web-worker");
303
- function getWorkerState() {
304
- // @ts-expect-error untyped global
305
- return globalThis.__vitest_worker__;
306
- }
307
308
  function assertGlobalExists(name) {
308
309
  if (!(name in globalThis)) {
309
310
  throw new Error(`[@vitest/web-worker] Cannot initiate a custom Web Worker. "${name}" is not supported in this environment. Please, consider using jsdom or happy-dom environment.`);
@@ -344,30 +345,6 @@ function createMessageEvent(data, transferOrOptions, clone) {
344
345
  return new MessageEvent("messageerror", { data: error });
345
346
  }
346
347
  }
347
- function getRunnerOptions() {
348
- const state = getWorkerState();
349
- const { config, rpc, moduleCache, moduleExecutionInfo } = state;
350
- return {
351
- async fetchModule(id) {
352
- const result = await rpc.fetch(id, "web");
353
- if (result.id && !result.externalize) {
354
- const code = readFileSync(result.id, "utf-8");
355
- return { code };
356
- }
357
- return result;
358
- },
359
- resolveId(id, importer) {
360
- return rpc.resolveId(id, importer, "web");
361
- },
362
- moduleCache,
363
- moduleExecutionInfo,
364
- interopDefault: config.deps.interopDefault ?? true,
365
- moduleDirectories: config.deps.moduleDirectories,
366
- root: config.root,
367
- base: config.base,
368
- state
369
- };
370
- }
371
348
  function stripProtocol(url) {
372
349
  return url.toString().replace(/^file:\/+/, "/");
373
350
  }
@@ -429,7 +406,6 @@ function convertNodePortToWebPort(port) {
429
406
  return port;
430
407
  }
431
408
  function createSharedWorkerConstructor() {
432
- const runnerOptions = getRunnerOptions();
433
409
  return class SharedWorker extends EventTarget {
434
410
  static __VITEST_WEB_WORKER__ = true;
435
411
  _vw_workerTarget = new EventTarget();
@@ -452,9 +428,6 @@ function createSharedWorkerConstructor() {
452
428
  onrtctransform: null,
453
429
  onunhandledrejection: null,
454
430
  origin: typeof location !== "undefined" ? location.origin : "http://localhost:3000",
455
- importScripts: () => {
456
- throw new Error("[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.");
457
- },
458
431
  crossOriginIsolated: false,
459
432
  onconnect: null,
460
433
  name: name || "",
@@ -484,15 +457,14 @@ function createSharedWorkerConstructor() {
484
457
  this._vw_workerTarget.addEventListener("connect", (e) => {
485
458
  context.onconnect?.(e);
486
459
  });
487
- const runner = new InlineWorkerRunner(runnerOptions, context);
488
- const id = getFileIdFromUrl(url);
489
- this._vw_name = id;
490
- runner.resolveUrl(id).then(([, fsPath]) => {
491
- this._vw_name = name ?? fsPath;
460
+ const fileId = getFileIdFromUrl(url);
461
+ this._vw_name = fileId;
462
+ const runner = startWebWorkerModuleRunner(context);
463
+ runner.mocker.resolveId(fileId).then(({ url, id: resolvedId }) => {
464
+ this._vw_name = name ?? url;
492
465
  debug("initialize shared worker %s", this._vw_name);
493
- return runner.executeFile(fsPath).then(() => {
494
- // worker should be new every time, invalidate its sub dependency
495
- runnerOptions.moduleCache.invalidateSubDepTree([fsPath, runner.mocker.getMockPath(fsPath)]);
466
+ return runner.import(url).then(() => {
467
+ runner._invalidateSubTreeById([resolvedId, runner.mocker.getMockPath(resolvedId)]);
496
468
  this._vw_workerTarget.dispatchEvent(new MessageEvent("connect", { ports: [this._vw_workerPort] }));
497
469
  debug("shared worker %s successfully initialized", this._vw_name);
498
470
  });
@@ -512,7 +484,6 @@ function createSharedWorkerConstructor() {
512
484
  }
513
485
 
514
486
  function createWorkerConstructor(options) {
515
- const runnerOptions = getRunnerOptions();
516
487
  const cloneType = () => options?.clone ?? process.env.VITEST_WEB_WORKER_CLONE ?? "native";
517
488
  return class Worker extends EventTarget {
518
489
  static __VITEST_WEB_WORKER__ = true;
@@ -539,9 +510,6 @@ function createWorkerConstructor(options) {
539
510
  onrtctransform: null,
540
511
  onunhandledrejection: null,
541
512
  origin: typeof location !== "undefined" ? location.origin : "http://localhost:3000",
542
- importScripts: () => {
543
- throw new Error("[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.");
544
- },
545
513
  crossOriginIsolated: false,
546
514
  name: options?.name || "",
547
515
  close: () => this.terminate(),
@@ -584,15 +552,14 @@ function createWorkerConstructor(options) {
584
552
  this.addEventListener("messageerror", (e) => {
585
553
  this.onmessageerror?.(e);
586
554
  });
587
- const runner = new InlineWorkerRunner(runnerOptions, context);
588
- const id = getFileIdFromUrl(url);
589
- this._vw_name = id;
590
- runner.resolveUrl(id).then(([, fsPath]) => {
591
- this._vw_name = options?.name ?? fsPath;
555
+ const fileId = getFileIdFromUrl(url);
556
+ this._vw_name = fileId;
557
+ const runner = startWebWorkerModuleRunner(context);
558
+ runner.mocker.resolveId(fileId).then(({ url, id: resolvedId }) => {
559
+ this._vw_name = options?.name ?? url;
592
560
  debug("initialize worker %s", this._vw_name);
593
- return runner.executeFile(fsPath).then(() => {
594
- // worker should be new every time, invalidate its sub dependency
595
- runnerOptions.moduleCache.invalidateSubDepTree([fsPath, runner.mocker.getMockPath(fsPath)]);
561
+ return runner.import(url).then(() => {
562
+ runner._invalidateSubTreeById([resolvedId, runner.mocker.getMockPath(resolvedId)]);
596
563
  const q = this._vw_messageQueue;
597
564
  this._vw_messageQueue = null;
598
565
  if (q) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/web-worker",
3
3
  "type": "module",
4
- "version": "3.2.4",
4
+ "version": "4.0.0-beta.10",
5
5
  "description": "Web Worker support for testing in Vitest",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -33,7 +33,7 @@
33
33
  "dist"
34
34
  ],
35
35
  "peerDependencies": {
36
- "vitest": "3.2.4"
36
+ "vitest": "4.0.0-beta.10"
37
37
  },
38
38
  "dependencies": {
39
39
  "debug": "^4.4.1"