From 16b65698aed766e1e645c78847f2e91bfc5b6f56 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 12 Oct 2021 00:30:46 +0200 Subject: [PATCH] feat: provide an ESM build with and without debug See also: https://github.com/socketio/engine.io-client/commit/00d7e7d7ee85b4cfa6f9f547203cc692083ac61c Related: - https://github.com/socketio/socket.io-client/issues/1188 - https://github.com/socketio/socket.io-client/issues/1378 --- lib/index.ts | 38 ++++++++-------------- lib/manager.ts | 11 ++++--- lib/on.ts | 2 +- lib/socket.ts | 9 ++--- lib/url.ts | 5 +-- package.json | 17 ++++++---- postcompile.sh | 8 +++++ support/package.cjs.json | 3 ++ support/package.esm.json | 3 ++ support/webpack.config.js | 2 +- test/connection.ts | 10 +++--- test/fixtures/no-unref.ts | 2 +- test/fixtures/unref-during-reconnection.ts | 2 +- test/fixtures/unref-polling-only.ts | 2 +- test/fixtures/unref-websocket-only.ts | 2 +- test/fixtures/unref.ts | 2 +- test/socket.ts | 2 +- test/url.ts | 4 +-- tsconfig.esm.json | 12 +++++++ tsconfig.json | 8 ++--- wrapper.mjs | 6 ---- 21 files changed, 83 insertions(+), 67 deletions(-) create mode 100755 postcompile.sh create mode 100644 support/package.cjs.json create mode 100644 support/package.esm.json create mode 100644 tsconfig.esm.json delete mode 100644 wrapper.mjs diff --git a/lib/index.ts b/lib/index.ts index 903b78a5e..6dbdc1bdb 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,19 +1,14 @@ -import { url } from "./url"; -import { Manager, ManagerOptions } from "./manager"; -import { Socket, SocketOptions } from "./socket"; +import { url } from "./url.js"; +import { Manager, ManagerOptions } from "./manager.js"; +import { Socket, SocketOptions } from "./socket.js"; +import debugModule from "debug"; // debug() -const debug = require("debug")("socket.io-client"); - -/** - * Module exports. - */ - -module.exports = exports = lookup; +const debug = debugModule("socket.io-client"); // debug() /** * Managers cache. */ -const cache: Record = (exports.managers = {}); +const cache: Record = {}; /** * Looks up an existing `Manager` for multiplexing. @@ -84,22 +79,17 @@ function lookup( export { protocol } from "socket.io-parser"; -/** - * `connect`. - * - * @param {String} uri - * @public - */ - -exports.connect = lookup; - /** * Expose constructors for standalone build. * * @public */ -export { Manager, ManagerOptions } from "./manager"; -export { Socket } from "./socket"; -export { lookup as io, SocketOptions }; -export default lookup; +export { + Manager, + ManagerOptions, + Socket, + SocketOptions, + lookup as io, + lookup as connect, +}; diff --git a/lib/manager.ts b/lib/manager.ts index 9b8583a60..58923d788 100644 --- a/lib/manager.ts +++ b/lib/manager.ts @@ -3,18 +3,19 @@ import { SocketOptions as EngineOptions, installTimerFunctions, } from "engine.io-client"; -import { Socket, SocketOptions } from "./socket"; +import { Socket, SocketOptions } from "./socket.js"; import * as parser from "socket.io-parser"; import { Decoder, Encoder, Packet } from "socket.io-parser"; -import { on } from "./on"; -import * as Backoff from "backo2"; +import { on } from "./on.js"; +import Backoff from "backo2"; import { DefaultEventsMap, EventsMap, StrictEventEmitter, -} from "./typed-events"; +} from "./typed-events.js"; +import debugModule from "debug"; // debug() -const debug = require("debug")("socket.io-client:manager"); +const debug = debugModule("socket.io-client:manager"); // debug() export interface ManagerOptions extends EngineOptions { /** diff --git a/lib/on.ts b/lib/on.ts index 8ea249836..426d154e6 100644 --- a/lib/on.ts +++ b/lib/on.ts @@ -1,5 +1,5 @@ import type * as Emitter from "component-emitter"; -import { StrictEventEmitter } from "./typed-events"; +import { StrictEventEmitter } from "./typed-events.js"; export function on( obj: Emitter | StrictEventEmitter, diff --git a/lib/socket.ts b/lib/socket.ts index 86946e9c7..8a0044aaf 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -1,15 +1,16 @@ import { Packet, PacketType } from "socket.io-parser"; -import { on } from "./on"; -import { Manager } from "./manager"; +import { on } from "./on.js"; +import { Manager } from "./manager.js"; import { DefaultEventsMap, EventNames, EventParams, EventsMap, StrictEventEmitter, -} from "./typed-events"; +} from "./typed-events.js"; +import debugModule from "debug"; // debug() -const debug = require("debug")("socket.io-client:socket"); +const debug = debugModule("socket.io-client:socket"); // debug() export interface SocketOptions { /** diff --git a/lib/url.ts b/lib/url.ts index 46b4494c6..2514615a1 100644 --- a/lib/url.ts +++ b/lib/url.ts @@ -1,6 +1,7 @@ -import * as parseuri from "parseuri"; +import parseuri from "parseuri"; +import debugModule from "debug"; // debug() -const debug = require("debug")("socket.io-client:url"); +const debug = debugModule("socket.io-client:url"); // debug() type ParsedUrl = { source: string; diff --git a/package.json b/package.json index 81f1eea4f..5c0a0bcf1 100644 --- a/package.json +++ b/package.json @@ -11,21 +11,24 @@ ], "files": [ "dist/", - "build/", - "wrapper.mjs" + "build/" ], "type": "commonjs", - "main": "./build/index.js", + "main": "./build/cjs/index.js", + "module": "./build/esm/index.js", "exports": { "./package.json": "./package.json", "./dist/socket.io.js": "./dist/socket.io.js", "./dist/socket.io.js.map": "./dist/socket.io.js.map", ".": { - "import": "./wrapper.mjs", - "require": "./build/index.js" + "import": { + "node": "./build/esm-debug/index.js", + "default": "./build/esm/index.js" + }, + "require": "./build/cjs/index.js" } }, - "types": "./build/index.d.ts", + "types": "./build/esm/index.d.ts", "dependencies": { "@socket.io/component-emitter": "~2.0.0", "backo2": "~1.0.2", @@ -68,7 +71,7 @@ "zuul-ngrok": "4.0.0" }, "scripts": { - "compile": "rimraf ./build && tsc", + "compile": "rimraf ./build && tsc && tsc -p tsconfig.esm.json && ./postcompile.sh", "test": "npm run format:check && npm run compile && if test \"$BROWSERS\" = \"1\" ; then npm run test:browser; else npm run test:node; fi", "test:node": "mocha --require ts-node/register --reporter dot --require test/support/server.js test/index.js", "test:browser": "zuul test/index.js", diff --git a/postcompile.sh b/postcompile.sh new file mode 100755 index 000000000..5c3aa3906 --- /dev/null +++ b/postcompile.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +cp ./support/package.cjs.json ./build/cjs/package.json +cp ./support/package.esm.json ./build/esm/package.json + +cp -r ./build/esm/ ./build/esm-debug/ + +sed -i '/debug(/d' ./build/esm/*.js diff --git a/support/package.cjs.json b/support/package.cjs.json new file mode 100644 index 000000000..5bbefffba --- /dev/null +++ b/support/package.cjs.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/support/package.esm.json b/support/package.esm.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/support/package.esm.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/support/webpack.config.js b/support/webpack.config.js index 67af9b9ec..dd7a9acd8 100644 --- a/support/webpack.config.js +++ b/support/webpack.config.js @@ -6,7 +6,7 @@ const banner = `Socket.IO v${version} Released under the MIT License.`; module.exports = { - entry: "./build/index.js", + entry: "./build/esm/index.js", output: { filename: "socket.io.js", library: "io", diff --git a/test/connection.ts b/test/connection.ts index b3054b134..2473b2e4d 100644 --- a/test/connection.ts +++ b/test/connection.ts @@ -1,9 +1,9 @@ -import * as expect from "expect.js"; +import expect from "expect.js"; import { io, Manager, ManagerOptions } from ".."; -import * as hasCORS from "has-cors"; -import * as FakeTimers from "@sinonjs/fake-timers"; -import * as textBlobBuilder from "text-blob-builder"; -import * as env from "./support/env"; +import hasCORS from "has-cors"; +import FakeTimers from "@sinonjs/fake-timers"; +import textBlobBuilder from "text-blob-builder"; +import env from "./support/env"; describe("connection", function () { this.timeout(70000); diff --git a/test/fixtures/no-unref.ts b/test/fixtures/no-unref.ts index 82d8c0a06..a96c4e1a7 100644 --- a/test/fixtures/no-unref.ts +++ b/test/fixtures/no-unref.ts @@ -1,4 +1,4 @@ -const io = require("../.."); +const { io } = require("../.."); const socket = io("http://localhost:3211", { autoUnref: false, }); diff --git a/test/fixtures/unref-during-reconnection.ts b/test/fixtures/unref-during-reconnection.ts index d5558d75a..af585e8df 100644 --- a/test/fixtures/unref-during-reconnection.ts +++ b/test/fixtures/unref-during-reconnection.ts @@ -1,4 +1,4 @@ -const io = require("../.."); +const { io } = require("../.."); const socket = io("http://localhost:3211", { autoUnref: true, }); diff --git a/test/fixtures/unref-polling-only.ts b/test/fixtures/unref-polling-only.ts index 01419d636..a3ca9e365 100644 --- a/test/fixtures/unref-polling-only.ts +++ b/test/fixtures/unref-polling-only.ts @@ -1,4 +1,4 @@ -const io = require("../.."); +const { io } = require("../.."); const socket = io("http://localhost:3210", { autoUnref: true, transports: ["polling"], diff --git a/test/fixtures/unref-websocket-only.ts b/test/fixtures/unref-websocket-only.ts index a4e26893a..240b7c99c 100644 --- a/test/fixtures/unref-websocket-only.ts +++ b/test/fixtures/unref-websocket-only.ts @@ -1,4 +1,4 @@ -const io = require("../.."); +const { io } = require("../.."); const socket = io("http://localhost:3210", { autoUnref: true, transports: ["websocket"], diff --git a/test/fixtures/unref.ts b/test/fixtures/unref.ts index 8a281d832..63128ecdb 100644 --- a/test/fixtures/unref.ts +++ b/test/fixtures/unref.ts @@ -1,4 +1,4 @@ -const io = require("../.."); +const { io } = require("../.."); const socket = io("http://localhost:3210", { autoUnref: true, }); diff --git a/test/socket.ts b/test/socket.ts index f59026498..dae4082d7 100644 --- a/test/socket.ts +++ b/test/socket.ts @@ -1,4 +1,4 @@ -import * as expect from "expect.js"; +import expect from "expect.js"; import { io } from ".."; describe("socket", function () { diff --git a/test/url.ts b/test/url.ts index 37904f43b..f595535dc 100644 --- a/test/url.ts +++ b/test/url.ts @@ -1,5 +1,5 @@ -import { url } from "../build/url"; -import * as expect from "expect.js"; +import { url } from "../build/cjs/url"; +import expect from "expect.js"; const loc: any = {}; diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 000000000..2805bb330 --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "outDir": "build/esm/", + "target": "es2018", + "module": "esnext", + "moduleResolution": "node", + "declaration": true + }, + "include": [ + "./lib/**/*" + ] +} diff --git a/tsconfig.json b/tsconfig.json index 8cd92f0d6..89f1157b7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,10 +1,10 @@ { "compilerOptions": { - "outDir": "./build", - "allowJs": false, - "target": "es2017", + "outDir": "build/cjs/", + "target": "es2018", // Node.js 10 (https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping) "module": "commonjs", - "declaration": true + "declaration": false, + "esModuleInterop": true }, "include": [ "./lib/**/*" diff --git a/wrapper.mjs b/wrapper.mjs deleted file mode 100644 index 3853094bd..000000000 --- a/wrapper.mjs +++ /dev/null @@ -1,6 +0,0 @@ -import io from "./build/index.js"; - -export const Manager = io.Manager; -export const Socket = io.Socket; -export { io }; -export default io;