-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
36 lines (26 loc) · 976 Bytes
/
index.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
'use strict';
const mod = require('module');
const getImportGlobalsSrc = require('rewire/lib/getImportGlobalsSrc');
const getDefinePropertySrc = require('rewire/lib/getDefinePropertySrc');
const initialStart = mod.wrapper[0];
const initialEnd = mod.wrapper[1];
// We prepend a list of all globals declared with var so they can be overridden (without changing original globals)
let prelude = getImportGlobalsSrc();
// Wrap module src inside IIFE so that function declarations do not clash with global variables
// @see https://github.com/jhnns/rewire/issues/56
prelude += '(function () { ';
// We append our special setter and getter.
let appendix = '\n' + getDefinePropertySrc();
// End of IIFE
appendix += '})();';
const GlobalRewire = {
enable() {
mod.wrapper[0] = initialStart + prelude;
mod.wrapper[1] = appendix + initialEnd;
},
disable() {
mod.wrapper[0] = initialStart;
mod.wrapper[1] = initialEnd;
}
};
module.exports = GlobalRewire;