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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMR runtime references global by wrong name in Worker #528

Closed
bwrrp opened this issue Jan 10, 2018 · 3 comments
Closed

HMR runtime references global by wrong name in Worker #528

bwrrp opened this issue Jan 10, 2018 · 3 comments

Comments

@bwrrp
Copy link
Contributor

bwrrp commented Jan 10, 2018

馃悰 bug report

I've created a very simple project with a web worker. It looks like the script reference in the Worker constructor call is picked up automatically by Parcel and compiled into a separate bundle, which is great. However, the generated bundle by default includes the hot module reloading runtime, which contains code referencing window.location. In a Worker scope, the global is called self, and the bundle code throws a ReferenceError before the actual worker script can run.

馃帥 Configuration / code sample

Create the following files and run parcel serve index.html

index.js

var w = new Worker('./worker.js');
w.onmessage = e => console.log(e.data);
setInterval(() => w.postMessage('test'), 1000);

worker.js

self.onmessage = e => self.postMessage(e.data + ' from worker');

index.html

<script src="./index.js"></script>

馃 Expected Behavior

Expected code to load and work correctly.

馃槸 Current Behavior

Uncaught ReferenceError: window is not defined

The code in worker.js does not run and no "test" messages are logged to the console.

馃拋 Possible Solution

The error is thrown in what looks to be the following code, included in the Worker bundle:

var ws = new WebSocket('ws://' + window.location.hostname + ':{{HMR_PORT}}/');

The HRM runtime should either detect it is running in a Worker scope and use the self global, or be disabled for Worker bundles if there are other issues that prevent it from working in that context.

馃實 Your Environment

Software Version(s)
Parcel 1.4.1
Node 8.4.0
npm/Yarn npm 5.6.0
Operating System Windows 10
@DeMoorJasper
Copy link
Member

related to #476

@bwrrp
Copy link
Contributor Author

bwrrp commented Jan 10, 2018

Oops, I think I forgot to search for PRs. This is indeed the same issue, sorry for the duplicate.

devongovett pushed a commit that referenced this issue Jan 14, 2018
The name of the global object may differ based on the context in which the
script is running. For example, web workers use "self" instead of "window". As
location is a property of the global, it can be referenced directly as a global
variable, which avoids having to deal with this naming inconsistency.

Fixes #528
devongovett pushed a commit that referenced this issue Oct 15, 2018
The name of the global object may differ based on the context in which the
script is running. For example, web workers use "self" instead of "window". As
location is a property of the global, it can be referenced directly as a global
variable, which avoids having to deal with this naming inconsistency.

Fixes #528
devongovett pushed a commit that referenced this issue Oct 15, 2018
The name of the global object may differ based on the context in which the
script is running. For example, web workers use "self" instead of "window". As
location is a property of the global, it can be referenced directly as a global
variable, which avoids having to deal with this naming inconsistency.

Fixes #528
@habemus-papadum
Copy link

Hello, I'm currently getting an HMR error in my WebWorkers that seems to be related to a use of the window global in the hmr runtime here. It wasn't clear to me if the fix was as simple as changing window.location.reload() to location.reload() and if there had been an unintentional regression introduced by #2676 so for now I'm just leaving this note -- any comments welcome. thanks, nehal

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

No branches or pull requests

3 participants