bun-dev-server 0.9.5 → 0.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -51,26 +51,29 @@ bun run devserver.ts
51
51
  Options that are available for configuration
52
52
  | **Option** | **Required** | **Default** | **Description** |
53
53
  |------------------------|--------------|--------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
54
- | port | true | N/A | The port to run the server on. |
55
- | buildConfig | true | N/A | The build configuration to use for the server. |
56
- | watchDir | true | N/A | The directory to watch for changes. |
57
- | watchDelay | false | 1000 | The delay in milliseconds to wait before starting <br> a new build once a file has changed.<br> Used in debounce function, in case many changes <br>happen in file system at once. |
58
- | enableTSC | false | false | Whether to enable TypeScript checking <br>it will use `tscConfigPath` in your project directory. |
59
- | tscConfigPath | false | `./tsconfig.json` | The path to the TypeScript configuration file. |
60
- | writeManifest | false | false | Whether to write the manifest file. |
61
- | manifestName | false | `bun_server_manifest.json`| The name of the manifest file. |
62
- | manifestWithHash| false | false | Whether to include the hash with the entrypoints in the manifest file. |
63
- | hotReload | false | none | Where to place hot reload script. Available values: "plugin" | "footer" | "none" |
64
- | reloadOnChange | false | false | Whether to reload the page when a file changes. <br> This requires property `hotReload` to be set<br> to `"plugin"` or `"footer"`. |
65
- | logRequests | false | false | Whether to log HTTP requests to the console. |
66
- | servePath | false | `outdir` in build config or `./dist` | The path to the directory to serve files from.<br> Takes precedence over `buildConfig.outdir`. |
67
- | cleanServePath | false | false | Whether to clean the `servePath` before building a new batch. |
68
- | serveOutputEjs | false | Built-in template | The EJS template used for <br> the output of the `/` path on the server. <br> When supplying your own template,<br> the properties that are provided during rendering <br>are `files` and `dirs` both are arrays. |
69
- | serveIndexHtmlEjs | false | Built-in template | he EJS template used for the output of the `/index.html` path on the server. <br> When supplying your own template, <br>the property that is provided during rendering <br>is `hashedImports` that is an array of strings. |
70
- | createIndexHTML | false | true | Whether to create index.html using `serveIndexHtmlEjs` template. |
71
- | beforeBuild | false | undefined | Event listener to execute before Bun builds |
72
- | afterBuild | false | undefined | Event listener to execute after Bun builds |
73
- | tls | false | undefined | Secure options for the server. Forwards buns `TLSOptions` |
74
- | websocketPath | false | `/hmr-ws` | The websocket path to use for the server. |
75
- | static | false | undefined | Static bun responses, can be used for things like `/favicon.ico` etc. |
76
-
54
+ | port | true | N/A | The port to run the server on. |
55
+ | buildConfig | true | N/A | The build configuration to use for the server. |
56
+ | watchDir | true | N/A | The directory to watch for changes. |
57
+ | watchDelay | false | 1000 | The delay in milliseconds to wait before starting <br> a new build once a file has changed.<br> Used in debounce function, in case many changes <br>happen in file system at once. |
58
+ | enableTSC | false | false | Whether to enable TypeScript checking <br>it will use `tscConfigPath` in your project directory. |
59
+ | tscConfigPath | false | `./tsconfig.json` | The path to the TypeScript configuration file. |
60
+ | writeManifest | false | false | Whether to write the manifest file. |
61
+ | manifestName | false | `bun_server_manifest.json`| The name of the manifest file. |
62
+ | manifestWithHash| false | false | Whether to include the hash with the entrypoints in the manifest file. |
63
+ | hotReload | false | none | Where to place hot reload script. Available values: "plugin" | "footer" | "none" |
64
+ | reloadOnChange | false | false | Whether to reload the page when a file changes. <br> This requires property `hotReload` to be set<br> to `"plugin"` or `"footer"`. |
65
+ | logRequests | false | false | Whether to log HTTP requests to the console. |
66
+ | servePath | false | `outdir` in build config or `./dist` | The path to the directory to serve files from.<br> Takes precedence over `buildConfig.outdir`. |
67
+ | cleanServePath | false | false | Whether to clean the `servePath` before building a new batch. |
68
+ | serveOutputEjs | false | Built-in template | The EJS template used for <br> the output of the `/` path on the server. <br> When supplying your own template,<br> the properties that are provided during rendering <br>are `files` and `dirs` both are arrays. |
69
+ | serveIndexHtmlEjs | false | Built-in template | The EJS template used for the output of the `/index.html` path on the server. <br> When supplying your own template, <br>the property that is provided during rendering <br>is `hashedImports` that is an array of strings. |
70
+ | createIndexHTML | false | true | Whether to create index.html using `serveIndexHtmlEjs` template. |
71
+ | beforeBuild | false | undefined | Event listener to execute before Bun builds |
72
+ | afterBuild | false | undefined | Event listener to execute after Bun builds |
73
+ | tls | false | undefined | Secure options for the server. Forwards buns `TLSOptions` |
74
+ | websocketPath | false | `/hmr-ws` | The websocket path to use for the server. |
75
+ | static | false | undefined | Static bun responses, can be used for things like `/favicon.ico` etc. |
76
+ | waitForTSCSuccessBeforeReload | false | undefined | Whether to wait for the TypeScript check to finish before reloading the page. |
77
+ | broadcastBuildOutputToClient | false | `true` | Whether to broadcast the build output to the browser.
78
+ | broadcastBuildOutputToConsole | false | `true` | Whether to broadcast the build output to the console.
79
+ | broadcastTSCErrorToClient | false | undefined | Whether to broadcast the TypeScript check error to the client.
@@ -32,6 +32,11 @@ export interface BunDevServerConfig extends Partial<BunServeConfig> {
32
32
  * @default false
33
33
  */
34
34
  enableTSC?: boolean;
35
+ /**
36
+ * Whether to broadcast the TypeScript check error to the client.
37
+ * @default false
38
+ */
39
+ broadcastTSCErrorToClient?: boolean;
35
40
  /**
36
41
  * The path to the TypeScript configuration file.
37
42
  * @default "./tsconfig.json".
@@ -57,6 +62,21 @@ export interface BunDevServerConfig extends Partial<BunServeConfig> {
57
62
  * @default "none"
58
63
  */
59
64
  hotReload?: "plugin" | "footer" | "none";
65
+ /**
66
+ * Whether to wait for the TypeScript check to finish before reloading the page.
67
+ * @default false
68
+ */
69
+ waitForTSCSuccessBeforeReload?: boolean;
70
+ /**
71
+ * Whether to broadcast the build output to the browser.
72
+ * @default true
73
+ */
74
+ broadcastBuildOutputToClient?: boolean;
75
+ /**
76
+ * Whether to broadcast the build output to the console.
77
+ * @default true
78
+ */
79
+ broadcastBuildOutputToConsole?: boolean;
60
80
  /**
61
81
  * Whether to reload the page when a file changes.
62
82
  * This requires property `hotReload` to be set to `"plugin"` or `"footer"`.
package/dist/index.js CHANGED
@@ -1006,6 +1006,10 @@ function hotReload() {
1006
1006
  window.location.reload();
1007
1007
  return;
1008
1008
  }
1009
+ if (parsed?.type === "tscerror") {
1010
+ console.error(parsed.message);
1011
+ return;
1012
+ }
1009
1013
  if (parsed?.type === "error") {
1010
1014
  console.error(parsed.message);
1011
1015
  let newDiv = window.document.getElementById("bun-hmr-error");
@@ -1114,20 +1118,28 @@ function writeManifest(output, outdir, withHash = false, manifestName = "bun_ser
1114
1118
  // src/tsChecker.ts
1115
1119
  var import_picocolors = __toESM(require_picocolors(), 1);
1116
1120
  var {$ } = globalThis.Bun;
1121
+ var success = {
1122
+ error: false,
1123
+ message: ""
1124
+ };
1117
1125
  async function performTSC(finalConfig, importMeta) {
1118
1126
  if (finalConfig.enableTSC) {
1119
1127
  console.log("Performing TSC check");
1120
1128
  const tsc = await $`tsc --noEmit --noErrorTruncation -p ${finalConfig.tscConfigPath}`.cwd(importMeta.dir).quiet().nothrow();
1121
1129
  if (tsc.exitCode === 0) {
1122
1130
  console.log(import_picocolors.default.bgGreen("\u2714 [SUCCESS]"), "TSC check passed");
1123
- return true;
1131
+ return success;
1124
1132
  } else {
1133
+ const errOutput = tsc.stdout.toString();
1125
1134
  console.log(import_picocolors.default.bgRed("\u2718 [ERROR]"), `\r
1126
- ${tsc.stdout.toString()}`);
1127
- return false;
1135
+ ${errOutput}`);
1136
+ return {
1137
+ error: true,
1138
+ message: errOutput
1139
+ };
1128
1140
  }
1129
1141
  }
1130
- return true;
1142
+ return success;
1131
1143
  }
1132
1144
 
1133
1145
  // src/server.ts
@@ -1140,7 +1152,9 @@ async function startBunDevServer(serverConfig, importMeta) {
1140
1152
  serveOutputEjs: serveOutputTemplate_default,
1141
1153
  serveIndexHtmlEjs: indexHTMLTemplate_default,
1142
1154
  createIndexHTML: true,
1143
- tscConfigPath: resolve(importMeta.dir, "./tsconfig.json")
1155
+ tscConfigPath: resolve(importMeta.dir, "./tsconfig.json"),
1156
+ broadcastBuildOutputToConsole: true,
1157
+ broadcastBuildOutputToClient: true
1144
1158
  };
1145
1159
  const finalConfig = { ...defaultConfig, ...serverConfig };
1146
1160
  if (finalConfig.watchDelay) {
@@ -1207,7 +1221,7 @@ async function startBunDevServer(serverConfig, importMeta) {
1207
1221
  if (req.url.toLowerCase().endsWith(finalConfig.websocketPath)) {
1208
1222
  finalConfig.logRequests && console.log(`${req.url} Socket Upgrade`);
1209
1223
  if (server.upgrade(req)) {
1210
- return;
1224
+ return withCORSHeaders(new Response("", { status: 200 }), req);
1211
1225
  }
1212
1226
  }
1213
1227
  const url = new URL(req.url);
@@ -1244,7 +1258,7 @@ var debouncedbuildAndNotify = import_debounce.default(async (importerMeta, final
1244
1258
  finalConfig.beforeBuild?.(buildEnv);
1245
1259
  try {
1246
1260
  const output = await Bun.build(buildCfg);
1247
- publishOutputLogs(bunServer, output, event);
1261
+ publishOutputLogs(bunServer, output, finalConfig, event);
1248
1262
  if (finalConfig.createIndexHTML) {
1249
1263
  publishIndexHTML(destinationPath, finalConfig.serveIndexHtmlEjs, output, event);
1250
1264
  }
@@ -1252,10 +1266,16 @@ var debouncedbuildAndNotify = import_debounce.default(async (importerMeta, final
1252
1266
  writeManifest(output, destinationPath, finalConfig.manifestWithHash, finalConfig.manifestName);
1253
1267
  }
1254
1268
  finalConfig.afterBuild?.(output, buildEnv);
1269
+ if (finalConfig.reloadOnChange && !finalConfig.waitForTSCSuccessBeforeReload) {
1270
+ bunServer.publish("message", JSON.stringify({ type: "reload" }));
1271
+ }
1255
1272
  const tscSuccess = await performTSC(finalConfig, importerMeta);
1256
- if (finalConfig.reloadOnChange && tscSuccess) {
1273
+ if (finalConfig.reloadOnChange && finalConfig.waitForTSCSuccessBeforeReload && !tscSuccess.error) {
1257
1274
  bunServer.publish("message", JSON.stringify({ type: "reload" }));
1258
1275
  }
1276
+ if (tscSuccess.error && finalConfig.broadcastTSCErrorToClient) {
1277
+ bunServer.publish("message", JSON.stringify({ type: "tscerror", message: tscSuccess.message }));
1278
+ }
1259
1279
  } catch (e) {
1260
1280
  console.error(e);
1261
1281
  }
@@ -1265,7 +1285,7 @@ function handleErrorResponse(req, err) {
1265
1285
  console.error(msg, err);
1266
1286
  return withCORSHeaders(new Response(msg, { status: 500 }), req);
1267
1287
  }
1268
- function publishOutputLogs(bunServer, output, event) {
1288
+ function publishOutputLogs(bunServer, output, config, event) {
1269
1289
  output.logs.forEach(console.log);
1270
1290
  bunServer.publish("message", JSON.stringify({ type: "message", message: `[Bun HMR] ${event.filename} ${event.eventType}` }));
1271
1291
  const outTable = output.outputs.filter((o) => o.kind !== "sourcemap").map((o) => {
@@ -1277,8 +1297,12 @@ function publishOutputLogs(bunServer, output, event) {
1277
1297
  size: convertBytes(o.size)
1278
1298
  };
1279
1299
  });
1280
- console.table(outTable);
1281
- bunServer.publish("message", JSON.stringify({ type: "output", message: outTable }));
1300
+ if (config.broadcastBuildOutputToConsole) {
1301
+ console.table(outTable);
1302
+ }
1303
+ if (config.broadcastBuildOutputToClient) {
1304
+ bunServer.publish("message", JSON.stringify({ type: "output", message: outTable }));
1305
+ }
1282
1306
  }
1283
1307
  function publishIndexHTML(destinationPath, template, output, _event) {
1284
1308
  const eps = output.outputs.filter((o) => o.kind === "entry-point");
@@ -1,2 +1,5 @@
1
1
  import type { BunDevServerConfig } from "./bunServeConfig";
2
- export declare function performTSC(finalConfig: BunDevServerConfig, importMeta: ImportMeta): Promise<boolean>;
2
+ export declare function performTSC(finalConfig: BunDevServerConfig, importMeta: ImportMeta): Promise<{
3
+ error: boolean;
4
+ message: string;
5
+ }>;
package/package.json CHANGED
@@ -24,7 +24,7 @@
24
24
  "exports": {
25
25
  ".": "./dist/index.js"
26
26
  },
27
- "version": "0.9.5",
27
+ "version": "0.9.7",
28
28
  "module": "index.ts",
29
29
  "type": "module",
30
30
  "license": "MIT",
@@ -32,10 +32,10 @@
32
32
  "build": "bun run ./build.ts"
33
33
  },
34
34
  "devDependencies": {
35
- "@types/bun": "^1.2.4"
35
+ "@types/bun": "^1.2.5"
36
36
  },
37
37
  "peerDependencies": {
38
- "typescript": "^5.7.3"
38
+ "typescript": "^5.8.2"
39
39
  },
40
40
  "dependencies": {
41
41
  "@types/ejs": "^3.1.5",