MCPcopy
hub / github.com/vitest-dev/vitest / CommonjsExecutor

Class CommonjsExecutor

packages/vitest/src/runtime/vm/commonjs-executor.ts:23–403  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21const requiresCache = new WeakMap<NodeJS.Module, NodeJS.Require>()
22
23export class CommonjsExecutor {
24 private context: vm.Context
25 private requireCache = new Map<string, NodeJS.Module>()
26 private publicRequireCache = this.createProxyCache()
27
28 private moduleCache = new Map<string, VMSyntheticModule>()
29 private builtinCache: Record<string, NodeJS.Module> = Object.create(null)
30 private extensions: Record<
31 string,
32 (m: NodeJS.Module, filename: string) => unknown
33 > = Object.create(null)
34
35 private fs: FileMap
36 private Module: typeof _Module
37 private interopDefault: boolean | undefined
38
39 constructor(options: CommonjsExecutorOptions) {
40 this.context = options.context
41 this.fs = options.fileMap
42 this.interopDefault = options.interopDefault
43
44 const primitives = vm.runInContext(
45 '({ Object, Array, Error })',
46 this.context,
47 ) as {
48 Object: typeof Object
49 Array: typeof Array
50 Error: typeof Error
51 }
52
53 // eslint-disable-next-line ts/no-this-alias
54 const executor = this
55
56 this.Module = class Module {
57 exports: any
58 isPreloading = false
59 id: string
60 filename: string
61 loaded: boolean
62 parent: null | Module | undefined
63 children: Module[] = []
64 path: string
65 paths: string[] = []
66
67 constructor(id = '', parent?: Module) {
68 this.exports = primitives.Object.create(Object.prototype)
69 // in our case the path should always be resolved already
70 this.path = dirname(id)
71 this.id = id
72 this.filename = id
73 this.loaded = false
74 this.parent = parent
75 }
76
77 get require() {
78 const require = requiresCache.get(this)
79 if (require) {
80 return require

Callers

nothing calls this directly

Calls 8

createProxyCacheMethod · 0.95
requireCoreModuleMethod · 0.95
loadCommonJSModuleMethod · 0.95
isBuiltinFunction · 0.85
createMethod · 0.80
readFileMethod · 0.80
_compileMethod · 0.80
resolveFunction · 0.50

Tested by

no test coverage detected