-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
157 lines (126 loc) · 3.79 KB
/
index.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
declare var global: any;
declare var require: any;
var globalStubs = {};
global.$_stubs_$ = {};
export function setGlobalStubs(stubs) {
globalStubs = { ...globalStubs, ...stubs };
global.$_stubs_$ = globalStubs;
}
let nodeRegistered = false;
let firstRequire = null;
function setFirstRequire(name: string) {
if (firstRequire == null) {
firstRequire = name;
}
}
function cleanup() {
let matcher = new RegExp(firstRequire);
if (global.FuseBox) {
global.FuseBox.flush(file => file.match(matcher));
}
if (require && require.cache) {
for (let item of Object.getOwnPropertyNames(require.cache)) {
if (item.match(matcher)) {
delete require.cache[item];
}
}
}
}
export function proxy(requireFunc: Function, stubs: any) {
firstRequire = null;
// if we are in node environment there is no proxy require and we need to override original require
if (require && !nodeRegistered) {
registerNode();
}
global.$_stubs_$ = Object.assign({}, globalStubs, stubs);
// we will do require twice to make sure we are requiring a non cached function
let req = requireFunc();
cleanup();
req = requireFunc();
global.$_stubs_$ = globalStubs;
cleanup();
return req;
}
export function mock(path, impl) {
if (require && !nodeRegistered) {
registerNode();
}
let parts = path.split('/');
global.$_stubs_$[parts[parts.length - 1]] = impl();
global.$_stubs_$[path] = impl();
}
export function unmockAll() {
// const keys = Object.getOwnPropertyNames(global.$_stubs_$);
// if (keys.length == 0) {
// return;
// }
// // clear caches
// if (global.FuseBox) {
// global.FuseBox.flush(file => keys.some(k => file.indexOf(k) >= 0));
// }
// if (require && require.cache) {
// for (let item of Object.getOwnPropertyNames(require.cache)) {
// if (keys.some(k => item.indexOf(k) >= 0)) {
// delete require.cache[item];
// }
// }
// }
global.$_stubs_$ = {};
}
export function unmock(path) {
let parts = path.split('/');
global.$_stubs_$[parts[parts.length - 1]] = null;
global.$_stubs_$[path] = null;
}
export function proxyRequire(require: Function, path: string) {
let parts = path.split('/');
let module_name = parts[parts.length - 1];
setFirstRequire(module_name);
return global.$_stubs_$[path] || global.$_stubs_$[module_name] || require(path);
}
export function FuseBoxStubPlugin(test) {
return {
test: test || /\.js/,
transform(file) {
file.contents = file.contents.replace(
/require\s*\((['"]\s*[\w-_\.\/\\]*\s*['"])\)/g,
'proxyRequire(function() { return require($1); }, $1)'
);
if (file.contents.indexOf('jest.mock') >= 0) {
// replace mocks
file.contents = file.contents.replace(/jest\.mock\s*\(/g, 'require("proxyrequire").mock(');
file.contents = file.contents + '\nrequire("proxyrequire").unmockAll()';
}
}
};
}
export function registerGlobals() {
global.proxyRequire = proxyRequire;
}
export function registerNode() {
global.$_stubs_$ = {};
var Module = require('module');
if (Module && Module.prototype && Module.prototype.require) {
var originalRequire = Module.prototype.require;
Module.prototype.require = function(this: any, path: string) {
let parts = path.split('/');
let module_name = parts[parts.length - 1];
setFirstRequire(module_name);
return (
global.$_stubs_$[path] ||
global.$_stubs_$[module_name] ||
originalRequire.apply(this, arguments)
);
};
}
nodeRegistered = true;
}
export function transform(content: string) {
return content.replace(
/require\s*\((['"]\s*[\w-_\.\/\\]*\s*['"])\)/g,
'proxyRequire(function() { return require($1); }, $1)'
);
}
export function webpackStubLoader(content: string) {
return transform(content);
}