Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/some-tigers-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"next-ws": minor
---

Drop esm build, ship cjs only
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "next-ws",
"version": "2.0.14",
"packageManager": "pnpm@10.15.0",
"type": "module",
"description": "Add support for WebSockets in the Next.js app directory",
"license": "MIT",
"keywords": ["next", "websocket", "ws", "server", "client"],
Expand All @@ -21,13 +20,11 @@
"exports": {
"./client": {
"types": "./dist/client/index.d.ts",
"import": "./dist/client/index.js",
"require": "./dist/client/index.cjs"
"default": "./dist/client/index.cjs"
},
"./server": {
"types": "./dist/server/index.d.ts",
"import": "./dist/server/index.js",
"require": "./dist/server/index.cjs"
"default": "./dist/server/index.cjs"
},
"./package.json": "./package.json"
},
Expand Down
24 changes: 1 addition & 23 deletions src/server/helpers/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { pathToFileURL } from 'node:url';
import * as logger from 'next/dist/build/output/log.js';
import type NextNodeServer from 'next/dist/server/next-server.js';
import type { SocketHandler } from './socket';
Expand Down Expand Up @@ -99,28 +98,7 @@ export async function importRouteModule(

// @ts-expect-error - getPageModule is protected
const buildPagePath = nextServer.getPagePath(filePath);
return importModule<RouteModule>(buildPagePath);
}

/**
* Import a module from a file path using either import or require.
* @param modulePath The file path of the module.
* @returns The imported module.
* @throws If the module could not be imported.
*/
async function importModule<T>(modulePath: string): Promise<T> {
const moduleUrl = pathToFileURL(modulePath).toString();

try {
return import(moduleUrl);
} catch (requireError) {
try {
return require(modulePath);
} catch (requireError) {
console.error(`Both import and require failed for ${modulePath}`);
throw requireError;
}
}
return require(buildPagePath) as RouteModule;
}

export async function getSocketHandler(routeModule: RouteModule) {
Expand Down
6 changes: 4 additions & 2 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { defineConfig } from 'tsup';
export default defineConfig([
{
entry: ['src/{client,server}/index.ts'],
format: ['cjs', 'esm'],
// Keep the extension as .cjs as to not break the require() calls in the patches
outExtension: () => ({ js: '.cjs', dts: '.d.cts' }),
format: 'cjs',
dts: true,
},
{
entry: ['src/cli.ts'],
outExtension: () => ({ js: '.cjs' }),
format: 'cjs',
external: ['next-ws'],
noExternal: ['*'],
minify: true,
},
]);