Skip to content

Commit

Permalink
chore: migrate to rollup
Browse files Browse the repository at this point in the history
This change allows us to:

- reduce the size of the bundle
- provide an ESM bundle (for usage in <script type="module">)

BREAKING CHANGE: due to how default export works with ES modules, we
have removed the default export of the library, which means the
following code:

```js
require("engine.io-client")(...);
```

will not work anymore. The named export must be used instead:

```js
const { Socket } = require("engine.io-client);
// or import { Socket } from "engine.io-client";

const socket = new Socket(...);
```

Note: the UMD build still exposes a function though:

```html
<script src="/path/to/engine.io.js"></script>
<script>
  const socket = eio(...);
</script>
```

Note: webpack is still used with zuul because of the custom builder
(zuul-builder-webpack)
  • Loading branch information
darrachequesne committed Oct 8, 2021
1 parent 221433e commit 27de300
Show file tree
Hide file tree
Showing 14 changed files with 391 additions and 45 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Engine.IO is a commonjs module, which means you can include it by using
1. write your app code

```js
const socket = require('engine.io-client')('ws://localhost');
const { Socket } = require('engine.io-client');
const socket = new Socket('ws://localhost');
socket.on('open', () => {
socket.on('message', (data) => {});
socket.on('close', () => {});
Expand All @@ -65,7 +66,7 @@ Engine.IO is a commonjs module, which means you can include it by using
```html
<script src="/path/to/engine.io.js"></script>
<script>
const socket = new eio.Socket('ws://localhost/');
const socket = eio('ws://localhost/');
socket.binaryType = 'blob';
socket.on('open', () => {
socket.send(new Int8Array(5));
Expand All @@ -80,7 +81,8 @@ Engine.IO is a commonjs module, which means you can include it by using
Add `engine.io-client` to your `package.json` and then:
```js
const socket = require('engine.io-client')('ws://localhost');
const { Socket } = require('engine.io-client');
const socket = new Socket('ws://localhost');
socket.on('open', () => {
socket.on('message', (data) => {});
socket.on('close', () => {});
Expand All @@ -95,7 +97,8 @@ const opts = {
ca: fs.readFileSync('test/fixtures/ca.crt')
};
const socket = require('engine.io-client')('ws://localhost', opts);
const { Socket } = require('engine.io-client');
const socket = new Socket('ws://localhost', opts);
socket.on('open', () => {
socket.on('message', (data) => {});
socket.on('close', () => {});
Expand All @@ -111,7 +114,8 @@ const opts = {
}
};
const socket = require('engine.io-client')('ws://localhost', opts);
const { Socket } = require('engine.io-client');
const socket = new Socket('ws://localhost', opts);
socket.on('open', () => {
socket.on('message', (data) => {});
socket.on('close', () => {});
Expand All @@ -124,13 +128,13 @@ Please note that in this case the headers won't be sent in the WebSocket upgrade
```js
// WILL NOT WORK in the browser
require('engine.io-client')('http://localhost', {
const socket = new Socket('http://localhost', {
extraHeaders: {
'X-Custom-Header-For-My-Project': 'will not be sent'
}
});
// WILL NOT WORK
require('engine.io-client')('http://localhost', {
const socket = new Socket('http://localhost', {
transports: ['websocket'], // polling is disabled
transportOptions: {
polling: {
Expand All @@ -141,7 +145,7 @@ require('engine.io-client')('http://localhost', {
}
});
// WILL WORK
require('engine.io-client')('http://localhost', {
const socket = new Socket('http://localhost', {
transports: ['polling', 'websocket'],
transportOptions: {
polling: {
Expand Down
3 changes: 3 additions & 0 deletions lib/browser-entrypoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Socket } from "./socket.js";

export default (uri, opts) => new Socket(uri, opts);
6 changes: 0 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Socket } from "./socket.js";

export default (uri, opts) => new Socket(uri, opts);

/**
* Expose deps for legacy compatibility
* and standalone browser access.
*/
export { Socket };
export { SocketOptions } from "./socket.js";
export const protocol = Socket.protocol;
Expand Down
Loading

0 comments on commit 27de300

Please sign in to comment.