diff --git a/packages/socket.io-client/package.json b/packages/socket.io-client/package.json index 972c7bfea..4f53f6ddb 100644 --- a/packages/socket.io-client/package.json +++ b/packages/socket.io-client/package.json @@ -32,6 +32,10 @@ "default": "./build/cjs/index.js" } }, + "./browser": { + "types": "./build/esm/index.d.ts", + "default": "./dist/socket.io.esm.min.js" + }, "./debug": { "import": { "types": "./build/esm/index.d.ts", diff --git a/packages/socket.io-client/test/browser-export.ts b/packages/socket.io-client/test/browser-export.ts new file mode 100644 index 000000000..64543ca60 --- /dev/null +++ b/packages/socket.io-client/test/browser-export.ts @@ -0,0 +1,34 @@ +import { existsSync, readFileSync } from "fs"; +import { createRequire } from "module"; +import { join } from "path"; +import expect = require("expect.js"); + +// the "socket.io-client/browser" subpath export allows importing the +// standalone ESM bundle directly (for example with Deno or in the browser), +// without a bundler and without copying the bundle locally +// see https://github.com/socketio/socket.io/issues/5383 +describe("browser export", () => { + const packageRoot = join(__dirname, ".."); + const pkg = JSON.parse( + readFileSync(join(packageRoot, "package.json"), "utf8"), + ); + + it("should declare a ./browser subpath export", () => { + expect(pkg.exports["./browser"]).to.eql({ + types: "./build/esm/index.d.ts", + default: "./dist/socket.io.esm.min.js", + }); + }); + + it("should point to files that exist", () => { + const entry = pkg.exports["./browser"]; + expect(existsSync(join(packageRoot, entry.default))).to.be(true); + expect(existsSync(join(packageRoot, entry.types))).to.be(true); + }); + + it("should be resolvable by its package name", () => { + const require = createRequire(join(packageRoot, "index.js")); + const resolved = require.resolve("socket.io-client/browser"); + expect(resolved).to.be(join(packageRoot, "dist", "socket.io.esm.min.js")); + }); +}); diff --git a/packages/socket.io-client/test/index.ts b/packages/socket.io-client/test/index.ts index 355c840ca..926b3e63b 100644 --- a/packages/socket.io-client/test/index.ts +++ b/packages/socket.io-client/test/index.ts @@ -1,3 +1,4 @@ +import "./browser-export"; import "./url"; import "./connection"; import "./socket";