Skip to content

Fix Webpack global scope issue #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2019
Merged

Fix Webpack global scope issue #360

merged 1 commit into from
Jul 15, 2019

Conversation

giacomodeliberali
Copy link
Contributor

In webpack 4 (Angular 8)

var _global = (function () {
	return this;
})();

will return undefined causing "Cannot read property WebSocket of undefined".

Resolves #287.

In webpack 4 (Angular 8) the `_global = ...` will return undefined causing "Cannot read property WebSocket of undefined"
@ibc
Copy link
Collaborator

ibc commented Jul 15, 2019

Makes sense. Merged, thanks.

@ibc ibc merged commit 114661a into theturtle32:master Jul 15, 2019
@michaelsbradleyjr
Copy link
Contributor

michaelsbradleyjr commented Jul 26, 2019

I realize this has been merged already but for a bit more resilience how about:

var _global = (function () {
  if (!this) {
    if (typeof global !== 'undefined') {
      return global;
    }
    if (typeof globalThis !== 'undefined') {
      return globalThis;
    }
    if (typeof self !== 'undefined') {
      return self;
    }
    if (typeof window !== 'undefined') {
      return window;
    }
    throw new Error('could not determine the global object');
  }
  return this;
})();

See: https://github.com/tc39/proposal-global#rationale.

@michaelsbradleyjr
Copy link
Contributor

michaelsbradleyjr commented Jul 26, 2019

There's actually a viable polyfill for globalThis:

(function() {
  if (typeof globalThis === 'object') return;
  try {
    Object.defineProperty(Object.prototype, '__magic__', {
      get: function() {
        return this;
      },
      configurable: true
    });
    __magic__.globalThis = __magic__;
    // The previous line should have made `globalThis` globally
    // available, but it fails in Internet Explorer 10 and older.
    // Detect this failure and fall back.
    if (typeof globalThis === 'undefined') {
      // Assume `window` exists.
      window.globalThis = window;
    }
    delete Object.prototype.__magic__;
  } catch (error) {
    // In IE8, Object.defineProperty only works on DOM objects.
    // If we hit this code path, assume `window` exists.
    window.globalThis = window;
  }
}());

See: https://mathiasbynens.be/notes/globalthis.

Node 12 and current Chrome, Firefox, and Safari all already implement globalThis. So instead of defining _global you could plop in this polyfill and then refer to globalThis instead of _global.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

require('webwocket').client fails if _global returns undefined (browser.js:1)
3 participants