Skip to content

Commit

Permalink
Preserve both namespace and ES module semantics
Browse files Browse the repository at this point in the history
Introduce a manual 'namespace' by assigning the other exported value objects to the `lookup` function, thereby allowing it to be called
directly, e.g. `io(...)`, or used to access the other values, e.g.
`io.connect(...)`. Maintain the other existing exports so that the nice
new ES module interface is still useful in its own right.
  • Loading branch information
chriskrycho committed Oct 15, 2021
1 parent 615e062 commit f26979a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/browser-entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * as io from "./index.js";
export { io as default } from "./index";
15 changes: 14 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ function lookup(

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

// Manually create a merged object which acts as both the `lookup` function and
// a 'namespace' on which the other functionality is exported and available.
// This lets callers invoke the funtion directly as `io(...)` and also lets them
// invoke subvalues on it, e.g. `io.connect(...)`. This allows the browser
// entrypoint module to export this `io` as default, while still providing a
// more general ES module export below.
const io = Object.assign(lookup, {
Manager,
Socket,
io: lookup,
connect: lookup,
});

/**
* Expose constructors for standalone build.
*
Expand All @@ -90,6 +103,6 @@ export {
ManagerOptions,
Socket,
SocketOptions,
lookup as io,
io,
lookup as connect,
};

0 comments on commit f26979a

Please sign in to comment.