Skip to content

Commit

Permalink
feat: provide an ESM build with and without debug
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Oct 13, 2021
1 parent 7187453 commit 16b6569
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 67 deletions.
38 changes: 14 additions & 24 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -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<string, Manager> = (exports.managers = {});
const cache: Record<string, Manager> = {};

/**
* Looks up an existing `Manager` for multiplexing.
Expand Down Expand Up @@ -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,

This comment has been minimized.

Copy link
@chriskrycho

chriskrycho Oct 14, 2021

Contributor

This appears to be the change which caused socketio/socket.io#4121. This module—not the browser-entrypoint module introduced in c76d367—is for some reason getting resolved as the root.

lookup as connect,
};
11 changes: 6 additions & 5 deletions lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/on.ts
Original file line number Diff line number Diff line change
@@ -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<any, any>,
Expand Down
9 changes: 5 additions & 4 deletions lib/socket.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down
5 changes: 3 additions & 2 deletions lib/url.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions postcompile.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions support/package.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
3 changes: 3 additions & 0 deletions support/package.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
2 changes: 1 addition & 1 deletion support/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions test/connection.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/no-unref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const io = require("../..");
const { io } = require("../..");
const socket = io("http://localhost:3211", {
autoUnref: false,
});
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/unref-during-reconnection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const io = require("../..");
const { io } = require("../..");
const socket = io("http://localhost:3211", {
autoUnref: true,
});
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/unref-polling-only.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const io = require("../..");
const { io } = require("../..");
const socket = io("http://localhost:3210", {
autoUnref: true,
transports: ["polling"],
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/unref-websocket-only.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const io = require("../..");
const { io } = require("../..");
const socket = io("http://localhost:3210", {
autoUnref: true,
transports: ["websocket"],
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/unref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const io = require("../..");
const { io } = require("../..");
const socket = io("http://localhost:3210", {
autoUnref: true,
});
Expand Down
2 changes: 1 addition & 1 deletion test/socket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as expect from "expect.js";
import expect from "expect.js";
import { io } from "..";

describe("socket", function () {
Expand Down
4 changes: 2 additions & 2 deletions test/url.ts
Original file line number Diff line number Diff line change
@@ -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 = {};

Expand Down
12 changes: 12 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"outDir": "build/esm/",
"target": "es2018",
"module": "esnext",
"moduleResolution": "node",
"declaration": true
},
"include": [
"./lib/**/*"
]
}
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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/**/*"
Expand Down
6 changes: 0 additions & 6 deletions wrapper.mjs

This file was deleted.

0 comments on commit 16b6569

Please sign in to comment.