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

Changed browser to es modules #20

Merged
merged 4 commits into from
Jun 6, 2022
Merged

Conversation

guillemcordoba
Copy link
Contributor

@guillemcordoba guillemcordoba commented Jul 9, 2021

@heineiuo I changed the browser.js to be es modules, which is what all bundlers and browsers actually understand.

I could do this in a separate file, but then I think it would be quite tricky to separate when to import esmodules vs commonjs ones, when the "module"/"main" keywords are used to distinguish the node and browser environment.

Also added rollup example.

@@ -14,4 +14,4 @@ if (typeof WebSocket !== 'undefined') {
ws = self.WebSocket || self.MozWebSocket
}

module.exports = ws
export default ws
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a breaking change, the major version number should be changed when we publish it🤔

} else if (typeof self !== 'undefined') {
ws = self.WebSocket || self.MozWebSocket;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we throw an error when no WebSocket detected, like ws did: https://github.com/websockets/ws/blob/master/browser.js

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can better use "globalThis"?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
especially since most modern browsers work from a regular WebSocket
https://caniuse.com/mdn-api_websocket

format: "es",
file: "app.output.js",
},
plugins: [resolve()],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this config and got an error, does here lost { browser: true } option?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, resolve() needs option browser: true I believe to replicate behaviour.

Comment on lines +9 to +10
} else if (typeof global !== 'undefined') {
ws = global.WebSocket || global.MozWebSocket;
Copy link

@JoCat JoCat Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover, checking for global is useless here, because this is a node.js object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#description

Historically, accessing the global object has required different syntax in different JavaScript environments. On the web you can use window, self, or frames - but in Web Workers only self will work. In Node.js none of these work, and you must instead use global.

@JoCat
Copy link

JoCat commented Sep 14, 2021

Wouldn't it be better to just do it like this?
browser.js

export default WebSocket

lol 😅

@JoCat
Copy link

JoCat commented May 16, 2022

@heineiuo @guillemcordoba Are there any plans to further advance this issue?

@heineiuo heineiuo mentioned this pull request May 18, 2022
@AbdulAhadKhan0308
Copy link

When will this request be merged? @heineiuo have the errors been resolved?

@heineiuo
Copy link
Owner

heineiuo commented Jun 6, 2022

As many people are waiting this change to be landed, so I think a good solution is just merge this PR and open a new PR to solve the errors in examples.

@heineiuo heineiuo merged commit 09b2b40 into heineiuo:master Jun 6, 2022
This was referenced Jun 6, 2022
@heineiuo
Copy link
Owner

heineiuo commented Jun 6, 2022

Released a beta version ([email protected]) with this change.

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.

5 participants