Skip to content

Commit b2d101e

Browse files
author
David Reed
committed
Update configurations to target CommonJS.
This commit will allow importing bare-server-node as a CommonJS package. A scenario that has led me to believe this change is necessary is the use of this package in CRA's (create-react-app) hook for setting up proxies. CRA only provides a CommonJS hook, which requires a weird workaround in order to register the Bare Server as a route in Express (before the dev server claims it) and then await a promise in the handler. This kind of syntax was not envisioned when bare-server-node was configured to target ESM. This will not affect how the package is imported or required.
1 parent 9b7a391 commit b2d101e

File tree

5 files changed

+9
-8
lines changed

5 files changed

+9
-8
lines changed

bin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env node
2-
import('./dist/cli.js');
2+
require('./dist/cli.js');

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@tomphttp/bare-server-node",
33
"description": "The Bare Server implementation in NodeJS.",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"homepage": "https://github.com/tomphttp",
66
"bugs": {
77
"url": "https://github.com/tomphttp/bare-server-node/issues",
@@ -22,7 +22,7 @@
2222
"tomphttp"
2323
],
2424
"license": "GPL-3.0",
25-
"type": "module",
25+
"type": "commonjs",
2626
"main": "dist/createServer.js",
2727
"types": "dist/createServer.d.ts",
2828
"files": [

src/BareServer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { Request, Response, writeResponse } from './AbstractMessage.js';
22
import type { BareHeaders } from './requestUtil.js';
33
import createHttpError from 'http-errors';
44
import { EventEmitter } from 'node:events';
5-
import { readFile } from 'node:fs/promises';
5+
import { readFileSync } from 'node:fs';
66
import type { IncomingMessage, ServerResponse } from 'node:http';
7+
import { join } from 'node:path';
78
import type { Duplex } from 'node:stream';
89

910
export interface BareErrorBody {
@@ -24,7 +25,7 @@ export class BareError extends Error {
2425
}
2526

2627
const pkg = JSON.parse(
27-
await readFile(new URL('../package.json', import.meta.url), 'utf-8')
28+
readFileSync(join(__dirname, '..', 'package.json'), 'utf-8')
2829
);
2930

3031
const project: BareProject = {

src/createServer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import type { ServerConfig } from './BareServer.js';
33
import registerV1 from './V1.js';
44
import registerV2 from './V2.js';
55

6-
export default function createBareServer(
6+
export = function createBareServer(
77
directory: string,
88
init: Partial<ServerConfig> = {}
99
) {
1010
const server = new BareServer(directory, init);
1111
registerV1(server);
1212
registerV2(server);
1313
return server;
14-
}
14+
};

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"outDir": "dist",
44
"lib": ["ESNext", "DOM"],
55
"target": "ESNext",
6-
"module": "ESNext",
6+
"module": "CommonJS",
77
"moduleResolution": "Node16",
88
"strict": true,
99
"stripInternal": true,

0 commit comments

Comments
 (0)