-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmock-import.js
153 lines (123 loc) · 3.96 KB
/
mock-import.js
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
import {createRequire} from 'module';
import tryCatch from 'try-catch';
import {convertImports} from './convert-imports/index.js';
import convertTracedImports from './convert-traced-imports/index.js';
import traceImports from './trace-imports/index.js';
import {isBuiltIn} from './is-built-in.js';
global.__mockImportCache = global.__mockImportCache || new Map();
global.__mockImportReImports = global.__mockImportReImports || new Set();
global.__mockImportCounter = global.__mockImportCounter || 0;
global.__traceImportCache = global.__traceImportCache || new Map();
global.__nextCount = () => ++global.__mockImportCounter;
global.__reImport = async (moduleUrl, url) => {
const {resolve} = createRequire(url);
const reFn = reImport({
resolve,
});
return await reFn(moduleUrl);
};
global.__mockImportNested = Boolean(process.env.MOCK_IMPORT_NESTED);
const traceCache = global.__traceImportCache;
const cache = global.__mockImportCache;
const reImports = global.__mockImportReImports;
const nextCount = global.__nextCount;
export const enableNestedImports = () => global.__mockImportNested = true;
export const disableNestedImports = () => global.__mockImportNested = false;
export const createMockImport = (url) => {
const {resolve} = createRequire(url);
return {
mockImport: mockImport({resolve}),
reImport: reImport({resolve}),
reImportDefault: reImportDefault({resolve}),
traceImport: traceImport({resolve}),
reTrace: reTrace({resolve}),
stop: stop({resolve}),
stopAll,
};
};
function transform({url, source, pathname, resolve}) {
let code = source;
if (reImports.has(pathname)) {
const [sourceFileName] = String(url).split('?');
code = convertImports({
sourceFileName,
resolve,
source: code,
cache,
nested: global.__mockImportNested,
});
}
if (traceCache.has(pathname)) {
const stack = traceCache.get(pathname);
code = traceImports({
url,
stack,
source: code,
});
}
if (traceCache.size)
code = convertTracedImports({
source: code,
resolve,
traceCache,
nextCount,
});
return code;
}
export async function load(url, context, defaultLoad) {
const {pathname} = new URL(url);
const {format, source: rawSource} = await defaultLoad(url, context, defaultLoad);
const source = String(rawSource);
if (format === 'commonjs')
return {
format,
};
if (url.startsWith('node:'))
return {
source,
format,
};
const {resolve} = createRequire(pathname);
const code = transform({
url,
source,
pathname,
resolve,
});
return {
source: code,
format,
};
}
const traceImport = ({resolve}) => (name, {stack}) => {
const [, pathname = name] = tryCatch(resolve, name);
traceCache.set(pathname, stack);
};
const reTrace = ({resolve}) => async (name) => {
const pathname = resolve(name);
return await import(`${pathname}?mock-import-count=${nextCount()}`);
};
const mockImport = ({resolve}) => (name, data) => {
const [, pathname = name] = tryCatch(resolve, name);
cache.set(pathname, data);
};
const reImport = ({resolve}) => async (name) => {
if (isBuiltIn(name))
return await import(name);
const pathname = resolve(name);
reImports.add(pathname);
return await import(`${pathname}?mock-import-count=${nextCount()}`);
};
const reImportDefault = ({resolve}) => async (name) => {
const imported = await reImport({resolve})(name);
return imported.default;
};
const stop = ({resolve}) => (name) => {
const pathname = resolve(name);
cache.delete(pathname);
};
const stopAll = () => {
cache.clear();
reImports.clear();
traceCache.clear();
};