Skip to content

Commit 16b6569

Browse files
feat: provide an ESM build with and without debug
See also: socketio/engine.io-client@00d7e7d Related: - #1188 - #1378
1 parent 7187453 commit 16b6569

21 files changed

+83
-67
lines changed

lib/index.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
import { url } from "./url";
2-
import { Manager, ManagerOptions } from "./manager";
3-
import { Socket, SocketOptions } from "./socket";
1+
import { url } from "./url.js";
2+
import { Manager, ManagerOptions } from "./manager.js";
3+
import { Socket, SocketOptions } from "./socket.js";
4+
import debugModule from "debug"; // debug()
45

5-
const debug = require("debug")("socket.io-client");
6-
7-
/**
8-
* Module exports.
9-
*/
10-
11-
module.exports = exports = lookup;
6+
const debug = debugModule("socket.io-client"); // debug()
127

138
/**
149
* Managers cache.
1510
*/
16-
const cache: Record<string, Manager> = (exports.managers = {});
11+
const cache: Record<string, Manager> = {};
1712

1813
/**
1914
* Looks up an existing `Manager` for multiplexing.
@@ -84,22 +79,17 @@ function lookup(
8479

8580
export { protocol } from "socket.io-parser";
8681

87-
/**
88-
* `connect`.
89-
*
90-
* @param {String} uri
91-
* @public
92-
*/
93-
94-
exports.connect = lookup;
95-
9682
/**
9783
* Expose constructors for standalone build.
9884
*
9985
* @public
10086
*/
10187

102-
export { Manager, ManagerOptions } from "./manager";
103-
export { Socket } from "./socket";
104-
export { lookup as io, SocketOptions };
105-
export default lookup;
88+
export {
89+
Manager,
90+
ManagerOptions,
91+
Socket,
92+
SocketOptions,
93+
lookup as io,
94+
lookup as connect,
95+
};

lib/manager.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import {
33
SocketOptions as EngineOptions,
44
installTimerFunctions,
55
} from "engine.io-client";
6-
import { Socket, SocketOptions } from "./socket";
6+
import { Socket, SocketOptions } from "./socket.js";
77
import * as parser from "socket.io-parser";
88
import { Decoder, Encoder, Packet } from "socket.io-parser";
9-
import { on } from "./on";
10-
import * as Backoff from "backo2";
9+
import { on } from "./on.js";
10+
import Backoff from "backo2";
1111
import {
1212
DefaultEventsMap,
1313
EventsMap,
1414
StrictEventEmitter,
15-
} from "./typed-events";
15+
} from "./typed-events.js";
16+
import debugModule from "debug"; // debug()
1617

17-
const debug = require("debug")("socket.io-client:manager");
18+
const debug = debugModule("socket.io-client:manager"); // debug()
1819

