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
|
"use strict"; // run code in ES5 strict mode
var registry = {},
getImportGlobalsSrc = require("../../getImportGlobalsSrc.js");
var requireInDisguise;
eval("requireInDisguise = require");
function getId(module) {
var index = registry.modules.indexOf(module);
return registry.id[index] || null;
}
function getIdFromCache(module) {
var cache = require.cache,
id;
for (id in cache) {
if (cache.hasOwnProperty(id)) {
if (cache[id] === module) {
return Number(id);
}
}
}
return null;
}
function getIdByExportsObj(moduleExports) {
var id;
for (id in registry) {
if (registry.hasOwnProperty(id)) {
if (registry[id].module.exports === moduleExports) {
return Number(id);
}
}
}
return null;
}
function webpackRewire(path, moduleExports) {
var id = getIdByExportsObj(moduleExports),
cachedModule,
rewiredModule,
setter,
getter;
if (typeof id !== "number") {
throw new Error("(rewire) Sorry, rewiring '" + path + "' is currently not supported.");
}
cachedModule = require.cache[id];
delete require.cache[id];
rewiredModule = requireInDisguise(id);
//require.cache[id] = cachedModule;
setter = registry[id].setter;
getter = registry[id].getter;
rewiredModule.__set__ = setter;
rewiredModule.__get__ = getter;
return rewiredModule;
}
webpackRewire.register = function (module, setter, getter) {
var id = getIdFromCache(module);
registry[id] = {
module: module,
setter: setter,
getter: getter
};
};
/**
* Scans for global vars and returns an evalable string that declares all globals as a var.
* This way a global variable can be overridden by __set__ without changing the global instance.
* It is executed each time again to include global variables that have been added later.
*
* @return {String}
*/
webpackRewire.getImportGlobalsSrc = function () {
return getImportGlobalsSrc(['require','module','exports','__dirname','__filename','process']);
};
module.exports = webpackRewire;
|