forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
196 lines (165 loc) · 5.64 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { createRequire } from 'module'
import { fileURLToPath, pathToFileURL } from 'url'
import vm from 'vm'
import { dirname, resolve } from 'pathe'
import { isPrimitive, normalizeId, slash, toFilePath } from './utils'
import type { ModuleCache, ViteNodeRunnerOptions } from './types'
export const DEFAULT_REQUEST_STUBS = {
'/@vite/client': {
injectQuery: (id: string) => id,
createHotContext() {
return {
accept: () => {},
prune: () => {},
}
},
updateStyle() {},
},
}
export class ViteNodeRunner {
root: string
moduleCache: Map<string, ModuleCache>
constructor(public options: ViteNodeRunnerOptions) {
this.root = options.root || process.cwd()
this.moduleCache = options.moduleCache || new Map()
}
async executeFile(file: string) {
return await this.cachedRequest(`/@fs/${slash(resolve(file))}`, [])
}
async executeId(id: string) {
return await this.cachedRequest(id, [])
}
async cachedRequest(rawId: string, callstack: string[]) {
const id = normalizeId(rawId, this.options.base)
if (this.moduleCache.get(id)?.promise)
return this.moduleCache.get(id)?.promise
const fsPath = toFilePath(id, this.root)
const promise = this.directRequest(id, fsPath, callstack)
this.setCache(id, { promise })
return await promise
}
async directRequest(id: string, fsPath: string, callstack: string[]) {
callstack = [...callstack, id]
const request = async(dep: string) => {
if (callstack.includes(dep)) {
if (!this.moduleCache.get(dep)?.exports)
throw new Error(`[vite-node] Circular dependency detected\nStack:\n${[...callstack, dep].reverse().map(p => `- ${p}`).join('\n')}`)
return this.moduleCache.get(dep)!.exports
}
return this.cachedRequest(dep, callstack)
}
const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS
if (id in requestStubs)
return requestStubs[id]
const { code: transformed, externalize } = await this.options.fetchModule(id)
if (externalize) {
const mod = await this.interopedImport(externalize)
this.setCache(id, { exports: mod })
return mod
}
if (transformed == null)
throw new Error(`[vite-node] Failed to load ${id}`)
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
const url = pathToFileURL(fsPath).href
const exports: any = {}
this.setCache(id, { code: transformed, exports })
const __filename = fileURLToPath(url)
const moduleProxy = {
set exports(value) {
exportAll(exports, value)
exports.default = value
},
get exports() {
return exports.default
},
}
// Be carefull when changing this
// changing context will change amount of code added on line :114 (vm.runInThisContext)
// this messes up sourcemaps for coverage
// adjust `offset` variable in packages/vitest/src/integrations/coverage.ts#L100 if you do change this
const context = this.prepareContext({
// esm transformed by Vite
__vite_ssr_import__: request,
__vite_ssr_dynamic_import__: request,
__vite_ssr_exports__: exports,
__vite_ssr_exportAll__: (obj: any) => exportAll(exports, obj),
__vite_ssr_import_meta__: { url },
// cjs compact
require: createRequire(url),
exports,
module: moduleProxy,
__filename,
__dirname: dirname(__filename),
})
const fn = vm.runInThisContext(`async (${Object.keys(context).join(',')})=>{{${transformed}\n}}`, {
filename: fsPath,
lineOffset: 0,
})
await fn(...Object.values(context))
return exports
}
prepareContext(context: Record<string, any>) {
return context
}
setCache(id: string, mod: Partial<ModuleCache>) {
if (!this.moduleCache.has(id))
this.moduleCache.set(id, mod)
else
Object.assign(this.moduleCache.get(id), mod)
}
/**
* Define if a module should be interop-ed
* This function mostly for the ability to override by subclass
*/
shouldInterop(path: string, mod: any) {
if (this.options.interopDefault === false)
return false
// never interop ESM modules
// TODO: should also skip for `.js` with `type="module"`
return !path.endsWith('.mjs') && 'default' in mod
}
/**
* Import a module and interop it
*/
async interopedImport(path: string) {
const mod = await import(path)
if (this.shouldInterop(path, mod)) {
const tryDefault = this.hasNestedDefault(mod)
return new Proxy(mod, {
get: proxyMethod('get', tryDefault),
set: proxyMethod('set', tryDefault),
has: proxyMethod('has', tryDefault),
deleteProperty: proxyMethod('deleteProperty', tryDefault),
})
}
return mod
}
hasNestedDefault(target: any) {
return '__esModule' in target && target.__esModule && 'default' in target.default
}
}
function proxyMethod(name: 'get' | 'set' | 'has' | 'deleteProperty', tryDefault: boolean) {
return function(target: any, key: string | symbol, ...args: [any?, any?]) {
const result = Reflect[name](target, key, ...args)
if (isPrimitive(target.default))
return result
if ((tryDefault && key === 'default') || typeof result === 'undefined')
return Reflect[name](target.default, key, ...args)
return result
}
}
function exportAll(exports: any, sourceModule: any) {
// eslint-disable-next-line no-restricted-syntax
for (const key in sourceModule) {
if (key !== 'default') {
try {
Object.defineProperty(exports, key, {
enumerable: true,
configurable: true,
get() { return sourceModule[key] },
})
}
catch (_err) { }
}
}
}