1 | /**
|
2 | * Declares all globals with a var and assigns the global object. Thus you're able to
|
3 | * override globals without changing the global object itself.
|
4 | *
|
5 | * Returns something like
|
6 | * "var console = global.console; var process = global.process; ..."
|
7 | *
|
8 | * @return {String}
|
9 | */
|
10 | function getImportGlobalsSrc(ignore) {
|
11 | var key,
|
12 | value,
|
13 | src = "",
|
14 | globalObj = typeof global === "undefined"? window: global;
|
15 |
|
16 | ignore = ignore || [];
|
17 | // global itself can't be overridden because it's the only reference to our real global objects
|
18 | ignore.push("global");
|
19 | // ignore 'module', 'exports' and 'require' on the global scope, because otherwise our code would
|
20 | // shadow the module-internal variables
|
21 | // @see https://github.com/jhnns/rewire-webpack/pull/6
|
22 | ignore.push("module", "exports", "require");
|
23 |
|
24 | for (key in globalObj) { /* jshint forin: false */
|
25 | if (ignore.indexOf(key) !== -1) {
|
26 | continue;
|
27 | }
|
28 | value = globalObj[key];
|
29 |
|
30 | // key may be an invalid variable name (e.g. 'a-b')
|
31 | try {
|
32 | eval("var " + key + ";");
|
33 | src += "var " + key + " = global." + key + "; ";
|
34 | } catch(e) {}
|
35 | }
|
36 |
|
37 | return src;
|
38 | }
|
39 |
|
40 | module.exports = getImportGlobalsSrc;
|