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
Improve polyfill #80
Improve polyfill #80
Conversation
lib/eventsource-polyfill.js
Outdated
@@ -1,5 +1,6 @@ | |||
var EventSource = require('./eventsource') | |||
if (typeof window === 'object') { | |||
window.EventSource = window.EventSource || EventSource | |||
} else if (typeof global === 'object') { | |||
global.EventSource = global.EventSource || EventSource |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it better to create the window object in node, or attach to global instead?
eg if (typeof window !== object) global.window = {}
new EventSource
vs new window.EventSource
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm… I'd say neither. In node, we'd probably just want to set module.exports
. So, module.exports = EventSource
. Take a look at the UMD spec: https://github.com/umdjs/umd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hoping to not have to do this in a module that's designed for both node & browser:
const EventSource = (typeof window === 'object' && window.EventSource)
? window.EventSource
: require('eventsource');
Are you ok with something like this for eventsource-polyfill.js
:
var EventSource = require('./eventsource')
if (typeof window === 'object') {
if (!window.EventSource) window.EventSource = EventSource
module.exports = window.EventSource;
} else {
module.exports = EventSource;
}
That way, eventsource-polyfill becomes a commonjs module (that also polyfills the browser).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems fine in principle, but you'll probably want to add a check to make sure module
is a valid thing before setting it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I guess you have to cover for people doing <script src='eventsource-polyfill.js'>
😒
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, no - you wouldn't need to, cause then require wouldn't work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rexxars This seems fine to me. I don't see the need for the additional global and this solves things in a nice way.
All these tricks are already done by browserify...
El 21/6/2017 3:06, "Joey Baker" <notifications@github.com> escribió:
… ***@***.**** approved this pull request.
@rexxars <https://github.com/rexxars> This seems fine to me. I don't see
the need for the additional global and this solves things in a nice way.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#80 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAgfvlYySMSzpsQCcsFbG7lKiaIVaoXPks5sGGx_gaJpZM4OAT1f>
.
|
Ok, I'm using this in node on the backend and in webpack on the frontend however (both via ES6 import). |
I don't have any problems with the node compatibility, but I'd rather not break backwards compatibility at this point. If you reintroduced |
Ping @rexxars |
Thanks for pinging and sorry for the delay. Released as 1.0.5. |
This PR prevents the polyfill from creating
window.EventSourcePolyfill
, and adds node support