Skip to content
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

remove dependency on window so pusher-js can be imported in workers/shared_workers #499

Closed
abhishiv opened this issue Oct 25, 2020 · 3 comments

Comments

@abhishiv
Copy link

abhishiv commented Oct 25, 2020

Right now in workers i have to externally importScript from cdn instead of pusher being a part of my bundle because it throws window is not defined.

importScripts("https://js.pusher.com/7.0/pusher.worker.min.js");

v/s

import Pusher from 'pusher-js'

If I import in my bundle pusher-js, this following error is thrown 'window is not defined' - I guess it could be easily fixed by modifying webpack config

(function webpackUniversalModuleDefinition(root, factory) {
	if(typeof exports === 'object' && typeof module === 'object')
		module.exports = factory();
	else if(typeof define === 'function' && define.amd)
		define([], factory);
	else if(typeof exports === 'object')
		exports["Pusher"] = factory();
	else
		root["Pusher"] = factory();
})(window, function() {

Also As noted by @ hph in #183 "window is not defined" error when using webpack + pusher-js + react" the solution that issue was closed with isn't ideal even for SSR, and won't work for use cases like mine.

Excellent, in that case I'll consider this issue closed for now, although I think we should try to find a better solution.

@leesio
Copy link
Contributor

leesio commented Oct 26, 2020

Thanks for reporting this issue.

I think the issue is that:

import Pusher from 'pusher-js'

Will use the build of pusher-js for the web runtime, rather than the worker runtime. In order to resolve this, we'll need to provide an entrypoint for the worker runtime, similar to how we expose the pusher-with-encryption bundle

leesio pushed a commit that referenced this issue Jan 25, 2021
This should solve issue #499.

It should be possible to import the worker build using

```
import Pusher from 'pusher-js/worker'
```

or

```
import Pusher from 'pusher-js/worker/with-encryption'
```
@leesio
Copy link
Contributor

leesio commented Jan 25, 2021

I just released version 7.0.3 which should allow you to import the worker like this:

import Pusher from 'pusher-js/worker

or

import Pusher from 'pusher-js/worker/with-encryption'

I believe this should solve your issue. If it doesn't, please leave a comment and I'll reopen the issue

@leesio leesio closed this as completed Jan 25, 2021
@van-datadive
Copy link

Pusher worker package still access window in randomInt function

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

No branches or pull requests

3 participants