1920
export interface ManagerOptions extends EngineOptions {
2021
/**

lib/on.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type * as Emitter from "component-emitter";
2-
import { StrictEventEmitter } from "./typed-events";
2+
import { StrictEventEmitter } from "./typed-events.js";
33

44
export function on(
55
obj: Emitter | StrictEventEmitter<any, any>,

lib/socket.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { Packet, PacketType } from "socket.io-parser";
2-
import { on } from "./on";
3-
import { Manager } from "./manager";
2+
import { on } from "./on.js";
3+
import { Manager } from "./manager.js";
44
import {
55
DefaultEventsMap,
66
EventNames,
77
EventParams,
88
EventsMap,
99
StrictEventEmitter,
10-
} from "./typed-events";
10+
} from "./typed-events.js";
11+
import debugModule from "debug"; // debug()
1112

12-
const debug = require("debug")("socket.io-client:socket");
13+
const debug = debugModule("socket.io-client:socket"); // debug()
1314

1415
export interface SocketOptions {
1516
/**

lib/url.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import * as parseuri from "parseuri";
1+
import parseuri from "parseuri";
2+
import debugModule from "debug"; // debug()
23

3-
const debug = require("debug")("socket.io-client:url");
4+
const debug = debugModule("socket.io-client:url"); // debug()
45

56
type ParsedUrl = {
67
source: string;

package.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@
1111
],
1212
"files": [
1313
"dist/",
14-
"build/",
15-
"wrapper.mjs"
14+
"build/"
1615
],
1716
"type": "commonjs",
18-
"main": "./build/index.js",
17+
"main": "./build/cjs/index.js",
18+
"module": "./build/esm/index.js",
1919
"exports": {
2020
"./package.json": "./package.json",
2121
"./dist/socket.io.js": "./dist/socket.io.js",
2222
"./dist/socket.io.js.map": "./dist/socket.io.js.map",
2323
".": {
24-
"import": "./wrapper.mjs",
25-
"require": "./build/index.js"
24+
"import": {
25+
"node": "./build/esm-debug/index.js",
26+
"default": "./build/esm/index.js"
27+
},
28+
"require": "./build/cjs/index.js"
2629
}
2730
},
28-
"types": "./build/index.d.ts",
31+
"types": "./build/esm/index.d.ts",
2932
"dependencies": {
3033
"@socket.io/component-emitter": "~2.0.0",
3134
"backo2": "~1.0.2",
@@ -68,7 +71,7 @@
6871
"zuul-ngrok": "4.0.0"
6972
},
7073
"scripts": {
71-
"compile": "rimraf ./build && tsc",
74+
"compile": "rimraf ./build && tsc && tsc -p tsconfig.esm.json && ./postcompile.sh",
7275
"test": "npm run format:check && npm run compile && if test \"$BROWSERS\" = \"1\" ; then npm run test:browser; else npm run test:node; fi",
7376
"test:node": "mocha --require ts-node/register --reporter dot --require test/support/server.js test/index.js",
7477
"test:browser": "zuul test/index.js",

postcompile.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
cp ./support/package.cjs.json ./build/cjs/package.json
4+
cp ./support/package.esm.json ./build/esm/package.json
5+
6+
cp -r ./build/esm/ ./build/esm-debug/
7+
8+
sed -i '/debug(/d' ./build/esm/*.js

support/package.cjs.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

support/package.esm.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

support/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const banner = `Socket.IO v${version}
66
Released under the MIT License.`;
77

88
module.exports = {
9-
entry: "./build/index.js",
9+
entry: "./build/esm/index.js",
1010
output: {
1111
filename: "socket.io.js",
1212
library: "io",

test/connection.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as expect from "expect.js";
1+
import expect from "expect.js";
22
import { io, Manager, ManagerOptions } from "..";
3-
import * as hasCORS from "has-cors";
4-
import * as FakeTimers from "@sinonjs/fake-timers";
5-
import * as textBlobBuilder from "text-blob-builder";
6-
import * as env from "./support/env";
3+
import hasCORS from "has-cors";
4+
import FakeTimers from "@sinonjs/fake-timers";
5+
import textBlobBuilder from "text-blob-builder";
6+
import env from "./support/env";
77

88
describe("connection", function () {
99
this.timeout(70000);

test/fixtures/no-unref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const io = require("../..");
1+
const { io } = require("../..");
22
const socket = io("http://localhost:3211", {
33
autoUnref: false,
44
});

test/fixtures/unref-during-reconnection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const io = require("../..");
1+
const { io } = require("../..");
22
const socket = io("http://localhost:3211", {
33
autoUnref: true,
44
});

test/fixtures/unref-polling-only.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const io = require("../..");
1+
const { io } = require("../..");
22
const socket = io("http://localhost:3210", {
33
autoUnref: true,
44
transports: ["polling"],

test/fixtures/unref-websocket-only.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const io = require("../..");
1+
const { io } = require("../..");
22
const socket = io("http://localhost:3210", {
33
autoUnref: true,
44
transports: ["websocket"],

test/fixtures/unref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const io = require("../..");
1+
const { io } = require("../..");
22
const socket = io("http://localhost:3210", {
33
autoUnref: true,
44
});

test/socket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as expect from "expect.js";
1+
import expect from "expect.js";
22
import { io } from "..";
33

44
describe("socket", function () {

test/url.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { url } from "../build/url";
2-
import * as expect from "expect.js";
1+
import { url } from "../build/cjs/url";
2+
import expect from "expect.js";
33

44
const loc: any = {};
55

tsconfig.esm.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "build/esm/",
4+
"target": "es2018",
5+
"module": "esnext",
6+
"moduleResolution": "node",
7+
"declaration": true
8+
},
9+
"include": [
10+
"./lib/**/*"
11+
]
12+
}

tsconfig.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"compilerOptions": {
3-
"outDir": "./build",
4-
"allowJs": false,
5-
"target": "es2017",
3+
"outDir": "build/cjs/",
4+
"target": "es2018", // Node.js 10 (https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping)
65
"module": "commonjs",
7-
"declaration": true
6+
"declaration": false,
7+
"esModuleInterop": true
88
},
99
"include": [
1010
"./lib/**/*"

wrapper.mjs

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)