Skip to content

Commit

Permalink
Use es5-ext/global as a more robust way to resolve browser's window
Browse files Browse the repository at this point in the history
The [es5-ext/global][es5g] module is based on Mathias Bynens' [polyfill][poly]
for [`globalThis`][gT]. It uses the same robust technique but is more
conservative in that it does not add a `globalThis` property to the resolved
object, i.e. `window` in a browser and `global` in Node. It also lacks the "old
IE" fallback seen in a [demo][demo] of the original polyfill, but that is
easily provided in this commit.

[es5g]: https://github.com/medikoo/es5-ext/blob/master/global.js
[poly]: https://mathiasbynens.be/notes/globalthis
[gT]: https://github.com/tc39/proposal-global
[demo]: https://mathiasbynens.be/demo/globalthis-ie
  • Loading branch information
michaelsbradleyjr committed Jul 29, 2019
1 parent f78487c commit b134a75
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/browser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var _global = (function () {
if (!this && typeof global !== 'undefined') {
return global;
}
return this;
})();
var NativeWebSocket = _global.WebSocket || _global.MozWebSocket;
var _globalThis;
try {
_globalThis = require('es5-ext/global');
} catch (error) {
} finally {
if (!_globalThis && typeof window !== 'undefined') { _globalThis = window; }
if (!_globalThis) { throw new Error('Could not determine global this'); }
}

var NativeWebSocket = _globalThis.WebSocket || _globalThis.MozWebSocket;
var websocket_version = require('./version');


Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"debug": "^2.2.0",
"es5-ext": "^0.10.50",

This comment has been minimized.

Copy link
@max-mapper

max-mapper Dec 12, 2019

it would be nice to get rid of this large dependency, when auditing our apps usage of web3 I found that this dependency added around 1000 .js files to our installed module size

This comment has been minimized.

Copy link
@michaelsbradleyjr

michaelsbradleyjr Dec 12, 2019

Author

There are two options, I think.

  1. The shim or an equivalent could be inlined.
  2. If web3 is using up-to-date babel transforms then maybe this script could rely on web3 having setup the globalThis polyfill from core-js. Or, if that's not feasible, maye this script can use the polyfill from core-js, though that could suffer from the same problem, i.e. having a dependency that bloats the install size.

This comment has been minimized.

Copy link
@max-mapper

max-mapper Dec 12, 2019

@michaelsbradleyjr either of those would work, thanks for responding! for now we are manually blocking es5-ext from being packaged because our app (Electron based) does not need it

"nan": "^2.14.0",
"gulp": "^4.0.2",
"typedarray-to-buffer": "^3.1.5",
Expand Down

0 comments on commit b134a75

Please sign in to comment.