/
esmInjector.ts
270 lines (248 loc) · 8.82 KB
/
esmInjector.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import MagicString from 'magic-string'
import { extract_names as extractNames } from 'periscopic'
import type { Expression, ImportDeclaration } from 'estree'
import type { AcornNode } from 'rollup'
import type { Node, Positioned } from './esmWalker'
import { esmWalker, isInDestructuringAssignment, isNodeInPattern, isStaticProperty } from './esmWalker'
const viInjectedKey = '__vi_inject__'
// const viImportMetaKey = '__vi_import_meta__' // to allow overwrite
const viExportAllHelper = '__vi_export_all__'
const skipHijack = [
'/@vite/client',
'/@vite/env',
/vite\/dist\/client/,
]
// this is basically copypaste from Vite SSR
// this method transforms all import and export statements into `__vi_injected__` variable
// to allow spying on them. this can be disabled by setting `slowHijackESM` to `false`
export function injectVitestModule(code: string, id: string, parse: (code: string, options: any) => AcornNode) {
if (skipHijack.some(skip => id.match(skip)))
return
const s = new MagicString(code)
let ast: any
try {
ast = parse(code, {
sourceType: 'module',
ecmaVersion: 'latest',
locations: true,
})
}
catch (err) {
console.error(`Cannot parse ${id}:\n${(err as any).message}`)
return
}
let uid = 0
const idToImportMap = new Map<string, string>()
const declaredConst = new Set<string>()
const hoistIndex = 0
let hasInjected = false
// this will tranfrom import statements into dynamic ones, if there are imports
// it will keep the import as is, if we don't need to mock anything
// in browser environment it will wrap the module value with "vitest_wrap_module" function
// that returns a proxy to the module so that named exports can be mocked
const transformImportDeclaration = (node: ImportDeclaration) => {
const source = node.source.value as string
if (skipHijack.some(skip => source.match(skip)))
return null
const importId = `__vi_esm_${uid++}__`
const hasSpecifiers = node.specifiers.length > 0
const code = hasSpecifiers
? `import { ${viInjectedKey} as ${importId} } from '${source}'\n`
: `import '${source}'\n`
return {
code,
id: importId,
}
}
function defineImport(node: ImportDeclaration) {
const declaration = transformImportDeclaration(node)
if (!declaration)
return null
s.appendLeft(hoistIndex, declaration.code)
return declaration.id
}
function defineImportAll(source: string) {
const importId = `__vi_esm_${uid++}__`
s.appendLeft(hoistIndex, `const { ${viInjectedKey}: ${importId} } = await import(${JSON.stringify(source)});\n`)
return importId
}
function defineExport(position: number, name: string, local = name) {
hasInjected = true
s.appendLeft(
position,
`\nObject.defineProperty(${viInjectedKey}, "${name}", `
+ `{ enumerable: true, configurable: true, get(){ return ${local} }});`,
)
}
// 1. check all import statements and record id -> importName map
for (const node of ast.body as Node[]) {
// import foo from 'foo' --> foo -> __import_foo__.default
// import { baz } from 'foo' --> baz -> __import_foo__.baz
// import * as ok from 'foo' --> ok -> __import_foo__
if (node.type === 'ImportDeclaration') {
const importId = defineImport(node)
if (!importId)
continue
s.remove(node.start, node.end)
for (const spec of node.specifiers) {
if (spec.type === 'ImportSpecifier') {
idToImportMap.set(
spec.local.name,
`${importId}.${spec.imported.name}`,
)
}
else if (spec.type === 'ImportDefaultSpecifier') {
idToImportMap.set(spec.local.name, `${importId}.default`)
}
else {
// namespace specifier
idToImportMap.set(spec.local.name, importId)
}
}
}
}
// 2. check all export statements and define exports
for (const node of ast.body as Node[]) {
// named exports
if (node.type === 'ExportNamedDeclaration') {
if (node.declaration) {
if (
node.declaration.type === 'FunctionDeclaration'
|| node.declaration.type === 'ClassDeclaration'
) {
// export function foo() {}
defineExport(node.end, node.declaration.id!.name)
}
else {
// export const foo = 1, bar = 2
for (const declaration of node.declaration.declarations) {
const names = extractNames(declaration.id as any)
for (const name of names)
defineExport(node.end, name)
}
}
s.remove(node.start, (node.declaration as Node).start)
}
else {
s.remove(node.start, node.end)
if (node.source) {
// export { foo, bar } from './foo'
const importId = defineImportAll(node.source.value as string)
// hoist re-exports near the defined import so they are immediately exported
for (const spec of node.specifiers) {
defineExport(
hoistIndex,
spec.exported.name,
`${importId}.${spec.local.name}`,
)
}
}
else {
// export { foo, bar }
for (const spec of node.specifiers) {
const local = spec.local.name
const binding = idToImportMap.get(local)
defineExport(node.end, spec.exported.name, binding || local)
}
}
}
}
// default export
if (node.type === 'ExportDefaultDeclaration') {
const expressionTypes = ['FunctionExpression', 'ClassExpression']
if (
'id' in node.declaration
&& node.declaration.id
&& !expressionTypes.includes(node.declaration.type)
) {
// named hoistable/class exports
// export default function foo() {}
// export default class A {}
hasInjected = true
const { name } = node.declaration.id
s.remove(node.start, node.start + 15 /* 'export default '.length */)
s.append(
`\nObject.defineProperty(${viInjectedKey}, "default", `
+ `{ enumerable: true, configurable: true, value: ${name} });`,
)
}
else {
// anonymous default exports
hasInjected = true
s.update(
node.start,
node.start + 14 /* 'export default'.length */,
`${viInjectedKey}.default =`,
)
// keep export default for optimized dependencies
s.append(`\nexport default { ${viInjectedKey}: ${viInjectedKey}.default };\n`)
}
}
// export * from './foo'
if (node.type === 'ExportAllDeclaration') {
s.remove(node.start, node.end)
const importId = defineImportAll(node.source.value as string)
// hoist re-exports near the defined import so they are immediately exported
if (node.exported) {
defineExport(hoistIndex, node.exported.name, `${importId}`)
}
else {
hasInjected = true
s.appendLeft(hoistIndex, `${viExportAllHelper}(${viInjectedKey}, ${importId});\n`)
}
}
}
// 3. convert references to import bindings & import.meta references
esmWalker(ast, {
onIdentifier(id, parent, parentStack) {
const grandparent = parentStack[1]
const binding = idToImportMap.get(id.name)
if (!binding)
return
if (isStaticProperty(parent) && parent.shorthand) {
// let binding used in a property shorthand
// { foo } -> { foo: __import_x__.foo }
// skip for destructuring patterns
if (
!isNodeInPattern(parent)
|| isInDestructuringAssignment(parent, parentStack)
)
s.appendLeft(id.end, `: ${binding}`)
}
else if (
(parent.type === 'PropertyDefinition'
&& grandparent?.type === 'ClassBody')
|| (parent.type === 'ClassDeclaration' && id === parent.superClass)
) {
if (!declaredConst.has(id.name)) {
declaredConst.add(id.name)
// locate the top-most node containing the class declaration
const topNode = parentStack[parentStack.length - 2]
s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`)
}
}
else {
s.update(id.start, id.end, binding)
}
},
// TODO: make env updatable
onImportMeta() {
// s.update(node.start, node.end, viImportMetaKey)
},
onDynamicImport(node) {
const replace = '__vi_wrap_module__(import('
s.overwrite(node.start, (node.source as Positioned<Expression>).start, replace)
s.overwrite(node.end - 1, node.end, '))')
},
})
if (hasInjected) {
// make sure "__vi_injected__" is declared as soon as possible
s.prepend(`const ${viInjectedKey} = { [Symbol.toStringTag]: "Module" };\n`)
s.append(`\nexport { ${viInjectedKey} }`)
}
return {
ast,
code: s.toString(),
map: s.generateMap({ hires: true, source: id }),
}
}