-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Distribute ESM module in addition to CJS #1886
Comments
Yes, probably, but I see no upsides, only downsides. |
For example we could use an ES module wrapper as documented here https://nodejs.org/api/packages.html#packages_approach_1_use_an_es_module_wrapper. $ git diff --cached
diff --git a/package.json b/package.json
index c2d63af..360ac2d 100644
--- a/package.json
+++ b/package.json
@@ -16,6 +16,10 @@
"author": "Einar Otto Stangvik <einaros@gmail.com> (http://2x.io)",
"license": "MIT",
"main": "index.js",
+ "exports": {
+ "import": "./wrapper.mjs",
+ "require": "./index.js"
+ },
"browser": "browser.js",
"engines": {
"node": ">=8.3.0"
@@ -23,7 +27,8 @@
"files": [
"browser.js",
"index.js",
- "lib/*.js"
+ "lib/*.js",
+ "wrapper.mjs"
],
"scripts": {
"test": "nyc --reporter=lcov --reporter=text mocha --throw-deprecation test/*.test.js",
diff --git a/wrapper.mjs b/wrapper.mjs
new file mode 100644
index 0000000..8df8956
--- /dev/null
+++ b/wrapper.mjs
@@ -0,0 +1,9 @@
+import createWebSocketStream from './lib/stream.js';
+import Receiver from './lib/receiver.js';
+import Sender from './lib/sender.js';
+import WebSocket from './lib/websocket.js';
+import WebSocketServer from './lib/websocket-server.js';
+
+export { createWebSocketStream, Receiver, Sender, WebSocket, WebSocketServer };
+
+export default WebSocket; but this might introduce issues with bundlers and would break code that uses the library like this const createWebSocketStream = require('ws/lib/stream'); At the very least we should bump the major version and I'm not sure if it makes sense when this import WebSocket from 'ws'; already works without changes. |
i vote for ditching commonjs.
We only target node versions, it's not like it's running in the browser. and uses if someone is still using commonjs then they can import it using the async fetch-blob, node-fetch, formdata-polyfill are some of wish have already ditched commonjs for being only ESM packages |
Unfortunately this is not true :( A lot of people bundle for the server side. If you search through closed issues there are some examples of people bundling for Electron or Lambda functions. Also, I don't want to drop Node.js 10 support in the next major release and there are some blockers for a pure ESM migration like https://github.com/websockets/ws/blob/7.5.2/lib/validation.js#L85-L104 and https://github.com/websockets/ws/blob/7.5.2/lib/buffer-util.js#L104-L129. |
hmm.
could be solved with top-level await? edit: that did require 14.8... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#browser_compatibility :( |
|
It could actually work with top-level |
I know it can work This is how i mange to ship one package that don't Bundle everything and conditionally loads from either cdn or npm if needed and not having to bundle many different version for each enviorment all ESM imports are loaded asynchronous like a asyncGenerator (even if you don't know it) hence why commonjs package can only load ESM packages with |
Yes, but what are the advantages over a simple wrapper like this #1886 (comment) that works everywhere? |
the differences is that you only export it as a ESM package... Like a Wrapper. You are not using ESM yourself anywhere in your own codebase cuz you use
...this year is probably going to be the worst when we are in a transition face from going from "UMD-whatever" to ESM standard |
I don't think this is a problem for It could only be an issue if some dev dependency decides to go ESM only but even in that case we are not forced to update it. |
☝️ That's correct |
btw, here is a interesting article i found earlier: https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1 if you want to read some tech stuff related to JS/ESM |
Yes, I know it's a mess. I received hundreds of emails from people discussing this endlessly in the Node.js organization. Top-level I honestly don't think it's a good idea for TL;DR we don't need top-level |
You are probably right, a #1886 (comment) would be fine for now for a transition period. Glad we could have this level of discussion. (where neither did not butcher the other person thoughts/ideas without hearing out first what both ppl where thinking and discussing pro & conn) I would wish that you put it in a backlog doe to ditch commonjs entirely once the time is right, like in the "next-next" major release or some milestone. (along with native Event + EventTarget & Blob support?) It would be cool if you partially supported Blob look-a-like items in just check if it's a object with a |
Yes, probably when Node.js 14 will no longer be supported. Backwards compatibility is nice.
Edit: even if it is exported |
The const buf = await blob.arrayBuffer();
ws.send(buf); Users can already do that. |
hello, i came here after finding they used a it appears in your latest commit on this issue that i'm really hoping somebody on earth can make a working es module library for web sockets, so i don't have to ;) tag me in and i'd be happy to participate in a code review so we can make sure this actually works for |
I don't think this is a problem. |
@lpinca — okay, i understand now — this makes sense
that clears it up for me, sorry about the false alarm, please proceed! |
Would it be possible to distribute an ESM module version in addition to the current CommonJS version?
I know it's not a clean solution (gotta use "main", "module" and "exports" fields in package.json to work with all setups), but more and more libraries are distributing both so as to enable users to switch over to ESM.
The text was updated successfully, but these errors were encountered: