Skip to content

Commit 1e29ec2

Browse files
authored
Drop esm build, ship cjs only (#109)
1 parent 9c9732d commit 1e29ec2

File tree

4 files changed

+12
-30
lines changed

4 files changed

+12
-30
lines changed

.changeset/some-tigers-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"next-ws": minor
3+
---
4+
5+
Drop esm build, ship cjs only

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"name": "next-ws",
33
"version": "2.0.14",
44
"packageManager": "[email protected]",
5-
"type": "module",
65
"description": "Add support for WebSockets in the Next.js app directory",
76
"license": "MIT",
87
"keywords": ["next", "websocket", "ws", "server", "client"],
@@ -21,13 +20,11 @@
2120
"exports": {
2221
"./client": {
2322
"types": "./dist/client/index.d.ts",
24-
"import": "./dist/client/index.js",
25-
"require": "./dist/client/index.cjs"
23+
"default": "./dist/client/index.cjs"
2624
},
2725
"./server": {
2826
"types": "./dist/server/index.d.ts",
29-
"import": "./dist/server/index.js",
30-
"require": "./dist/server/index.cjs"
27+
"default": "./dist/server/index.cjs"
3128
},
3229
"./package.json": "./package.json"
3330
},

src/server/helpers/route.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { pathToFileURL } from 'node:url';
21
import * as logger from 'next/dist/build/output/log.js';
32
import type NextNodeServer from 'next/dist/server/next-server.js';
43
import type { SocketHandler } from './socket';
@@ -99,28 +98,7 @@ export async function importRouteModule(
9998

10099
// @ts-expect-error - getPageModule is protected
101100
const buildPagePath = nextServer.getPagePath(filePath);
102-
return importModule<RouteModule>(buildPagePath);
103-
}
104-
105-
/**
106-
* Import a module from a file path using either import or require.
107-
* @param modulePath The file path of the module.
108-
* @returns The imported module.
109-
* @throws If the module could not be imported.
110-
*/
111-
async function importModule<T>(modulePath: string): Promise<T> {
112-
const moduleUrl = pathToFileURL(modulePath).toString();
113-
114-
try {
115-
return import(moduleUrl);
116-
} catch (requireError) {
117-
try {
118-
return require(modulePath);
119-
} catch (requireError) {
120-
console.error(`Both import and require failed for ${modulePath}`);
121-
throw requireError;
122-
}
123-
}
101+
return require(buildPagePath) as RouteModule;
124102
}
125103

126104
export async function getSocketHandler(routeModule: RouteModule) {

tsup.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import { defineConfig } from 'tsup';
33
export default defineConfig([
44
{
55
entry: ['src/{client,server}/index.ts'],
6-
format: ['cjs', 'esm'],
6+
// Keep the extension as .cjs as to not break the require() calls in the patches
7+
outExtension: () => ({ js: '.cjs', dts: '.d.cts' }),
8+
format: 'cjs',
79
dts: true,
810
},
911
{
1012
entry: ['src/cli.ts'],
13+
outExtension: () => ({ js: '.cjs' }),
1114
format: 'cjs',
1215
external: ['next-ws'],
1316
noExternal: ['*'],
14-
minify: true,
1517
},
1618
]);

0 commit comments

Comments
 (0)